/* ============================================================================
   Carino Currency — VARIANT C · FX-LEAD  (keyless FX / news / trade watcher)
   One-viewport, zero-page-scroll dashboard. CSS ONLY; HTML + JS are frozen.

   Layout doctrine
   ---------------
   body is a flex COLUMN pinned to 100dvh with overflow:hidden. Its flex-none
   children are the injected #carinoNav (60px), header.topbar, and — on mobile
   only — nav.tabbar. main.grid takes the remaining height (flex:1) and NEVER
   scrolls the page: every panel is a flex column whose .panel-body is the sole
   scroll region.

   DESKTOP (>=900px) — FX-LEAD arrangement
     Row 1 (0.9fr): #panelFx full width, SHORT — a prominent movers strip over a
                    single horizontally-scrolling row of compact trend cards.
     Row 2 (1.1fr): #panelNews | #panelTrade, split 50/50, the taller band.

   MOBILE (<900px)
     nav.tabbar appears (sticky feel, gold underline on active); only the panel
     matching main.grid[data-active] is shown and fills the viewport.

   Fleet look: near-black surfaces (#050505/#0b0b0b/#111), sharp gold #eab308,
   --ok #22c55e / --err #ef4444, IBM Plex Mono/Sans + Red Hat Display.
   ========================================================================== */

