/* ============================================================
   Happy Ending — SISO Demonstration  Phase 7
   Precision instrument aesthetic. Dark + ice + gold.
   IBM Plex Mono (technical authority) + DM Sans (legible)
   ============================================================ */

:root {
  --bg:          #080d14;
  --bg-panel:    #0b1018;
  --bg-card:     #0f1620;
  --bg-hi:       #162030;
  --bg-glass:    rgba(15,22,32,0.85);
  --border:      #1e3448;
  --border-hi:   #2a4a65;
  --text:        #c2d4e8;
  --text-dim:    #7490aa;  /* lifted from #5a7a94 — 4.8:1 on bg-card */
  --text-muted:  #6a8298;  /* lifted from #364e62 — 4.5:1 on bg-card */
  --cyan:        #18d6e0;
  --cyan-dim:    rgba(24,214,224,0.12);
  --orange:      #e07818;
  --green:       #2ecc8a;
  --gold:        #f5c842;
  --gold-dim:    rgba(245,200,66,0.10);
  --red:         #e04848;
  --red-dim:     rgba(224,72,72,0.10);
  --violet:      #9b7de8;
  --happy:       #18d6e0;
  --collatz:     #f5c842;
  --goldbach:    #e04848;
  --radius:      4px;
  --radius-lg:   7px;
  --t:           0.14s ease;
}

*, *::before, *::after { margin:0; padding:0; box-sizing:border-box; }
html { font-size:13px; }

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ── */
.layout {
  display: grid;
  grid-template-columns: 1fr 1fr 360px;
  grid-template-rows: 100vh;
  height: 100vh;
}

/* ── Canvas halves ── */
.canvas-half {
  position: relative;
  background: var(--bg);
  overflow: hidden;
  height: 100%;
  min-height: 0;
}

.canvas-half + .canvas-half { border-left: 1px solid var(--border); }

.zone-canvas, .main-canvas {
  position: absolute; top:0; left:0;
  width:100%; height:100%; display:block;
}
.zone-canvas { pointer-events:none; z-index:1; }
.main-canvas { z-index:2; cursor:crosshair; }

/* Arrangement overlay */
.arr-overlay {
  position: absolute; top:0.9rem; left:0.9rem;
  pointer-events:none; user-select:none; z-index:20;
  background: var(--bg-glass);
  padding: 0.4rem 0.6rem;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.arr-badge {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 3.5px;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
  display: block;
}

.step-count-arr {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--cyan);
  line-height: 1;
  letter-spacing: -1px;
}

.mode-display-arr {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.65rem;
  color: var(--text-dim);
  margin-top: 0.18rem;
}

/* Zone legend */
.zone-legend {
  position: absolute;
  bottom: 0.7rem; left: 0.8rem;
  pointer-events: none; z-index: 20;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.6rem;
  color: var(--text-muted);
  display: flex; gap: 0.8rem;
  background: var(--bg-glass);
  padding: 0.22rem 0.5rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  backdrop-filter: blur(6px);
}

.zone-legend .dot {
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 2px;
  margin-right: 3px;
  vertical-align: middle;
}
.dot-safe      { background: rgba(46,204,138,0.6); }
.dot-forbidden { background: rgba(224,72,72,0.6); }

/* Detection banner */
#detection-banner {
  display: none;
  position: absolute;
  bottom: 3rem; left: 50%;
  transform: translateX(-50%);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 3px;
  text-transform: uppercase;
  background: rgba(8,13,20,0.92);
  border: 1px solid currentColor;
  padding: 0.4rem 1.1rem;
  border-radius: var(--radius);
  pointer-events: none;
  z-index: 20;
  backdrop-filter: blur(8px);
}

/* ── Sidebar ── */
.sidebar {
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  overflow-y: auto;
}

.sidebar::-webkit-scrollbar { width: 3px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border-hi); border-radius: 2px; }
.sidebar::-webkit-scrollbar-thumb:hover { background: var(--cyan); }

/* Sticky header */
.sidebar-header {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg-panel);
  padding: 0.9rem 0.85rem 0.65rem;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 3px;
  text-transform: uppercase;
}

