/* ======================================================================
   Nasdaq TotalView 3D Order Book - presentation layer

   Every colour is a custom property so the theme toggle can swap the whole
   palette by flipping :root[data-theme]. The WebGL scene cannot read these,
   so assets/theme values are mirrored in the module script.
   ====================================================================== */

:root {
  --bg:        #05070d;
  --bg-rgb:    5, 7, 13;
  --panel:     rgba(10, 16, 28, 0.78);
  --surface-input:  rgba(12, 20, 34, 0.9);
  --surface-ghost:  rgba(255, 255, 255, 0.04);
  --meter-track:    rgba(255, 255, 255, 0.07);
  --tooltip-bg:     rgba(8, 13, 24, 0.94);
  --tooltip-shadow: rgba(0, 0, 0, 0.55);
  --hairline:  rgba(255, 255, 255, 0.55);
  --border:    rgba(90, 130, 190, 0.28);
  --text:      #dbe6f5;
  --muted:     #7a8ba6;
  --bid:       #24e08a;
  --ask:       #ff4d6a;
  --accent:    #39b8ff;
  --warn:      #ffc746;
  --bid-soft:  rgba(36, 224, 138, 0.34);
  --ask-soft:  rgba(255, 77, 106, 0.34);
  --accent-tint:   rgba(57, 184, 255, 0.14);
  --accent-line:   rgba(57, 184, 255, 0.45);
  --accent-glow:   rgba(57, 184, 255, 0.18);
  --warn-tint: rgba(255, 199, 70, 0.12);
  --warn-line: rgba(255, 199, 70, 0.40);
  --ice-tint:  rgba(57, 184, 255, 0.12);
  --ice-line:  rgba(57, 184, 255, 0.45);
  --err-tint:  rgba(255, 77, 106, 0.12);
  --err-line:  rgba(255, 77, 106, 0.45);
  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  /* Single source of truth for the toolbar height, so the legend, hint and
     HUD max-height can all be derived from it instead of guessed at. */
  --toolbar-h: 56px;
  --control-h: 44px;   /* one height for every topbar control (also the a11y target) */
}

:root[data-theme="light"] {
  --bg:        #eef2f7;
  --bg-rgb:    238, 242, 247;
  --panel:     rgba(255, 255, 255, 0.86);
  --surface-input:  rgba(255, 255, 255, 0.95);
  --surface-ghost:  rgba(20, 35, 60, 0.05);
  --meter-track:    rgba(20, 35, 60, 0.10);
  --tooltip-bg:     rgba(255, 255, 255, 0.96);
  --tooltip-shadow: rgba(35, 55, 85, 0.28);
  --hairline:  rgba(22, 32, 46, 0.45);
  --border:    rgba(52, 86, 134, 0.26);
  --text:      #16202e;
  --muted:     #5a6b80;
  --bid:       #0b9d5e;
  --ask:       #d02b45;
  --accent:    #0a6fbe;
  --warn:      #a8710f;
  --bid-soft:  rgba(11, 157, 94, 0.30);
  --ask-soft:  rgba(208, 43, 69, 0.30);
  --accent-tint:   rgba(10, 111, 190, 0.12);
  --accent-line:   rgba(10, 111, 190, 0.42);
  --accent-glow:   rgba(10, 111, 190, 0.20);
  --warn-tint: rgba(168, 113, 15, 0.12);
  --warn-line: rgba(168, 113, 15, 0.40);
  --ice-tint:  rgba(10, 111, 190, 0.10);
  --ice-line:  rgba(10, 111, 190, 0.42);
  --err-tint:  rgba(208, 43, 69, 0.10);
  --err-line:  rgba(208, 43, 69, 0.42);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  width: 100%; height: 100%;
  overflow: hidden; overscroll-behavior: none;
  background: var(--bg); color: var(--text);
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

/* Gesture ownership lives on the canvas, not the document, so page zoom can
   stay enabled (WCAG 1.4.4) without the camera losing pinch. */
#scene {
  position: fixed; top: 0; left: 0;
  width: 100%; height: 100%;
  display: block; touch-action: none;
}