:root {
  --bg:            #050505;
  --bg-card:       #0b0b0b;
  --bg-elev:       #111111;
  --bg-elev-2:     #151515;
  --accent:        #eab308;
  --accent-faint:  rgba(234,179,8,0.06);
  --accent-glow:   rgba(234,179,8,0.15);
  --accent-dim:    rgba(234,179,8,0.30);
  --border:        #262626;
  --border-soft:   #1a1a1a;
  --text:          #ffffff;
  --text-sec:      #a3a3a3;
  --text-muted:    #666666;
  --ok:            #22c55e;   /* strengthening / positive */
  --err:           #ef4444;   /* weakening / negative */
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
  --sans: 'IBM Plex Sans', system-ui, sans-serif;
  --disp: 'Red Hat Display', var(--sans);

  /* density / rhythm tokens */
  --gap:      12px;
  --gap-sm:   8px;
  --radius:   8px;
  --radius-s: 6px;
  --pad-panel: 12px 14px;
  --safe-b:   env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
*::before, *::after { box-sizing: border-box; }

/* ---- viewport lock: page never scrolls; height fits by construction ------ */
html, body {
  height: 100vh;          /* fallback for engines without dvh */
  height: 100dvh;
  min-height: 0;
  margin: 0;
  overflow: hidden;
}
html { scroll-behavior: smooth; }

body {
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(1100px 460px at 50% -12%, rgba(234,179,8,0.07) 0%, transparent 60%),
    linear-gradient(180deg, #0b0b0b 0%, #050505 100%);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Runtime-injected navbar is body's first child — keep it from shrinking. */
#carinoNav { flex: none; }

/* ---- top control bar (flex-none) ---------------------------------------- */
.topbar {
  flex: none;
  padding: 9px 18px;
  border-bottom: 1px solid var(--border-soft);
  background: linear-gradient(180deg, rgba(255,255,255,0.015), transparent);
}
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  max-width: 1440px;
  margin: 0 auto;
}
.ctl { display: inline-flex; align-items: center; gap: 8px; }
.ctl-l {
  font-family: var(--mono); font-size: 0.6rem; text-transform: uppercase;
  letter-spacing: 0.1em; color: var(--text-muted); white-space: nowrap;
}
.input {
  background: var(--bg-elev); border: 1px solid var(--border); color: var(--text);
  border-radius: 5px; font-family: var(--mono); font-size: 0.76rem; padding: 7px 9px;
  transition: border-color 0.15s;
}
.input:focus { outline: none; border-color: var(--accent-dim); }

/* segmented window switch */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 5px; overflow: hidden; }
.seg-b {
  background: transparent; border: none; color: var(--text-sec); cursor: pointer;
  font-family: var(--mono); font-size: 0.66rem; letter-spacing: 0.06em; padding: 7px 11px;
  transition: 0.15s;
}
.seg-b:not(:last-child) { border-right: 1px solid var(--border); }
.seg-b:hover { color: var(--text); }
.seg-b.active { background: var(--accent-faint); color: var(--accent); }

/* buttons */
.btn {
  display: inline-flex; align-items: center; gap: 7px; cursor: pointer;
  background: transparent; border: 1px solid var(--border); color: var(--text-sec);
  font-family: var(--mono); font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em;
  padding: 8px 13px; border-radius: 5px; transition: 0.18s; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn svg { width: 14px; height: 14px; flex: none; }
.btn.primary { border-color: var(--accent-dim); color: var(--accent); background: var(--accent-faint); }
.btn.primary:hover { background: var(--accent-glow); }
.btn:disabled { opacity: 0.5; cursor: default; border-color: var(--border); color: var(--text-muted); }
.btn-t { display: inline; }

/* progress meter */
.progress-wrap { flex: 1 1 180px; min-width: 130px; display: flex; align-items: center; gap: 10px; }
.progress { flex: 1; height: 5px; background: var(--border-soft); border-radius: 3px; overflow: hidden; }
.progress > i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.3s ease; }
.progress-label {
  font-family: var(--mono); font-size: 0.64rem; color: var(--text-muted); white-space: nowrap;
}

/* ---- mobile pillar switcher (hidden on desktop) ------------------------- */
.tabbar {
  flex: none;
  display: none;                 /* shown < 900px */
  gap: 2px;
  padding: 0 10px;
  border-bottom: 1px solid var(--border);
  background: rgba(5,5,5,0.9);
  backdrop-filter: blur(6px);
  position: sticky; top: 0; z-index: 5;
}
.tab-b {
  flex: 1 1 auto;
  background: transparent; border: none; cursor: pointer;
  color: var(--text-sec);
  font-family: var(--mono); font-size: 0.72rem; letter-spacing: 0.06em; text-transform: uppercase;
  padding: 11px 6px 9px;
  border-bottom: 2px solid transparent;
  transition: 0.15s;
}
.tab-b:hover { color: var(--text); }
.tab-b.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ---- main grid: fills remaining height, never scrolls the page ---------- */
.grid {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: 1fr;      /* mobile: single active panel */
  grid-template-rows: 1fr;
  gap: var(--gap);
  padding: var(--gap);
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
}

/* ---- panels: each is its own flex column with an internal scroll body ---- */
.panel {
  display: none;                   /* mobile: only the active one shows */
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
/* mobile: reveal the panel matching data-active */
.grid[data-active="fx"]    #panelFx,
.grid[data-active="news"]  #panelNews,
.grid[data-active="trade"] #panelTrade { display: flex; }

.panel h2 {
  flex: none;
  font-family: var(--mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.12em;
  color: var(--text-muted); margin: 0;
  padding: 11px 14px 9px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.panel h2 .h2-extra {
  color: var(--text-sec); font-size: 0.62rem; font-weight: 400; text-transform: none;
  letter-spacing: 0.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* THE ONLY SCROLL REGION on the whole page */
.panel-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--pad-panel);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.panel-body::-webkit-scrollbar { width: 8px; }
.panel-body::-webkit-scrollbar-track { background: transparent; }
.panel-body::-webkit-scrollbar-thumb {
  background: var(--border); border-radius: 6px; border: 2px solid transparent; background-clip: padding-box;
}
.panel-body::-webkit-scrollbar-thumb:hover { background: #3a3a3a; background-clip: padding-box; }

.panel-sub { color: var(--text-muted); font-size: 0.72rem; margin: 0 0 10px; }
.note {
  color: var(--text-muted); font-family: var(--mono); font-size: 0.6rem; line-height: 1.5;
  margin: 10px 0 0;
}

/* ---- chips -------------------------------------------------------------- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; cursor: pointer;
  background: var(--bg-elev); border: 1px solid var(--border-soft); color: var(--text-sec);
  font-family: var(--mono); font-size: 0.66rem; padding: 4px 8px; border-radius: 20px;
  transition: 0.15s; user-select: none; white-space: nowrap;
}
.chip:hover { border-color: var(--accent-dim); color: var(--text); }
.chip.on { background: var(--accent-faint); border-color: var(--accent-dim); color: var(--accent); }
.chip .flag { font-size: 0.85rem; line-height: 1; }

/* ---- movers callout (prominent on the FX lead row) ---------------------- */
.movers { display: flex; flex-wrap: wrap; gap: var(--gap-sm); margin-bottom: 12px; }
.mover {
  flex: 1 1 150px; min-width: 130px;
  background: linear-gradient(160deg, var(--bg-elev-2), var(--bg-elev));
  border: 1px solid var(--border-soft); border-radius: var(--radius-s); padding: 9px 12px;
}
.mover .mv-l {
  font-family: var(--mono); font-size: 0.56rem; text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--text-muted);
}
.mover .mv-v {
  font-family: var(--disp); font-weight: 700; font-size: 1rem; margin-top: 3px;
  display: flex; align-items: baseline; gap: 6px;
}
.mover .mv-code { color: var(--text); display: inline-flex; align-items: baseline; gap: 2px; }
.mover .mv-code .flag { font-size: 0.95rem; line-height: 1; }
.mover .mv-pct { font-family: var(--mono); font-size: 0.82rem; }

/* ---- fx grid + cards ---------------------------------------------------- */
/* Mobile default: responsive multi-column grid (scrolls vertically in body). */
.fx-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: var(--gap-sm);
}
.fx-card {
  background: var(--bg-elev); border: 1px solid var(--border-soft); border-radius: 7px;
  padding: 10px 11px; min-width: 0; transition: border-color 0.15s;
}
.fx-card:hover { border-color: var(--border); }
.fx-card .fx-top { display: flex; align-items: center; gap: 7px; }
.fx-card .fx-flag { font-size: 1.05rem; line-height: 1; }
.fx-card .fx-pair { font-family: var(--mono); font-size: 0.72rem; color: var(--text); font-weight: 600; }
.fx-card .fx-name {
  font-family: var(--mono); font-size: 0.56rem; color: var(--text-muted); margin-left: auto;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55%;
}
.fx-card .fx-rate { font-family: var(--disp); font-weight: 700; font-size: 1.1rem; margin: 5px 0 2px; }
.fx-card .fx-spark { height: 34px; margin: 3px 0 2px; }
.fx-card .fx-foot { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.fx-card .fx-foot .fx-name { margin-left: 0; }

/* delta + up/down/flat tints */
.delta { font-family: var(--mono); font-size: 0.72rem; font-weight: 600; }
.delta.up  { color: var(--ok); }
.delta.down{ color: var(--err); }
.delta.flat{ color: var(--text-muted); }
.mv-pct.up,  .yoy.up  { color: var(--ok); }
.mv-pct.down,.yoy.down{ color: var(--err); }
.mv-pct.flat,.yoy.flat{ color: var(--text-muted); }

/* ---- sparklines (emitted by viz.js) ------------------------------------- */
.spark { width: 100%; height: 100%; display: block; }
.spark-area { fill: rgba(234,179,8,0.12); }
.spark-line { stroke: var(--accent); stroke-width: 1.5; }
.spark-dot  { fill: var(--accent); }
.spark.up   .spark-line { stroke: var(--ok); }
.spark.up   .spark-area { fill: rgba(34,197,94,0.12); }
.spark.up   .spark-dot  { fill: var(--ok); }
.spark.down .spark-line { stroke: var(--err); }
.spark.down .spark-area { fill: rgba(239,68,68,0.12); }
.spark.down .spark-dot  { fill: var(--err); }

/* ---- news --------------------------------------------------------------- */
.news-toolbar { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.news-search { display: flex; gap: 8px; }
.news-search .input { flex: 1 1 auto; min-width: 0; }
.news-feed {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: var(--gap-sm);
}
.news-item {
  display: block; background: var(--bg-elev); border: 1px solid var(--border-soft);
  border-left: 2px solid var(--border); border-radius: var(--radius-s);
  padding: 9px 11px; text-decoration: none; color: var(--text); transition: 0.15s;
}
.news-item:hover { border-color: var(--accent-dim); border-left-color: var(--accent); background: #141414; }
.news-item.pos { border-left-color: var(--ok); }
.news-item.neg { border-left-color: var(--err); }
.news-title { font-size: 0.8rem; line-height: 1.35; margin: 0 0 6px; }
.news-src {
  font-family: var(--mono); font-size: 0.58rem; color: var(--text-muted);
  display: flex; gap: 8px; justify-content: space-between; align-items: baseline;
}
.news-tone { font-weight: 600; }

/* ---- trade -------------------------------------------------------------- */
.trade-head { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin-bottom: 10px; }
.stats { display: flex; flex-wrap: wrap; gap: var(--gap-sm); flex: 1 1 300px; }
.stat {
  flex: 1 1 auto; background: var(--bg-elev); border: 1px solid var(--border-soft);
  border-radius: var(--radius-s); padding: 8px 11px; min-width: 100px;
}
.stat .n { font-family: var(--disp); font-weight: 700; font-size: 1.05rem; color: var(--accent); }
.stat .l {
  font-family: var(--mono); font-size: 0.56rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--text-muted); margin-top: 2px; display: flex; gap: 6px; align-items: baseline;
}
.yoy { font-size: 0.6rem; letter-spacing: 0; text-transform: none; }

.chart-wrap { height: 160px; margin: 4px 0 12px; flex: none; }
.trade-chart { width: 100%; height: 100%; display: block; }

.table-scroll { overflow-x: auto; scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
.table-scroll::-webkit-scrollbar { height: 7px; }
.table-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }
table.tbl { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 0.72rem; }
table.tbl th {
  text-align: right; color: var(--text-muted); font-weight: 500; text-transform: uppercase;
  letter-spacing: 0.05em; font-size: 0.58rem; padding: 6px 8px; border-bottom: 1px solid var(--border);
  white-space: nowrap; position: sticky; top: 0; background: var(--bg-card);
}
table.tbl th:first-child { text-align: left; }
table.tbl td { padding: 5px 8px; border-bottom: 1px solid var(--border-soft); text-align: right; white-space: nowrap; }
table.tbl td:first-child { text-align: left; color: var(--text-muted); }
table.tbl td.pos { color: var(--ok); }
table.tbl td.neg { color: var(--err); }
table.tbl tr:hover td { background: rgba(255,255,255,0.02); }

.empty-note { color: var(--text-muted); font-family: var(--mono); font-size: 0.72rem; padding: 12px 0; }

/* ==========================================================================
   DESKTOP (>=900px) — FX-LEAD grid: all three panels visible, page fixed
   ========================================================================== */
@media (min-width: 900px) {
  .tabbar { display: none; }

  .grid {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: minmax(0, 0.9fr) minmax(0, 1.1fr);
  }
  /* every panel is visible on desktop (overrides the mobile display:none) */
  .grid .panel { display: flex; }

  #panelFx    { grid-column: 1 / -1; grid-row: 1; }
  #panelNews  { grid-column: 1;      grid-row: 2; }
  #panelTrade { grid-column: 2;      grid-row: 2; }

  /* FX lead: the full-width top row shows every watched currency at once as a
     dense wrapping grid (no hidden cards); the panel-body scrolls only if the
     watchlist is very large. */
  #panelFx .movers { margin-bottom: 10px; }
  #panelFx .fx-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: var(--gap-sm);
  }
  #panelFx .note { margin-top: 8px; }
}

/* Extra-wide: give the FX lead row a touch more air */
@media (min-width: 1360px) {
  .grid { gap: 14px; padding: 14px; }
}

/* ==========================================================================
   MOBILE (<900px) — single active panel, tab switcher, safe-area
   ========================================================================== */
@media (max-width: 899px) {
  .tabbar { display: flex; }

  .topbar { padding: 8px 12px; }
  .controls { gap: 8px; }
  .grid { padding: 10px; gap: 10px; }

  /* active panel fills the remaining viewport; its body scrolls internally */
  .grid .panel { border-radius: var(--radius); }
}

/* Narrow phones: shrink controls, icon-only action buttons */
@media (max-width: 560px) {
  .topbar { padding: 7px 10px; }
  .controls { gap: 6px; }
  .ctl-l { display: none; }                       /* labels implied by content */
  .controls .btn .btn-t { display: none; }        /* icon-only Refresh / Export */
  .controls .btn { padding: 8px 10px; }
  .seg-b { padding: 7px 9px; }
  .input { padding: 6px 8px; font-size: 0.72rem; }
  .progress-wrap { flex: 1 1 100%; order: 5; min-width: 0; }

  .news-search .btn { padding: 8px 12px; }
  .fx-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
  .news-feed { grid-template-columns: 1fr; }
}

/* Honor the bottom safe-area inset (notched devices) on the last flex child */
@supports (padding: env(safe-area-inset-bottom)) {
  @media (max-width: 899px) {
    .grid { padding-bottom: calc(10px + var(--safe-b)); }
  }
}

/* Honor left/right safe-area insets (notch in landscape) so controls and panels
   never sit under a cutout. */
@supports (padding: env(safe-area-inset-left)) {
  .topbar { padding-left: max(18px, env(safe-area-inset-left)); padding-right: max(18px, env(safe-area-inset-right)); }
  .tabbar { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .grid   { padding-left: max(var(--gap), env(safe-area-inset-left)); padding-right: max(var(--gap), env(safe-area-inset-right)); }
}

/* ---- reduced motion ----------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}