.sidebar-header .subtitle {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.58rem;
  color: var(--text-muted);
  letter-spacing: 1.5px;
  text-transform: uppercase;
  margin-top: 0.1rem;
}

.sidebar-body { padding: 0.7rem 0.85rem; }

/* ── Panels ── */
.panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 0.45rem;
  overflow: hidden;
  transition: border-color var(--t);
}

.panel-header {
  padding: 0.35rem 0.6rem;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.58rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  background: rgba(0,0,0,0.15);
}

.ph-cyan    { color: var(--cyan);   }
.ph-gold    { color: var(--gold);   }
.ph-green   { color: var(--green);  }
.ph-violet  { color: var(--violet); }
.ph-red     { color: var(--red);    }

.panel-body { padding: 0.5rem 0.6rem; }

/* ── Framework Panel ── */
#framework-panel {
  border-color: rgba(245,200,66,0.22);
}

.fw-table {
  width: 100%;
  border-collapse: collapse;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.62rem;
}

.fw-table th {
  text-align: left;
  padding: 0.25rem 0.35rem 0.25rem 0;
  font-weight: 700;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  border-bottom: 1px solid var(--border);
  font-size: 0.58rem;
}

.fw-table th.col-criterion { color: var(--text-muted); }
.fw-table th.col-happy     { color: var(--happy);    }
.fw-table th.col-collatz   { color: var(--collatz);  }
.fw-table th.col-goldbach  { color: var(--goldbach); }

.fw-table td {
  padding: 0.26rem 0.35rem 0.26rem 0;
  border-bottom: 1px solid rgba(30,52,72,0.45);
  line-height: 1.45;
  vertical-align: top;
}

.fw-table td.criterion {
  color: var(--text-muted);
  font-size: 0.58rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  white-space: nowrap;
  padding-right: 0.5rem;
}

.fw-table tr:last-child td { border-bottom: none; }
.fw-table .tick  { color: var(--green); font-weight: 700; }
.fw-table .cross { color: var(--red);   font-weight: 700; }

.fw-table .closes-yes { color: var(--green);    font-weight: 700; letter-spacing: 1px; }
.fw-table .closes-no  { color: var(--goldbach); font-weight: 700; letter-spacing: 1px; }

.goldbach-note {
  margin-top: 0.45rem;
  padding: 0.4rem 0.5rem;
  background: var(--red-dim);
  border-left: 2px solid var(--goldbach);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.65rem;
  color: var(--text-dim);
  line-height: 1.65;
}

.goldbach-note strong {
  color: var(--goldbach);
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 700;
}

/* ── Summary Panel ── */
#summary-panel {
  display: none;
  border-color: rgba(245,200,66,0.25);
}

.sum-row {
  display: grid;
  grid-template-columns: auto 1fr 1fr;
  gap: 0 0.6rem;
  align-items: center;
  padding: 0.2rem 0;
  border-bottom: 1px solid rgba(30,52,72,0.45);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.65rem;
}
.sum-row:last-child { border-bottom: none; }
.sum-row .lbl  { color: var(--text-muted); font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.8px; }
.sum-row .va   { color: var(--cyan);   font-weight: 700; }
.sum-row .vb   { color: var(--orange); font-weight: 700; }

#sum-gate-same {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.68rem;
  font-weight: 700;
  padding: 0.4rem 0.5rem;
  background: var(--gold-dim);
  border-left: 2px solid var(--gold);
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--gold);
  margin: 0.45rem 0;
  line-height: 1.5;
}

.final-statement {
  font-family: 'DM Sans', sans-serif;
  font-size: 0.68rem;
  color: var(--text-dim);
  line-height: 1.75;
  padding-top: 0.4rem;
  border-top: 1px solid var(--border);
}

.final-statement em {
  font-style: normal;
  color: var(--gold);
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 700;
}