/* -------------------------------------------------------------------- */
/* Top bar                                                              */
/* -------------------------------------------------------------------- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  padding: calc(var(--safe-top) + 8px) 10px 8px;
  display: flex; gap: 8px; align-items: center;
  background: linear-gradient(180deg, rgba(var(--bg-rgb), 0.94) 0%, rgba(var(--bg-rgb), 0) 100%);
  z-index: 20;
  min-height: 58px;
}

#status-group {
  display: flex; align-items: center; justify-content: flex-end; gap: 8px;
  flex-shrink: 0;
}

#symbol-form { display: flex; gap: 6px; flex: 1 1 90px; min-width: 0; align-items: center; }

#symbol-input {
  flex: 1 1 auto; min-width: 0;
  padding: 10px 12px;
  background: var(--surface-input);
  border: 1px solid var(--border); border-radius: 10px;
  color: var(--text); font: inherit; font-size: 16px;   /* 16px = no iOS auto-zoom */
  letter-spacing: 2px; text-transform: uppercase;
  outline: none;
}
#symbol-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-glow); }

button {
  height: var(--control-h);
  padding: 0 14px;                 /* was 10px 14px */
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent-tint);
  border: 1px solid var(--accent-line); border-radius: 10px;
  color: var(--accent); font: inherit; font-weight: 700; font-size: 13px;
  cursor: pointer; white-space: nowrap; user-select: none;
}
button:active { background: var(--accent-glow); transform: scale(0.97); }
button.ghost { background: var(--surface-ghost); border-color: var(--border); color: var(--muted); }
button:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.icon-button {
  width: var(--control-h); height: var(--control-h);
  padding: 0; font-size: 16px; line-height: 1;
  background: var(--surface-ghost); border-color: var(--border); color: var(--text);
  position: relative;
}
/* Visual box stays compact; the hit area is padded out to >= 44px. */
.icon-button::after { content: none; }

/* View selector: native control = free wheel UI on mobile and free a11y. */
#view-select {
  flex: 0 0 auto; max-width: 132px;
  height: var(--control-h); min-height: 0;   /* min-height was the odd one out */
  padding: 0 26px 0 11px;
  font: inherit; font-size: 11px; font-weight: 700; letter-spacing: 0.6px;
  border-radius: 10px; cursor: pointer;
  border: 1px solid var(--accent-line); color: var(--accent);
  background: var(--accent-tint) no-repeat right 9px center / 8px 5px
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 5'%3E%3Cpath d='M0 0h8L4 5z' fill='%2339b8ff'/%3E%3C/svg%3E");
  text-overflow: ellipsis;
}
#view-select option { background: var(--bg); color: var(--text); }

#status {
  display: flex; align-items: center; gap: 6px;
  padding: 0 10px; border-radius: 10px; white-space: nowrap;
  background: var(--panel); border: 1px solid var(--border);
  font-size: 11px; color: var(--muted);
  height: var(--control-h);
}
#status .dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--muted); flex: 0 0 auto;
}
#status.live  .dot { background: var(--bid); box-shadow: 0 0 10px var(--bid); animation: pulse 1.6s infinite; }
#status.live  { color: var(--bid); }
#status.wait  .dot { background: var(--warn); box-shadow: 0 0 10px var(--warn); }
#status.wait  { color: var(--warn); }
#status.down  .dot { background: var(--ask); box-shadow: 0 0 10px var(--ask); }
#status.down  { color: var(--ask); }
#status.frozen .dot { background: var(--accent); box-shadow: 0 0 10px var(--accent); }
#status.frozen { color: var(--accent); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }

/* -------------------------------------------------------------------- */
/* HUD - fluid width, bounded height, collapsible                       */
/* -------------------------------------------------------------------- */
#hud {
  position: fixed; left: 10px; top: calc(var(--safe-top) + 62px);
  width: clamp(158px, 44vw, 216px); max-width: calc(100vw - 20px);
  max-height: calc(100dvh - var(--safe-top) - var(--safe-bottom)
                   - var(--toolbar-h) - 84px);
  padding: 12px 13px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  z-index: 15;
  /* Interactive so it can be scrolled when the metric list overflows; the
     canvas keeps every other gesture because touch-action is limited to Y. */
  pointer-events: auto; overflow-y: auto; touch-action: pan-y;
  scrollbar-width: none;
  transition: max-height 0.2s ease, width 0.2s ease, opacity 0.2s ease;
}
#hud::-webkit-scrollbar { display: none; }

