:root {
  --green: #1b4332;
  --green-bg: #b7e4c7;
  --tight: #2d6a4f;
  --tight-bg: #d8f3dc;
  --yellow: #6b5b00;
  --yellow-bg: #fefae0;
  --orange: #8a4000;
  --orange-bg: #ffe8cc;
  --red: #9d0208;
  --red-bg: #fde2e4;
  --border: #c9c9c9;
  --bg: #fafaf8;
  --text: #1a1a1a;
  --muted: #555;
  --panel-bg: #f5f4f0;
}

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

body {
  font-family: Charter, 'Bitstream Charter', 'Iowan Old Style', 'Palatino Linotype', Georgia, serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 15px;
}

header {
  max-width: 1400px;
  margin: 0 auto;
  padding: 2rem 1.5rem 1rem;
}

header h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: 0.3rem; }

header p {
  color: var(--muted);
  font-size: 0.92rem;
  max-width: 720px;
}

.controls {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0.75rem 1.5rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  align-items: flex-start;
  border-bottom: 1px solid var(--border);
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.control-group > label {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.options {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  align-items: center;
}

.options label {
  font-size: 0.85rem;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.delta-regime-rows {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.delta-row { align-items: center; }
.delta-row-label { font-size: 0.85rem; }
label:has(input:disabled) {
  opacity: 0.35;
  cursor: not-allowed;
}

.view-toggle {
  margin-left: auto;
  align-self: center;
}

.view-toggle button {
  font-family: inherit;
  font-size: 0.85rem;
  padding: 0.35rem 0.8rem;
  border: 1px solid var(--border);
  background: white;
  cursor: pointer;
  color: var(--text);
  transition: background 0.15s;
}

.view-toggle button:first-child { border-radius: 4px 0 0 4px; }
.view-toggle button:last-child { border-radius: 0 4px 4px 0; border-left: none; }
.view-toggle button.active { background: var(--text); color: white; }

/* Grid view */
.grid-wrap {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1.5rem;
  display: flex;
  gap: 1.5rem;
}

.grid-container { flex: 1; min-width: 0; overflow-x: auto; }

.result-grid {
  width: 100%;
  min-width: 960px;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.result-grid th {
  background: var(--panel-bg);
  padding: 0.4rem 0.5rem;
  text-align: center;
  font-weight: 600;
  font-size: 0.76rem;
  border: 1px solid var(--border);
  overflow: hidden;
}

.result-grid th.row-header-label {
  text-align: left;
  width: 115px;
  min-width: 115px;
}

.result-grid td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.45rem;
  vertical-align: top;
  cursor: pointer;
  transition: box-shadow 0.15s;
  overflow: hidden;
}

.result-grid td:hover { box-shadow: inset 0 0 0 2px #333; }

.result-grid th.col-spacer,
.result-grid td.col-spacer {
  width: 12px;
  min-width: 12px;
  padding: 0;
  border: none;
  background: var(--bg);
  cursor: default;
}

.result-grid td.col-spacer:hover { box-shadow: none; }

.result-grid td.row-label {
  font-weight: 600;
  font-size: 0.82rem;
  background: var(--panel-bg);
  cursor: help;
}

.result-grid td.row-label:hover { box-shadow: none; }

.cell-bound { font-weight: 600; font-size: 0.84rem; margin-bottom: 0.15rem; }
.cell-meta { font-size: 0.7rem; color: var(--muted); }
.cell-lower {
  font-size: 0.68rem;
  margin-top: 0.25rem;
  padding-top: 0.2rem;
  border-top: 1px dashed var(--border);
  color: var(--muted);
}

.cell-green { background: var(--green-bg); }
.cell-green .cell-bound { color: var(--green); }
.cell-tight { background: var(--tight-bg); }
.cell-tight .cell-bound { color: var(--tight); }
.cell-yellow { background: var(--yellow-bg); }
.cell-yellow .cell-bound { color: var(--yellow); }
.cell-orange { background: var(--orange-bg); }
.cell-orange .cell-bound { color: var(--orange); }
.cell-red { background: var(--red-bg); }
.cell-red .cell-bound { color: var(--red); }

.cell-na {
  background:
    repeating-linear-gradient(
      135deg,
      #ececea,
      #ececea 6px,
      #f4f4f1 6px,
      #f4f4f1 12px
    );
  cursor: help;
}
.cell-na .cell-na-label {
  text-align: center;
  font-style: italic;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.2rem;
}
.cell-na .cell-na-desc {
  text-align: center;
  font-size: 0.68rem;
  color: var(--muted);
  line-height: 1.25;
}

/* Detail panel */
.detail-panel {
  width: 280px;
  min-width: 280px;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1.25rem;
  align-self: flex-start;
  position: sticky;
  top: 1rem;
}

.detail-panel.empty {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  color: var(--muted);
  font-style: italic;
  font-size: 0.9rem;
}

.detail-panel h3 {
  font-size: 1rem;
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

.detail-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
  font-size: 0.85rem;
}

.detail-label {
  font-weight: 600;
  min-width: 95px;
  color: var(--muted);
  flex-shrink: 0;
}

.detail-value { flex: 1; }
.detail-value a { color: #1a5276; text-decoration: none; }
.detail-value a:hover { text-decoration: underline; }

.detail-notes {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.detail-notes a {
  color: #1a5276;
  text-decoration: none;
  border-bottom: 1px dotted #1a5276;
}
.detail-notes a:hover { color: #154360; border-bottom-style: solid; }

.detail-relations {
  margin-top: 0.5rem;
  padding: 0.4rem 0.5rem;
  background: rgba(123, 94, 167, 0.08);
  border-left: 3px solid #7b5ea7;
  border-radius: 0 3px 3px 0;
}

.detail-relations .detail-label { color: #5d4584; }
.detail-relations .relations-note {
  color: var(--muted);
  font-style: italic;
  font-size: 0.78rem;
}

.problem-link {
  color: #5d4584;
  font-weight: 600;
  text-decoration: none;
  border-bottom: 1px dotted #5d4584;
  cursor: pointer;
}
.problem-link:hover { color: #3d2a5e; border-bottom-style: solid; }

.cite-ref {
  color: #1a5276;
  cursor: pointer;
  border-bottom: 1px dotted #1a5276;
}
.cite-ref:hover { color: #154360; border-bottom-style: solid; }

/* Number line */
.numline {
  margin: 0.75rem 0;
  padding: 0 0.25rem;
}

.numline-label {
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.numline-track {
  position: relative;
  height: 18px;
  background: #e8e8e4;
  border-radius: 3px;
  margin-bottom: 0.15rem;
  overflow: visible;
}

.numline-gap {
  position: absolute;
  top: 0;
  height: 100%;
  background: repeating-linear-gradient(
    -45deg,
    var(--red-bg),
    var(--red-bg) 3px,
    #f5c6c8 3px,
    #f5c6c8 6px
  );
  border-radius: 2px;
  opacity: 0.7;
}

.numline-region {
  position: absolute;
  top: 0;
  height: 100%;
  border-radius: 2px;
}

.numline-region.ub { background: var(--green-bg); border: 1px solid var(--green); }
.numline-region.ub-yellow { background: var(--yellow-bg); border: 1px solid var(--yellow); }
.numline-region.ub-orange { background: var(--orange-bg); border: 1px solid var(--orange); }
.numline-region.lb { background: #e0d4f5; border: 1px solid #7b5ea7; }

.numline-marker {
  position: absolute;
  top: -2px;
  width: 2px;
  height: 22px;
  transform: translateX(-1px);
}

.numline-marker.offline {
  background: #333;
  top: -4px;
  height: 26px;
  opacity: 0.5;
}

.numline-ticks {
  position: relative;
  height: 14px;
  font-size: 0.62rem;
  color: var(--muted);
}

.numline-tick {
  position: absolute;
  transform: translateX(-50%);
  top: 0;
}

.numline-annotations {
  margin-top: 0.35rem;
  font-size: 0.7rem;
  line-height: 1.5;
}

.numline-annotations .lb-annot { color: #7b5ea7; }
.numline-annotations .ub-annot { color: var(--green); }
.numline-annotations .ub-annot.yellow { color: var(--yellow); }
.numline-annotations .ub-annot.orange { color: var(--orange); }
.numline-annotations .gap-annot { color: var(--red); font-style: italic; }
.numline-annotations .offline-annot { color: #555; }

.numline-stack { margin: 0.75rem 0; }
.numline-stack .numline-stack-label {
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 0.4rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.numline-stack .numline {
  margin: 0;
  padding: 0.4rem 0.25rem;
}
.numline-stack .numline + .numline {
  border-top: 1px dashed var(--border);
}
.numline-stack .numline.active {
  background: rgba(0, 0, 0, 0.025);
  border-radius: 4px;
}
.numline-regime-header {
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
}
.numline-regime-header .regime-tex { color: var(--text); font-weight: 600; }
.numline-regime-header .active-tag {
  font-size: 0.62rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--green);
  background: var(--green-bg);
  padding: 0.05rem 0.35rem;
  border-radius: 3px;
}

/* Table view */
.table-wrap {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1.5rem;
  overflow-x: auto;
  display: none;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.data-table th {
  background: var(--panel-bg);
  padding: 0.5rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.78rem;
  border: 1px solid var(--border);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.data-table th:hover { background: #e8e7e3; }
.data-table th .sort-arrow { margin-left: 0.3rem; font-size: 0.7rem; }

.data-table td {
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  vertical-align: top;
}

.data-table td.col-delta_regime { white-space: nowrap; }

.data-table tbody tr:nth-child(even) { background: #f5f5f2; }
.data-table tbody tr:hover { background: #eee; }
.data-table .kind-lower td { background: #f0e8ff; }
.data-table .kind-lower:hover td { background: #e6dcf5; }
.data-table .kind-open td { background: var(--red-bg); }
.data-table .kind-open:hover td { background: #f8cdd0; }

.legend {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0.75rem 1.5rem;
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
  font-size: 0.78rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

.legend-item { display: flex; align-items: center; gap: 0.35rem; }

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  border: 1px solid var(--border);
}

footer {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1.5rem;
  font-size: 0.78rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.footer-credit { flex: 1 1 auto; min-width: 0; }
.footer-meta { flex: 0 0 auto; white-space: nowrap; }

footer a { color: #1a5276; text-decoration: none; }
footer a:hover { text-decoration: underline; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

.modal-backdrop[hidden] { display: none; }

.modal-panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
  max-width: 540px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 1.5rem 1.75rem;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 0.6rem;
  right: 0.8rem;
  background: transparent;
  border: none;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
}

.modal-close:hover { background: rgba(0, 0, 0, 0.08); color: var(--text); }

.modal-body h3 {
  font-size: 1rem;
  margin-bottom: 0.75rem;
  padding-right: 1.5rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

.setup-summary {
  font-size: 0.85rem;
  color: var(--muted);
  line-height: 1.45;
  margin: -0.4rem 0 0.85rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px dashed var(--border);
}

.setup-summary strong {
  color: var(--text);
  font-weight: 600;
  margin-right: 0.3rem;
}

.modal-filters {
  background: rgba(0, 0, 0, 0.025);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.55rem 0.75rem;
  margin: 0 0 0.85rem;
  font-size: 0.78rem;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.modal-filters-heading {
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.66rem;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.modal-filters-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.9rem;
  align-items: center;
  font-size: 0.8rem;
  line-height: 1.4;
}

.modal-filters-row label {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  cursor: pointer;
}

@media (max-width: 900px) {
  .grid-wrap { flex-direction: column; }
  .detail-panel { width: 100%; min-width: 0; position: static; }
}
