:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --border: #d1d5db;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --text: #111827;
  --text-muted: #6b7280;
  --danger: #dc2626;
  --success: #16a34a;
  --radius: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,.12);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ── */
header {
  background: var(--accent);
  color: #fff;
  padding: 10px 20px;
  display: flex;
  align-items: center;
}
header h1 { font-size: 18px; font-weight: 600; letter-spacing: .3px; }

/* ── Toolbar ── */
.toolbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.toolbar-label { color: var(--text-muted); margin-right: 4px; }

/* ── Main layout ── */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
  gap: 0;
}

.results-panel {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: var(--bg);
}
.results-panel h2 { font-size: 15px; color: var(--text-muted); margin-bottom: 12px; }

.matroids-panel {
  width: 340px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.panel-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.panel-header h2 { font-size: 15px; }

#matroid-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ── Matroid card ── */
.matroid-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: var(--surface);
  transition: box-shadow .15s;
}
.matroid-card:hover { box-shadow: var(--shadow); }
.matroid-card.selected { border-color: var(--accent); background: #eff6ff; }

.card-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.card-top input[type=checkbox] { flex-shrink: 0; width: 15px; height: 15px; cursor: pointer; }
.card-label {
  font-family: monospace;
  font-size: 13px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  cursor: text;
  border-radius: 3px;
  padding: 0 2px;
}
.card-label:hover { color: var(--accent); }
.card-label-input {
  font-family: monospace;
  font-size: 13px;
  font-weight: 600;
  flex: 1;
  width: 100%;
  border: 1px solid var(--accent);
  border-radius: 3px;
  padding: 0 4px;
  outline: none;
  color: var(--text);
}
.card-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
  margin-left: 23px;
}
.card-actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  margin-left: 23px;
}

/* ── Buttons ── */
body.busy { cursor: wait; }
body.busy button { cursor: wait; }

/* ── Hover tooltip (data-tooltip attribute) ── */
.has-tooltip { position: relative; }
.has-tooltip::after {
  content: attr(data-tooltip);
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: #1f2937;
  color: #f9fafb;
  font-size: 11px;
  line-height: 1.6;
  padding: 7px 11px;
  border-radius: 5px;
  white-space: pre;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 300;
  width: max-content;
  max-width: 340px;
  box-shadow: 0 4px 12px rgba(0,0,0,.3);
}
.has-tooltip:hover::after { opacity: 1; }

button { cursor: pointer; border: none; border-radius: var(--radius); font-size: 13px; padding: 5px 10px; transition: transform 0.08s ease; }
button:active { transform: scale(0.95); }

.btn-primary   { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }

.btn-secondary { background: #e5e7eb; color: var(--text); }
.btn-secondary:hover { background: #d1d5db; }

.btn-add   { background: var(--accent); color: #fff; font-size: 13px; padding: 4px 10px; }
.btn-close { background: transparent; color: var(--text-muted); font-size: 16px; padding: 0 4px; }
.btn-close:hover { color: var(--danger); }

.btn-sm { padding: 3px 8px; font-size: 12px; }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-op { background: #f3f4f6; color: var(--text); }
.btn-op:hover { background: #e5e7eb; }

/* ── Results ── */
.results-placeholder { color: var(--text-muted); font-style: italic; }

.result-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
}
.result-title {
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 6px;
  color: var(--text-muted);
}
.result-formula {
  font-family: monospace;
  font-size: 13px;
  line-height: 1.8;
  word-break: break-word;
}
.result-rank {
  font-size: 22px;
  font-weight: 700;
  color: var(--accent);
}
.result-rank-meta { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.result-error { color: var(--danger); font-size: 13px; }

/* ── Modals ── */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.35);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal-backdrop.hidden { display: none; }

.modal {
  background: var(--surface);
  border-radius: 8px;
  padding: 20px;
  width: 480px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 8px 30px rgba(0,0,0,.2);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px;
}
.modal-header h3 { font-size: 16px; }

.modal label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--text-muted);
}
.modal label input, .modal label textarea {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 8px;
  font-size: 13px;
  color: var(--text);
  outline: none;
}
.modal label input:focus, .modal label textarea:focus { border-color: var(--accent); }

.hint { font-size: 12px; color: var(--text-muted); margin-bottom: 10px; line-height: 1.5; }
.mx-notation-hint { margin-top: 6px; border-left: 2px solid var(--border); padding-left: 6px; }

/* ── Tabs ── */
.tabs { display: flex; gap: 2px; margin-bottom: 16px; border-bottom: 1px solid var(--border); }
.tab { background: transparent; color: var(--text-muted); padding: 6px 12px; border-radius: var(--radius) var(--radius) 0 0; font-size: 13px; }
.tab:hover { background: var(--bg); }
.tab.active { background: var(--surface); color: var(--accent); border: 1px solid var(--border); border-bottom: 1px solid var(--surface); margin-bottom: -1px; font-weight: 600; }

.tab-content { display: block; }
.tab-content.hidden { display: none; }

/* ── Checkboxes / radios ── */
.checkbox-grid {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin-bottom: 12px;
}
.checkbox-grid label {
  flex-direction: row;
  align-items: center;
  gap: 4px;
  margin: 0;
  color: var(--text);
  cursor: pointer;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}
.checkbox-grid label:hover { border-color: var(--accent); }
.checkbox-grid input[type=checkbox] { width: 13px; height: 13px; }

.radio-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.radio-list label {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text);
  margin: 0;
}
.radio-list label:hover { border-color: var(--accent); background: #eff6ff; }

/* ── Graph canvas ── */
#graph-canvas {
  display: block;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: crosshair;
  background: #fafafa;
  width: 100%;
  height: 300px;
}
.graph-controls { display: flex; gap: 8px; margin-top: 8px; justify-content: flex-end; }

.row-inputs { display: flex; gap: 16px; margin-bottom: 12px; }
.row-inputs label { flex: 1; }

.nested-btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }

.panel-subheader {
  padding: 5px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
}
.select-all-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}
.select-all-label input { width: 13px; height: 13px; cursor: pointer; }

.modal-action-row { display: flex; gap: 8px; margin-top: 4px; }

.subheader-actions { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }

.bases-list {
  font-family: monospace;
  font-size: 13px;
  line-height: 1.8;
  max-height: 360px;
  overflow-y: auto;
}
.bases-list .basis-row {
  padding: 3px 6px;
  border-radius: 4px;
}
.bases-list .basis-row:nth-child(odd) { background: var(--bg); }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Generic hidden helper ── */
.hidden { display: none !important; }

/* ── Main tabs ── */
.main-tabs {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  display: flex;
  gap: 0;
  flex-shrink: 0;
}
.main-tab-btn {
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  margin-bottom: -1px;
  cursor: pointer;
  transition: color .15s;
}
.main-tab-btn:hover { color: var(--text); }
.main-tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }
.main-tab-content {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

/* ── Matrix Explorer layout ── */
.mx-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}
.mx-config {
  width: 260px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.mx-display {
  flex: 1;
  overflow: auto;
  padding: 20px;
  background: var(--bg);
  min-width: 0;
}
.mx-placeholder { color: var(--text-muted); font-style: italic; }

/* Config form elements */
.mx-field-group { display: flex; flex-direction: column; gap: 5px; }
.mx-field-label {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .6px;
  margin-bottom: 2px;
}
.mx-radio {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  cursor: pointer;
  padding: 1px 0;
  color: var(--text);
}
.mx-radio input[type=radio] { cursor: pointer; flex-shrink: 0; }
.mx-row-inputs { display: flex; gap: 10px; }
.mx-input-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--text-muted);
  flex: 1;
}
.mx-input-label input {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 7px;
  font-size: 13px;
  outline: none;
  width: 100%;
  color: var(--text);
}
.mx-input-label input:focus { border-color: var(--accent); }

/* Stats bar */
.mx-stats {
  display: flex;
  gap: 20px;
  align-items: center;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--text-muted);
  flex-wrap: wrap;
}
.mx-stat-dim { color: var(--text); }
.mx-stat-dim strong { color: var(--accent); font-size: 15px; }
.mx-stats-actions { margin-left: auto; display: flex; gap: 8px; }

/* Matrix table */
.mx-table-wrap {
  overflow: auto;
  max-height: calc(100vh - 200px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.mx-table {
  border-collapse: collapse;
  font-size: 12px;
  font-family: monospace;
  white-space: nowrap;
}
.mx-corner {
  background: var(--surface);
  position: sticky;
  top: 0;
  left: 0;
  z-index: 4;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
}
.mx-col-hdr {
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 2;
  padding: 0;
  border-bottom: 2px solid var(--border);
  border-right: 1px solid #eee;
  vertical-align: bottom;
  height: 96px;
}
.mx-col-hdr div {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  padding: 6px 7px;
  white-space: nowrap;
  font-size: 11px;
  color: var(--text-muted);
}
.mx-row-hdr {
  position: sticky;
  left: 0;
  background: var(--surface);
  z-index: 1;
  font-weight: 500;
  font-size: 11px;
  color: var(--text);
  padding: 4px 10px 4px 8px;
  border-right: 2px solid var(--border);
  border-bottom: 1px solid #eee;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
}
.mx-cell {
  text-align: center;
  padding: 3px 6px;
  border-right: 1px solid #eee;
  border-bottom: 1px solid #eee;
  min-width: 28px;
  font-variant-numeric: tabular-nums;
}
.mx-cell-zero { color: #d1d5db; }
.mx-cell-pos  { color: var(--accent); font-weight: 600; }
.mx-cell-neg  { color: var(--danger); font-weight: 600; }
.mx-table tbody tr:hover .mx-cell,
.mx-table tbody tr:hover .mx-row-hdr { background: #eff6ff; }

/* Clickable row/col headers */
.mx-row-hdr { cursor: pointer; user-select: none; }
.mx-col-hdr { cursor: pointer; user-select: none; }
.mx-row-hdr:hover { color: var(--accent); }
.mx-col-hdr:hover div span { color: var(--accent); }

/* Drag-and-drop highlights */
.mx-table tr.drag-over .mx-row-hdr,
.mx-table tr.drag-over .mx-cell { background: #bfdbfe !important; }
.mx-col-hdr.drag-over div { background: #bfdbfe !important; }

/* ── Sidebar toggle button ── */
.panel-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}
.btn-sidebar-toggle {
  width: 24px;
  height: 24px;
  padding: 0;
  background: var(--bg);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.btn-sidebar-toggle:hover { background: var(--border); color: var(--text); }

/* ── Matroids panel collapse ── */
.matroids-panel { transition: width 0.2s ease; }
.matroids-panel.collapsed {
  width: 28px;
  min-width: 28px;
}
.matroids-panel.collapsed .panel-subheader,
.matroids-panel.collapsed #matroid-list,
.matroids-panel.collapsed .panel-header h2,
.matroids-panel.collapsed .btn-add {
  display: none;
}
.matroids-panel.collapsed .panel-header {
  padding: 8px 2px;
  justify-content: center;
}

/* ── mx-config collapse ── */
.mx-config { transition: width 0.2s ease; position: relative; }
.mx-sidebar-toggle {
  align-self: flex-end;
  margin-bottom: 4px;
}
.mx-config.collapsed {
  width: 28px;
  min-width: 28px;
  padding: 8px 2px;
  align-items: center;
}
.mx-config.collapsed .mx-config-inner { display: none; }