/* Collapsed: header only. Toggled from #hud-toggle in the toolbar. */
#hud.collapsed { max-height: 62px; overflow: hidden; }
#hud.collapsed #depth-block,
#hud.collapsed #liq-block,
#hud.collapsed #impact-block,
#hud.collapsed #pulse-block,
#hud.collapsed #pie-block,
#hud.collapsed #alerts,
#hud.collapsed .row { display: none !important; }

#hud .ticker {
  font-size: 22px; font-weight: 800; letter-spacing: 2px;
  color: var(--text); line-height: 1.1;
}
#hud .mid { font-size: 15px; color: var(--accent); margin-top: 2px; letter-spacing: 1px; }

#hud .row {
  display: flex; justify-content: space-between; align-items: baseline;
  gap: 8px; padding: 3px 0; font-size: 11.5px;
}
#hud .row .k {
  color: var(--muted); min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
#hud .row .v { color: var(--text); font-weight: 600; flex: 0 0 auto; }
#hud .v.bid { color: var(--bid); }
#hud .v.ask { color: var(--ask); }

hr.sep { border: none; border-top: 1px solid var(--border); margin: 9px 0 7px; }

/* Order-book imbalance meter */
#obi-meter {
  position: relative; height: 8px; border-radius: 4px; margin: 7px 0 3px;
  background: linear-gradient(90deg, var(--ask-soft), var(--meter-track), var(--bid-soft));
  overflow: hidden;
}
#obi-fill {
  position: absolute; top: 0; bottom: 0; left: 50%; width: 0%;
  background: var(--bid); transition: left 0.22s ease, width 0.22s ease, background 0.22s ease;
}
#obi-meter::after {
  content: ''; position: absolute; left: 50%; top: -2px; bottom: -2px;
  width: 1px; background: var(--hairline);
}

/* Fill-ratio meter: red (all cancelled) -> green (all executed) */
#fill-meter {
  position: relative; height: 8px; border-radius: 4px; margin: 7px 0 3px;
  background: var(--meter-track); overflow: hidden;
}
#fill-bar {
  position: absolute; inset: 0 auto 0 0; width: 0%;
  background: linear-gradient(90deg, var(--ask), var(--warn), var(--bid));
  transition: width 0.25s ease;
}

#alerts { margin-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.alert {
  padding: 4px 7px; border-radius: 6px; font-size: 10.5px; letter-spacing: 0.5px;
  background: var(--warn-tint); border: 1px solid var(--warn-line);
  color: var(--warn);
}
.alert.ice { background: var(--ice-tint); border-color: var(--ice-line); color: var(--accent); }
.alert.err { background: var(--err-tint); border-color: var(--err-line); color: var(--ask); }

/* -------------------------------------------------------------------- */
/* Legend + perf                                                        */
/* -------------------------------------------------------------------- */
#legend {
  position: fixed; right: 10px;
  bottom: calc(var(--safe-bottom) + var(--toolbar-h) + 16px);
  padding: 9px 11px; border-radius: 12px;
  background: var(--panel); border: 1px solid var(--border);
  font-size: 10px; color: var(--muted); line-height: 1.7;
  z-index: 15; pointer-events: none; text-align: right;
  max-width: 46vw;
}
#legend b { color: var(--text); font-weight: 600; }
#perf { color: var(--accent); }

#hint {
  position: fixed; left: 50%;
  bottom: calc(var(--safe-bottom) + var(--toolbar-h) + 18px);
  transform: translateX(-50%);
  padding: 7px 14px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--border);
  font-size: 10.5px; color: var(--muted); z-index: 15;
  pointer-events: none; opacity: 1; transition: opacity 0.8s ease;
  max-width: calc(100vw - 20px); text-align: center;
}
#hint.hidden { opacity: 0; }

/* -------------------------------------------------------------------- */
/* Bar inspector tooltip                                                */
/* -------------------------------------------------------------------- */
#tooltip {
  position: fixed; left: 0; top: 0;
  padding: 9px 11px; border-radius: 10px;
  background: var(--tooltip-bg);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  font-size: 11px; line-height: 1.55; color: var(--text);
  z-index: 30; pointer-events: none;
  opacity: 0; transition: opacity 0.12s ease;
  white-space: nowrap;
  box-shadow: 0 6px 22px var(--tooltip-shadow);
}
#tooltip.show { opacity: 1; }
#tooltip .tt-head {
  font-weight: 800; letter-spacing: 1px; font-size: 12px;
  margin-bottom: 5px; color: var(--text);
}
#tooltip .tt-row { display: flex; justify-content: space-between; gap: 16px; }
#tooltip .tt-row .k { color: var(--muted); }
#tooltip .tt-verdict {
  margin-top: 6px; padding-top: 5px;
  border-top: 1px solid var(--border);
  font-weight: 700; letter-spacing: 0.4px;
}