/* ── Gate code ── */
.gate-code {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.65rem;
  line-height: 1.7;
  white-space: pre;
  color: var(--text);
  background: var(--bg);
  border-radius: var(--radius);
  padding: 0.5rem 0.55rem;
  border: 1px solid var(--border);
  overflow-x: auto;
}
.gate-code .c   { color: var(--text-muted); }
.gate-code .h   { color: var(--gold);  font-weight: 600; }
.gate-code .k   { color: var(--cyan);  font-weight: 500; }
.gate-code .sep { display:block; height:0; border-top:1px solid var(--border); margin:0.3rem 0; }

/* ── Buttons ── */
.controls { display: flex; flex-wrap: wrap; gap: 0.25rem; }

button {
  padding: 0.28rem 0.55rem;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.65rem;
  font-weight: 600;
  background: var(--bg-card);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color var(--t), color var(--t), background var(--t);
  white-space: nowrap;
  letter-spacing: 0.2px;
}
button:hover { border-color: var(--cyan); color: var(--cyan); background: var(--cyan-dim); }
button:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }
button.active { background: var(--cyan); color: var(--bg); border-color: var(--cyan); }

button.arr-btn[data-arr="a"]        { border-color:rgba(24,214,224,0.35); color:rgba(24,214,224,0.65); }
button.arr-btn[data-arr="b"]        { border-color:rgba(224,120,24,0.35); color:rgba(224,120,24,0.65); }
button.arr-btn[data-arr="a"].active { background:var(--cyan);   color:var(--bg); border-color:var(--cyan); }
button.arr-btn[data-arr="b"].active { background:var(--orange); color:var(--bg); border-color:var(--orange); }

button.batch-btn  { border-color:rgba(155,125,232,0.35); color:var(--violet); }
button.batch-btn:hover  { background:rgba(155,125,232,0.12); border-color:var(--violet); }
button.batch-btn.active { background:var(--red); border-color:var(--red); color:var(--bg); }

button.replay-btn { border-color:rgba(155,125,232,0.35); color:var(--violet); }
button.replay-btn:hover  { background:rgba(155,125,232,0.12); border-color:var(--violet); }
button.replay-btn.active { background:var(--red); border-color:var(--red); color:var(--bg); }

.speed-row {
  display:flex; align-items:center; gap:0.45rem;
  font-family:'IBM Plex Mono',monospace;
  font-size:0.65rem; color:var(--text-muted); white-space:nowrap;
}
.speed-row input[type="range"] { flex:1; accent-color:var(--cyan); min-width:0; }

select {
  background:var(--bg); border:1px solid var(--border); color:var(--text-dim);
  font-family:'IBM Plex Mono',monospace; font-size:0.65rem;
  padding:0.25rem 0.4rem; border-radius:var(--radius); cursor:pointer;
  transition:border-color var(--t);
}
select:focus, select:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; border-color: var(--cyan); color: var(--text); }

/* ── Comparison table ── */
.comp-table { width:100%; font-size:0.62rem; border-collapse:collapse; font-family:'IBM Plex Mono',monospace; }
.comp-table th {
  color:var(--text-muted); font-weight:600; text-align:left;
  padding:0.2rem 0.3rem 0.2rem 0; border-bottom:1px solid var(--border);
  font-size:0.58rem; letter-spacing:0.5px; text-transform:uppercase;
}
.comp-table th:nth-child(2) { color:var(--cyan); }
.comp-table th:nth-child(3) { color:var(--orange); }
.comp-table td { padding:0.2rem 0.3rem 0.2rem 0; border-bottom:1px solid rgba(30,52,72,0.45); font-weight:700; }
.comp-table td:first-child { color:var(--text-muted); font-weight:400; font-size:0.6rem; }
.comp-table tr:last-child td { border-bottom:none; }

