/* QAnsel — professional quantum circuit workbench UI
   Theme-aware (light/dark), responsive down to mobile. */

:root {
  --bg:            #f4f6fb;
  --panel:         #ffffff;
  --panel-2:       #fbfcfe;
  --ink:           #1a2130;
  --ink-soft:      #566079;
  --ink-faint:     #8a93a8;
  --line:          #e2e7f0;
  --line-strong:   #cdd5e4;
  --accent:        #4f46e5;
  --accent-soft:   #eef0ff;
  --accent-ink:    #ffffff;
  --ok:            #16a34a;
  --warn:          #d97706;
  --err:           #dc2626;
  --wire:          #9aa4bd;
  --gate-1q:       #4f46e5;
  --gate-2q:       #0891b2;
  --gate-3q:       #7c3aed;
  --gate-meas:     #db2777;
  --gate-special:  #64748b;
  --playhead:      #22c55e;   /* distinct from every gate color, so the ring is visible on them */
  --shadow:        0 1px 2px rgba(20,28,50,.06), 0 6px 20px rgba(20,28,50,.06);
  --radius:        12px;
  --mono:          ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans:          system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

:root[data-theme="dark"], html[data-theme="dark"] {
  --bg:            #0d1117;
  --panel:         #161b26;
  --panel-2:       #1b2130;
  --ink:           #e6ebf5;
  --ink-soft:      #a9b4cc;
  --ink-faint:     #6b7690;
  --line:          #262d3d;
  --line-strong:   #333c50;
  --accent:        #7c83ff;
  --accent-soft:   #21263c;
  --accent-ink:    #0d1117;
  --wire:          #4a5470;
  --shadow:        0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:#0d1117; --panel:#161b26; --panel-2:#1b2130; --ink:#e6ebf5;
    --ink-soft:#a9b4cc; --ink-faint:#6b7690; --line:#262d3d; --line-strong:#333c50;
    --accent:#7c83ff; --accent-soft:#21263c; --accent-ink:#0d1117; --wire:#4a5470;
    --shadow:0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------------- Top bar ---------------- */
.appbar {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo {
  width: 30px; height: 30px; border-radius: 8px;
  object-fit: cover; display: block;
  box-shadow: var(--shadow);
}
.brand h1 { font-size: 16px; margin: 0; letter-spacing: .2px; }
.brand .sub { font-size: 11px; color: var(--ink-faint); margin-top: -2px; }

.toolbar { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: wrap; }

.seg {
  display: inline-flex; background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 9px; padding: 2px;
}
.seg button {
  border: 0; background: transparent; color: var(--ink-soft);
  padding: 6px 12px; border-radius: 7px; cursor: pointer; font-weight: 600; font-size: 13px;
}
.seg button.active { background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow); }

.field { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-soft); }
.field input[type="number"] {
  width: 74px; padding: 6px 8px; border: 1px solid var(--line-strong);
  border-radius: 8px; background: var(--panel-2); color: var(--ink); font-family: var(--mono);
}
.field.noise input[type="range"] { width: 96px; accent-color: var(--accent); }

button.btn {
  border: 1px solid var(--line-strong); background: var(--panel-2); color: var(--ink);
  padding: 8px 12px; border-radius: 9px; cursor: pointer; font-weight: 600; font-size: 13px;
  display: inline-flex; align-items: center; gap: 6px; transition: .12s;
}
button.btn:hover { border-color: var(--accent); color: var(--accent); }
button.btn:disabled { opacity: .45; cursor: default; }
button.btn:disabled:hover { border-color: var(--line-strong); color: var(--ink); }
button.btn.primary {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
  box-shadow: var(--shadow);
}
button.btn.primary:hover { filter: brightness(1.06); color: var(--accent-ink); }
button.btn.icon { padding: 8px 10px; }
button.btn:disabled { opacity: .5; cursor: not-allowed; }
button.btn svg { width: 16px; height: 16px; display: block; flex: none; }
button.btn.icon svg { width: 17px; height: 17px; }

.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ink-faint); box-shadow: 0 0 0 3px var(--line); }
.status-dot.ok  { background: var(--ok);  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.status-dot.err { background: var(--err); box-shadow: 0 0 0 3px color-mix(in srgb, var(--err) 25%, transparent); }

/* ---------------- Workspace ---------------- */
.workspace {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: 190px minmax(0,1fr) 400px;
  gap: 12px; padding: 12px;
}
.panel {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}
.panel > .phead {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-bottom: 1px solid var(--line);
  font-weight: 700; font-size: 12px; letter-spacing: .4px; text-transform: uppercase; color: var(--ink-soft);
}
.phead .spacer { margin-left: auto; }
.phead .mini {
  font-size: 11px; text-transform: none; letter-spacing: 0; font-weight: 600;
  color: var(--ink-faint); border: 1px solid var(--line); border-radius: 6px; padding: 2px 6px;
  background: var(--panel-2); cursor: pointer;
}
.phead .mini:hover { color: var(--accent); border-color: var(--accent); }

/* ---------------- Palette ---------------- */
.palette { overflow-y: auto; }
.palette .pbody { padding: 10px; display: flex; flex-direction: column; gap: 12px; }
.gate-group h4 { margin: 0 0 6px; font-size: 10px; text-transform: uppercase; letter-spacing: .5px; color: var(--ink-faint); }
.gate-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.gchip {
  user-select: none; cursor: grab; text-align: center; touch-action: none;
  border: 1px solid var(--line-strong); border-radius: 9px; padding: 9px 4px;
  font-family: var(--mono); font-weight: 700; font-size: 13px; background: var(--panel-2);
  color: var(--ink); transition: .1s; position: relative;
}
.gchip:active { cursor: grabbing; }

/* Cursor-following drag ghost (mouse + touch) */
.drag-ghost {
  position: fixed; z-index: 2000; pointer-events: none;
  transform: translate(-50%, -50%);
  background: var(--accent); color: var(--accent-ink);
  font-family: var(--mono); font-weight: 800; font-size: 14px;
  padding: 7px 12px; border-radius: 10px; box-shadow: var(--shadow);
  opacity: .93; white-space: nowrap;
}
.gchip small { display:block; font-family: var(--sans); font-weight:600; font-size:9px; color: var(--ink-faint); margin-top: 2px; }
.gchip:hover { border-color: var(--accent); transform: translateY(-1px); box-shadow: var(--shadow); }
.gchip.armed { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.gchip[data-arity="2"] { border-left: 3px solid var(--gate-2q); }
.gchip[data-arity="3"] { border-left: 3px solid var(--gate-3q); }
.gchip[data-kind="special"] { border-left: 3px solid var(--gate-special); }

.palette .hint {
  font-size: 11px; color: var(--ink-faint); line-height: 1.5; padding: 8px 10px;
  border: 1px dashed var(--line-strong); border-radius: 9px; background: var(--panel-2);
}

/* ---------------- Center: circuit + code ---------------- */
.center { display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.circuit-panel { flex: 1 1 55%; min-height: 220px; }
.circuit-scroll { overflow: auto; padding: 8px 4px; flex: 1; }
#circuit { display: block; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.hitcell { -webkit-tap-highlight-color: transparent; }
.circuit-controls { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.circuit-controls .grp { display: inline-flex; align-items: center; gap: 4px; }
.pill { font-size: 11px; color: var(--ink-faint); }

.code-panel { flex: 1 1 45%; min-height: 160px; }
.editor-wrap { position: relative; flex: 1; overflow: hidden; }
.editor-wrap pre, .editor-wrap textarea {
  margin: 0; padding: 12px 14px; border: 0;
  font-family: var(--mono); font-size: 13px; line-height: 1.5;
  tab-size: 2; white-space: pre; word-wrap: normal;
  position: absolute; inset: 0; overflow: auto;
}
.editor-wrap pre { pointer-events: none; color: var(--ink); background: transparent; }
.editor-wrap textarea {
  color: transparent; background: transparent; caret-color: var(--accent);
  resize: none; outline: none;
}
.editor-wrap textarea::selection { background: color-mix(in srgb, var(--accent) 30%, transparent); }
.tok-kw   { color: #c026d3; font-weight: 700; }
.tok-gate { color: var(--accent); font-weight: 700; }
.tok-num  { color: var(--warn); }
.tok-str  { color: var(--ok); }
.tok-com  { color: var(--ink-faint); font-style: italic; }
.tok-reg  { color: #0891b2; }
html[data-theme="dark"] .tok-kw { color:#e879f9; }

.code-actions { display:flex; gap:8px; padding: 8px 12px; border-top: 1px solid var(--line); align-items:center; }
.code-actions .msg { font-size: 11px; color: var(--ink-faint); margin-left:auto; font-family: var(--mono); }
.code-actions .msg.err { color: var(--err); }
.code-actions .msg.ok { color: var(--ok); }

/* ---------------- Results ---------------- */
.results { min-height: 0; }
.results .rbody { overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 14px; }
.run-meta { display:flex; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--ink-soft); }
.run-meta .chip { background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; font-family: var(--mono); }
.run-meta .chip b { color: var(--ink); }

.nowplaying { font-family: var(--mono); font-size: 12px; color: var(--accent); font-weight: 700;
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.nowplaying .pcbadge { background: var(--accent-soft); border-radius: 6px; padding: 2px 8px; }
.nowplaying .dim { color: var(--ink-faint); font-weight: 600; }

.progressbar { height: 6px; background: var(--line); border-radius: 999px; overflow: hidden; }
.progressbar > div { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), #06b6d4); transition: width .1s; }

/* Wide register values (a 24–30-bit binary column) scroll inside the table's own
   container rather than pushing the panel — and the page — into horizontal scroll. */
.regtable-wrap { overflow-x: auto; }
.regtable { width: 100%; border-collapse: collapse; font-size: 12px; }
.regtable td.bin { white-space: nowrap; }
.regtable th { text-align: left; color: var(--ink-faint); font-weight: 600; font-size: 10px; text-transform: uppercase; letter-spacing:.4px; padding: 4px 8px; border-bottom: 1px solid var(--line); }
.regtable td { padding: 6px 8px; border-bottom: 1px solid var(--line); font-family: var(--mono); }
.regtable td.name { font-family: var(--sans); font-weight: 700; }
.regtable td.name .badge { font-size: 9px; font-weight: 700; padding: 1px 5px; border-radius: 5px; margin-left: 6px; }
.badge.q { background: color-mix(in srgb, var(--gate-1q) 18%, transparent); color: var(--gate-1q); }
.badge.c { background: color-mix(in srgb, var(--gate-2q) 18%, transparent); color: var(--gate-2q); }
.regtable td.bin { letter-spacing: 1px; }
.regtable td.bin .b1 { color: var(--accent); font-weight: 700; }
.flash { animation: flash .5s ease; }
@keyframes flash { from { background: var(--accent-soft); } to { background: transparent; } }

.sectitle { font-size: 11px; text-transform: uppercase; letter-spacing:.4px; color: var(--ink-faint); font-weight: 700; margin: 2px 0; }
.hist-wrap { overflow-x: auto; }
#hist { display: block; width: 100%; }
.empty { color: var(--ink-faint); font-size: 12px; text-align: center; padding: 24px 8px; }

.log { font-family: var(--mono); font-size: 11px; color: var(--ink-soft); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px; max-height: 120px; overflow: auto; white-space: pre-wrap; }

/* ---------------- Modal ---------------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,20,35,.5); backdrop-filter: blur(2px);
  display: none; place-items: center; z-index: 50;
}
.modal-backdrop.open { display: grid; }
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow); width: min(440px, 92vw);
  /* Cap the height to the viewport and let the body scroll, so a tall dialog in
     a short (landscape-phone) window stays fully reachable instead of spilling
     off-screen with no way to scroll to the cut-off controls. */
  max-height: 92vh; display: flex; flex-direction: column; overflow: hidden;
}
.modal h3 { margin: 0; padding: 14px 16px; border-bottom: 1px solid var(--line); font-size: 15px; flex: none; }
.modal .mbody { padding: 16px; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; min-height: 0; }
.modal .row { display: flex; flex-direction: column; gap: 5px; }
.modal .row label { font-size: 12px; color: var(--ink-soft); font-weight: 600; }
.modal .row input, .modal .row select {
  padding: 9px 10px; border: 1px solid var(--line-strong); border-radius: 9px;
  background: var(--panel-2); color: var(--ink); font-family: var(--mono); font-size: 13px;
}
.modal .mfoot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 16px; border-top: 1px solid var(--line); flex: none; }

/* ---------------- Floating gate picker / context menu ---------------- */
.floatmenu {
  position: fixed; z-index: 60; display: none;
  background: var(--panel); border: 1px solid var(--line-strong);
  border-radius: 12px; box-shadow: var(--shadow), 0 8px 30px rgba(0,0,0,.28);
  padding: 8px; max-width: min(320px, 92vw); max-height: 70vh; overflow: auto;
}
.floatmenu.open { display: block; animation: pop .12s ease; }
@keyframes pop { from { transform: scale(.96); opacity: .4; } to { transform: scale(1); opacity: 1; } }
.floatmenu .fm-title { font-size: 10px; text-transform: uppercase; letter-spacing:.5px;
  color: var(--ink-faint); font-weight: 700; margin: 6px 4px 4px; }
#gate-picker .gate-grid { grid-template-columns: repeat(5, 44px); }
#gate-picker .gate-group { margin-bottom: 6px; }
.fm-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 12px; border: 0; background: transparent; color: var(--ink);
  font: inherit; font-size: 14px; text-align: left; cursor: pointer; border-radius: 8px;
}
.fm-item:hover { background: var(--accent-soft); color: var(--accent); }
.fm-item.danger:hover { background: color-mix(in srgb, var(--err) 16%, transparent); color: var(--err); }
.fm-item .ic { width: 20px; text-align: center; font-size: 15px; }

/* ---------------- Mobile / touch layout ----------------
   Driven by a `.mobile` class that app.js puts on <body> when the device is
   touch-primary (feature-detected, à la the old QAnsel) OR the window is narrow.
   Feature detection — not a width breakpoint — means a phone in landscape (which
   can be quite wide) still gets the touch UI. */

/* The fixed Gates panel becomes a floating picker (tap a cell). */
body.mobile .palette { display: none; }
body.mobile #gate-picker .gate-grid { grid-template-columns: repeat(4, 56px); }
body.mobile .fm-item { padding: 12px 12px; }

/* Stack the panels and let the whole page scroll, each at a usable height. */
body.mobile { overflow: auto; }
body.mobile .workspace {
  flex: none; height: auto; display: flex; flex-direction: column; overflow: visible;
}
body.mobile .workspace > .panel,
body.mobile .center, body.mobile .center > .panel { min-width: 0; }
/* Promote the circuit + code panels to be flex items of the workspace so the
   results panel can be ordered between them: circuit → results → code. */
body.mobile .center { display: contents; }
body.mobile .circuit-panel { order: 1; flex: none; height: 58vh; min-height: 300px; }
body.mobile .results       { order: 2; overflow: visible; }
body.mobile .code-panel    { order: 3; flex: none; height: 40vh; min-height: 200px; }
body.mobile .results .rbody { overflow: visible; }

/* On mobile put the qubit/column steppers on a row ABOVE the tracks (thumb
   reach); the circuit panel is a flex column, so reorder its children. */
body.mobile .circuit-panel .circuit-controls { order: 1; border-top: 0; border-bottom: 1px solid var(--line); }
body.mobile .circuit-panel .circuit-scroll   { order: 2; }

/* Compact top bar: only the button row sticks; the QAnsel title scrolls away to
   save vertical space. display:contents promotes .brand + .toolbar to be flex
   items of <body>, so the toolbar can stay pinned past the title. */
body.mobile .brand .sub { display: none; }
body.mobile .appbar { display: contents; }
/* Title row stays left-aligned (only the button row below is centered). */
body.mobile .brand { justify-content: flex-start; padding: 8px 12px 4px; }
body.mobile .toolbar {
  position: sticky; top: 0; z-index: 30;
  width: 100%; margin: 0; gap: 6px; justify-content: center;
  padding: 8px 10px; background: var(--panel); border-bottom: 1px solid var(--line);
}
body.mobile .toolbar .btn { padding: 7px 9px; }

/* Narrow (portrait) refinement. */
@media (max-width: 640px) {
  body.mobile .gate-grid { grid-template-columns: repeat(4, 1fr); }
  body.mobile .circuit-panel { height: 52vh; }
}
/* Landscape phones are very short — give the circuit most of the height. */
@media (orientation: landscape) and (max-height: 560px) {
  body.mobile .circuit-panel { height: 78vh; min-height: 240px; }
  body.mobile .code-panel { height: 55vh; min-height: 150px; }
}

/* ---------------- Print ---------------- */
@media print {
  body { overflow: visible; background: #fff; }
  .appbar, .palette, .code-panel, .circuit-controls, .toolbar, .no-print { display: none !important; }
  .workspace { display: block; padding: 0; }
  .panel { box-shadow: none; border: none; }
  .circuit-scroll { overflow: visible; }
  #circuit { width: 100%; }
}