/* -------------------------------------------------------------------- */
/* Toolbar - fixed 4-column grid: cannot wrap, cannot scroll            */
/* -------------------------------------------------------------------- */
#toolbar {
  position: fixed; left: 10px; right: 10px;
  bottom: calc(var(--safe-bottom) + 10px);
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  gap: 6px; z-index: 16;
}
#toolbar .tool {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; min-height: var(--toolbar-h); padding: 6px 2px;
  background: var(--surface-ghost); border: 1px solid var(--border); color: var(--muted);
  font-weight: 700;
  height: auto;
}
#toolbar .ico { font-size: 17px; line-height: 1; }
#toolbar .lbl {
  font-size: 9.5px; letter-spacing: 0.6px; line-height: 1;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
/* State is driven by aria-pressed / aria-expanded, never inline styles. */
#toolbar .tool[aria-pressed="true"],
#toolbar .tool.active {
  color: var(--accent); border-color: var(--accent-line); background: var(--accent-tint);
}
/* Toggle sits in the HUD's own top-right corner. Sticky, not absolute, so it
   stays visible while the metric list scrolls underneath it. */
#hud-toggle {
  position: sticky; top: 0; float: right;
  width: 26px; height: 26px; padding: 0; margin: -2px -2px 0 6px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 8px; font-size: 12px; line-height: 1;
  background: var(--surface-ghost); border: 1px solid var(--border);
  color: var(--muted); cursor: pointer;
  z-index: 2; pointer-events: auto;
}
#hud-toggle .ico { transition: transform 0.2s ease; }
#hud-toggle[aria-expanded="false"] { color: var(--accent); border-color: var(--accent-line); }
#hud-toggle[aria-expanded="false"] .ico { transform: rotate(-90deg); }

/* 26px box, >=44px hit area. */
#hud-toggle::after { content: ''; position: absolute; inset: -9px; }

/* Keep the ticker clear of the floated button. */
#hud .ticker { padding-right: 4px; }

/* Collapsed: header + toggle only. The toggle is NOT a .row, so it survives
   the display:none sweep - but assert it anyway in case that list grows. */
#hud.collapsed { max-height: 56px; overflow: hidden; }
#hud.collapsed #hud-toggle { display: inline-flex !important; }


/* -------------------------------------------------------------------- */
/* Mobile                                                               */
/* -------------------------------------------------------------------- */
@media (max-width: 620px) {
  #hud { top: calc(var(--safe-top) + 58px); padding: 10px; }
  #hud .ticker { font-size: 18px; }
  #hud .mid { font-size: 13px; }
  #hud .row { font-size: 11px; }          /* readability floor */
  #status span.label { display: none; }
  #status { padding: 0 9px; }
  #view-select { max-width: 96px; padding-left: 9px; }
  #symbol-form { flex: 1 1 70px; }
  #legend { max-width: 44vw; font-size: 10px; padding: 6px 8px; line-height: 1.5; }
}

@media (max-width: 380px) {
  #toolbar .lbl { font-size: 8.5px; letter-spacing: 0.3px; }
  #view-select { max-width: 84px; font-size: 10px; }
}

/* Landscape phones fall between the width and height breakpoints, so they
   need their own query - this is where the HUD used to run off-screen. */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --control-h: 38px; --toolbar-h: 44px; }
  #hud { width: clamp(150px, 30vw, 176px); padding: 8px 9px; top: calc(var(--safe-top) + 54px); }
  #hud .ticker { font-size: 15px; }
  #hud .mid { font-size: 12px; }
  #hud .row { font-size: 10px; padding: 2px 0; }
  #alerts .alert:nth-child(n+3) { display: none; }
  #toolbar .lbl { display: none; }        /* icons only; title/aria carry meaning */
  #hint { display: none; }
}

@media (max-height: 480px) and (orientation: portrait) {
  #hud { top: calc(var(--safe-top) + 54px); }
  #hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