/* ── Batch ── */
.batch-grid { display:grid; grid-template-columns:1fr 1fr; gap:0.25rem 0.45rem; margin-bottom:0.4rem; }
.batch-field { display:flex; flex-direction:column; gap:0.12rem; }
.batch-field label { font-family:'IBM Plex Mono',monospace; font-size:0.58rem; font-weight:700; color:var(--text-muted); text-transform:uppercase; letter-spacing:1px; }
.batch-field input[type="number"] {
  background:var(--bg); border:1px solid var(--border); color:var(--text);
  font-family:'IBM Plex Mono',monospace; font-size:0.68rem;
  padding:0.23rem 0.38rem; border-radius:var(--radius); width:100%;
  transition:border-color var(--t);
}
.batch-field input[type="number"]:focus,
.batch-field input[type="number"]:focus-visible { outline: 2px solid var(--violet); outline-offset: 2px; border-color: var(--violet); }

.progress-wrap { height:3px; background:var(--bg); border-radius:2px; overflow:hidden; margin:0.4rem 0 0.15rem; }
.progress-bar  { height:100%; width:0%; background:linear-gradient(90deg,var(--violet),var(--cyan)); border-radius:2px; transition:width 0.25s ease; }
.progress-lbl  { font-family:'IBM Plex Mono',monospace; font-size:0.58rem; color:var(--text-muted); }

/* ── Replay ── */
.replay-status { font-family:'IBM Plex Mono',monospace; font-size:0.62rem; color:var(--text-muted); margin-top:0.28rem; }
.replay-count  { font-family:'IBM Plex Mono',monospace; font-size:0.6rem;  color:var(--text-muted); margin-top:0.1rem; }
input[type="file"] { display:none; }

/* ── Charts ── */
.chart-wrap { position:relative; width:100%; }
.chart-wrap.main-chart { height:88px; }
.chart-wrap.rate-chart { height:50px; margin-top:0.3rem; }
canvas#chart-canvas, canvas#rate-canvas { display:block; width:100%; height:100%; }

/* ── Gate log ── */
.gate-log {
  max-height:85px; overflow-y:auto; padding:0.22rem 0.38rem;
  background:var(--bg); border-radius:var(--radius); border:1px solid var(--border);
}
.gate-log::-webkit-scrollbar { width:2px; }
.gate-log::-webkit-scrollbar-thumb { background:var(--border-hi); }

.log-row { padding:0.07rem 0; font-family:'IBM Plex Mono',monospace; font-size:0.6rem; line-height:1.45; }
.log-row.left     { color:rgba(24,214,224,0.7); }
.log-row.right    { color:rgba(224,120,24,0.7); }
.log-row.rejected { color:rgba(224,72,72,0.7);  }

/* ── Note ── */
.note { font-family:'DM Sans',sans-serif; font-size:0.65rem; color:var(--text-muted); line-height:1.7; padding:0.2rem 0; }

/* ── Hidden panels ── */
#threshold-panel { display:none; border-color:rgba(46,204,138,0.22); }
#per-point-panel { display:none; border-color:rgba(24,214,224,0.18); }

/* ── Welcome Modal ── */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(8,13,20,0.82);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.modal-backdrop.hidden { display: none; }

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border-hi);
  border-radius: 10px;
  width: 100%; max-width: 520px;
  max-height: 88vh;
  display: flex; flex-direction: column;
  position: relative;
  box-shadow: 0 24px 64px rgba(0,0,0,0.7), 0 0 0 1px rgba(24,214,224,0.07);
}

.modal-topbar {
  display: flex; align-items: flex-start; justify-content: space-between;
  padding: 1.2rem 1.2rem 0.6rem;
  flex-shrink: 0;
}

.modal-title-block {}
.modal-title-block h2 {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 1.05rem; font-weight: 700;
  color: var(--text); letter-spacing: 2px; text-transform: uppercase;
}
.modal-title-block .modal-tagline {
  font-family: 'DM Sans', sans-serif;
  font-size: 0.72rem; color: var(--text-dim);
  margin-top: 0.2rem; line-height: 1.5;
}

.modal-close {
  background: none; border: 1px solid var(--border);
  color: var(--text-dim); font-size: 0.85rem; font-weight: 700;
  width: 28px; height: 28px; border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0; margin-left: 1rem;
  transition: border-color var(--t), color var(--t), background var(--t);
  padding: 0;
  font-family: 'IBM Plex Mono', monospace;
}
.modal-close:hover  { border-color: var(--red); color: var(--red); background: var(--red-dim); }
.modal-close:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

.modal-divider {
  height: 1px; background: var(--border); flex-shrink: 0; margin: 0 1.2rem;
}

.modal-body {
  overflow-y: auto; flex: 1;
  padding: 0.9rem 1.2rem;
  scrollbar-width: thin;
  scrollbar-color: var(--border-hi) transparent;
}
.modal-body::-webkit-scrollbar { width: 3px; }
.modal-body::-webkit-scrollbar-thumb { background: var(--border-hi); border-radius: 2px; }

.modal-intro {
  font-family: 'DM Sans', sans-serif;
  font-size: 0.82rem; color: var(--text); line-height: 1.75;
  margin-bottom: 0.85rem;
}

.modal-intro strong {
  color: var(--gold);
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 700;
}

.modal-detail {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 0.5rem;
  overflow: hidden;
  transition: border-color var(--t);
}
.modal-detail[open] { border-color: var(--border-hi); }

.modal-detail summary {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.67rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 1.5px;
  color: var(--text-dim);
  padding: 0.5rem 0.7rem;
  cursor: pointer;
  list-style: none;
  display: flex; align-items: center; gap: 0.5rem;
  background: rgba(0,0,0,0.15);
  user-select: none;
  transition: color var(--t), background var(--t);
}
.modal-detail summary::-webkit-details-marker { display: none; }
.modal-detail summary::before {
  content: '▶';
  font-size: 0.5rem; color: var(--text-muted);
  transition: transform 0.15s ease, color var(--t);
  flex-shrink: 0;
}
.modal-detail[open] summary::before { transform: rotate(90deg); color: var(--cyan); }
.modal-detail summary:hover { color: var(--text); background: rgba(24,214,224,0.04); }
.modal-detail summary:focus-visible { outline: 2px solid var(--cyan); outline-offset: -2px; }

.modal-detail .detail-body {
  padding: 0.6rem 0.75rem 0.7rem;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.76rem; color: var(--text-dim); line-height: 1.75;
  border-top: 1px solid var(--border);
}
.modal-detail .detail-body p { margin-bottom: 0.5rem; }
.modal-detail .detail-body p:last-child { margin-bottom: 0; }
.modal-detail .detail-body strong { color: var(--text); }
.modal-detail .detail-body code {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem; color: var(--cyan);
  background: var(--bg); padding: 0.05em 0.3em;
  border-radius: 3px; border: 1px solid var(--border);
}
.modal-detail .detail-body .formula {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem; color: var(--gold);
  background: var(--gold-dim); border: 1px solid rgba(245,200,66,0.18);
  border-radius: var(--radius); padding: 0.4rem 0.6rem;
  margin: 0.5rem 0; display: block; line-height: 1.6;
}

.modal-footer {
  padding: 0.8rem 1.2rem;
  border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end;
  flex-shrink: 0;
}

.modal-start {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem; font-weight: 700; letter-spacing: 1px;
  padding: 0.5rem 1.4rem;
  background: var(--cyan); color: var(--bg);
  border: none; border-radius: var(--radius); cursor: pointer;
  transition: background var(--t), transform var(--t);
}
.modal-start:hover { background: #38e8f0; transform: translateY(-1px); }
.modal-start:focus-visible { outline: 3px solid var(--cyan); outline-offset: 3px; }

/* ── Label-as-button for file import ── */
label.file-btn {
  display: inline-flex; align-items: center;
  padding: 0.28rem 0.55rem;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.65rem; font-weight: 600;
  background: var(--bg-card); color: var(--text-dim);
  border: 1px solid var(--border); border-radius: var(--radius);
  cursor: pointer;
  transition: border-color var(--t), color var(--t), background var(--t);
  white-space: nowrap; letter-spacing: 0.2px;
}
label.file-btn:hover { border-color: var(--cyan); color: var(--cyan); background: var(--cyan-dim); }
label.file-btn:focus-within { outline: 2px solid var(--cyan); outline-offset: 2px; }
