/* =========================================================================
   Paleoclimate Data Viewer — stylesheet
   Quiet scientific-instrument look: hairline borders, soft shadows,
   restrained palette, tabular numerals for readouts.
   ========================================================================= */

:root {
  --bg:          #f2f4f8;
  --panel:       #ffffff;
  --panel-2:     #f8fafc;
  --panel-3:     #eef2f7;
  --ink:         #182230;
  --ink-2:       #4d5b6e;
  --ink-3:       #7d8a9c;
  --ink-4:       #a7b2c0;
  --line:        #e2e8f0;
  --line-2:      #edf1f6;
  --accent:      #2563eb;
  --accent-ink:  #1749b8;
  --accent-soft: #e9f0fd;
  --warm:        #d97706;
  --warm-soft:   #fdf1e2;
  --gi:          #dc2626;
  --gs:          #2563eb;
  --shadow-s:    0 1px 2px rgba(24, 34, 48, .05);
  --shadow-m:    0 6px 18px rgba(24, 34, 48, .07), 0 1px 3px rgba(24, 34, 48, .05);
  --shadow-l:    0 24px 64px rgba(24, 34, 48, .18);
  --r-s: 7px;
  --r-m: 10px;
  --r-l: 14px;
  --sidebar-w: 312px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto,
          "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button { font-family: inherit; cursor: pointer; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ccd5e0; border-radius: 8px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: #aeb9c8; background-clip: content-box; }

/* ------------------------------------------------------------------ layout */
#app { display: flex; height: 100vh; overflow: hidden; }

/* ----------------------------------------------------------------- sidebar */
#sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
  z-index: 4;
}

.sidebar-head { padding: 18px 18px 12px; border-bottom: 1px solid var(--line-2); }
.brand { display: flex; gap: 11px; align-items: center; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px; flex: 0 0 34px;
  background: linear-gradient(145deg, #1e3a5f 0%, #2563eb 60%, #0d9488 130%);
  display: grid; place-items: center;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.25), var(--shadow-s);
}
.brand-mark svg { width: 20px; height: 20px; }
.brand h1 { margin: 0; font-size: 14.5px; font-weight: 650; letter-spacing: .1px; }
.brand p  { margin: 2px 0 0; font-size: 11px; color: var(--ink-3); }

/* mode switch */
.mode-switch {
  display: flex; gap: 3px; padding: 3px; margin: 14px 18px 0;
  background: var(--panel-3); border-radius: var(--r-m);
}
.mode-switch button {
  flex: 1; border: 0; background: transparent; color: var(--ink-2);
  font-size: 12.5px; font-weight: 560; padding: 7px 6px; border-radius: var(--r-s);
  transition: background .14s, color .14s, box-shadow .14s;
}
.mode-switch button:hover { color: var(--ink); }
.mode-switch button.active { background: var(--panel); color: var(--accent-ink); box-shadow: var(--shadow-s); }

.mode-hint {
  margin: 10px 18px 0; font-size: 11.5px; line-height: 1.55; color: var(--ink-3);
}
.mode-hint b { color: var(--ink-2); font-weight: 600; }

/* tools */
.sidebar-tools { padding: 12px 18px 10px; display: flex; flex-direction: column; gap: 8px; }
.sidebar-tools input[type="search"] {
  width: 100%; padding: 8px 11px; border: 1px solid var(--line); border-radius: var(--r-s);
  font-size: 12.5px; background: var(--panel-2); color: var(--ink); outline: none;
  transition: border-color .14s, background .14s, box-shadow .14s;
}
.sidebar-tools input[type="search"]:focus {
  border-color: var(--accent); background: #fff;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}
.tool-row { display: flex; align-items: center; justify-content: space-between; font-size: 11.5px; color: var(--ink-3); }
.tool-row .tool-btns { display: flex; gap: 6px; }
.tool-row button {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink-2);
  font-size: 11px; padding: 3px 10px; border-radius: 20px; transition: all .14s;
}
.tool-row button:hover { border-color: var(--accent); color: var(--accent-ink); background: var(--accent-soft); }
.tool-row .count { font-variant-numeric: tabular-nums; }
.tool-row .count b { color: var(--accent-ink); font-weight: 650; }

/* dataset list */
.dataset-list { flex: 1; overflow-y: auto; padding: 2px 12px 16px; min-height: 0; }
.cat-head {
  display: flex; align-items: center; gap: 8px;
  padding: 13px 8px 5px; font-size: 10px; font-weight: 700;
  letter-spacing: .9px; color: var(--ink-4); text-transform: uppercase;
  position: sticky; top: 0; background: linear-gradient(#fff 65%, rgba(255,255,255,0));
  z-index: 2;
}
.cat-head::after { content: ""; flex: 1; height: 1px; background: var(--line-2); }

.ds-item {
  width: 100%; display: flex; gap: 10px; align-items: flex-start; text-align: left;
  border: 1px solid transparent; background: transparent; border-radius: var(--r-m);
  padding: 8px 10px; margin-bottom: 2px; transition: background .12s, border-color .12s;
  position: relative;
}
.ds-item:hover { background: var(--panel-2); }
.ds-item[aria-pressed="true"] { background: var(--accent-soft); border-color: #c9dbfb; }
.ds-swatch {
  width: 4px; align-self: stretch; min-height: 30px; border-radius: 3px;
  background: var(--c, #888); flex: 0 0 4px; opacity: .45;
  margin: 1px 0;
}
.ds-item[aria-pressed="true"] .ds-swatch { opacity: 1; }
.ds-text { flex: 1; min-width: 0; }
.ds-ind { display: block; font-size: 12.8px; font-weight: 580; color: var(--ink); line-height: 1.35; }
.ds-meta { display: block; font-size: 11px; color: var(--ink-3); margin-top: 2px; line-height: 1.35; }
.ds-span {
  display: block; font-size: 10px; color: var(--ink-4); margin-top: 3px;
  font-variant-numeric: tabular-nums; letter-spacing: .2px;
}
.ds-flag {
  display: inline-block; font-size: 9px; padding: 0 5px; border-radius: 4px;
  background: var(--warm-soft); color: #9a5b0b; margin-left: 6px; vertical-align: 1px;
  border: 1px solid #f3dfbd; font-weight: 620; letter-spacing: .2px;
}
.ds-order {
  position: absolute; top: 8px; right: 9px; font-size: 9.5px; font-weight: 700;
  color: #fff; background: var(--accent); border-radius: 20px;
  padding: 1.5px 7px; line-height: 1.45; letter-spacing: .3px;
}
.ds-order.is-right { background: var(--gi); }
.sidebar-foot {
  border-top: 1px solid var(--line-2); padding: 10px 18px; font-size: 10.5px;
  color: var(--ink-4); line-height: 1.5;
}
.sidebar-foot code { font-family: var(--mono); font-size: 10px; color: var(--ink-3); }
.sidebar-foot b { color: var(--ink-3); font-weight: 620; }

/* ------------------------------------------------------------------- stage */
#stage { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 20px; background: rgba(255,255,255,.88);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); z-index: 3;
}
.topbar-title { font-size: 13.5px; font-weight: 640; letter-spacing: .1px; }
.topbar-title .sub { font-weight: 420; color: var(--ink-3); margin-left: 9px; font-size: 12px; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink-2);
  font-size: 12.5px; font-weight: 560; padding: 6px 13px; border-radius: var(--r-s);
  display: inline-flex; align-items: center; gap: 7px; transition: all .14s; white-space: nowrap;
}
.btn:hover { border-color: #c4cfdd; color: var(--ink); box-shadow: var(--shadow-s); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
.btn[aria-pressed="true"] { background: var(--accent-soft); border-color: #bcd3fa; color: var(--accent-ink); }
.btn svg { width: 11px; height: 11px; display: block; }
.btn.is-disabled, .btn:disabled {
  opacity: .45; cursor: not-allowed; box-shadow: none;
}
.btn.is-disabled:hover, .btn:disabled:hover { border-color: var(--line); color: var(--ink-2); background: var(--panel); }
#btnSync[data-locked="true"] { background: var(--accent-soft); border-color: #bcd3fa; color: var(--accent-ink); }
#btnSync[data-locked="false"] { background: var(--warm-soft); border-color: #efd3ac; color: #9a5b0b; }

.ev-dots { display: inline-flex; gap: 3px; align-items: center; }
.ev-dots i { width: 5px; border-radius: 2px; display: block; }
.ev-dots .ev-gi { height: 12px; background: var(--gi); }
.ev-dots .ev-gs { height: 8px; background: var(--gs); }

.plots { flex: 1; overflow-y: auto; padding: 16px 20px 24px; min-height: 0; display: flex; flex-direction: column; gap: 14px; }

/* --------------------------------------------------------- empty state */
.empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; color: var(--ink-3); text-align: center; padding: 40px;
}
.empty-state .es-ico { opacity: .55; }
.empty-state h2 { margin: 0; font-size: 16px; font-weight: 620; color: var(--ink-2); }
.empty-state p { margin: 0; font-size: 12.5px; max-width: 480px; line-height: 1.7; }
.empty-state b { color: var(--ink-2); font-weight: 620; }

/* -------------------------------------------------------------- plot card */
.plot-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-l);
  box-shadow: var(--shadow-s); display: flex; flex-direction: column;
  overflow: hidden; flex: 0 0 auto;
}

.plot-head {
  display: flex; align-items: center; gap: 12px; padding: 8px 14px;
  border-bottom: 1px solid var(--line-2); flex-wrap: wrap; row-gap: 6px;
}
.plot-head .ph-left { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1 1 auto; flex-wrap: wrap; }
.plot-head .ph-right { display: flex; align-items: center; gap: 7px; margin-left: auto; flex: 0 0 auto; }

.chip {
  display: inline-flex; align-items: center; gap: 7px; padding: 3px 10px 3px 7px;
  border-radius: 20px; border: 1px solid var(--line); background: var(--panel);
  font-size: 11.5px; color: var(--ink-2); max-width: 100%;
}
.chip .dot {
  width: 8px; height: 8px; border-radius: 50%; flex: 0 0 8px;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 16%, transparent);
}
.chip .ax {
  font-size: 9px; font-weight: 750; letter-spacing: .5px; color: var(--ink-4);
  text-transform: uppercase; border-right: 1px solid var(--line); padding-right: 7px;
}
.chip .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 560; }
.chip .unit { color: var(--ink-4); font-weight: 420; }

.plot-host-wrap { position: relative; }
.readout {
  position: absolute; top: 8px; left: 0; z-index: 3; pointer-events: none;
  font-family: var(--mono); font-size: 10.5px; color: var(--ink-2);
  background: rgba(255, 255, 255, .92); border: 1px solid var(--line-2); border-radius: var(--r-s);
  padding: 3px 9px; white-space: nowrap; font-variant-numeric: tabular-nums;
  min-height: 22px; display: flex; align-items: center; gap: 8px;
  box-shadow: var(--shadow-s);
  opacity: 0; transition: opacity .12s;
}
.readout.has-value { opacity: 1; }
.readout .ro-k { color: var(--ink-4); }
.readout .ro-v { color: var(--ink); font-weight: 600; }
.readout .ro-sep { width: 1px; height: 11px; background: var(--line); }
.readout .ro-lag { color: var(--warm); }

.win-readout {
  font-family: var(--mono); font-size: 10.5px; color: var(--ink-3);
  padding: 3px 9px; background: var(--panel-3); border-radius: 20px; white-space: nowrap;
  font-variant-numeric: tabular-nums; min-width: 15ch; text-align: center;
}

.mini-btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink-3);
  font-size: 11px; padding: 3.5px 9px; border-radius: 6px; transition: all .14s;
}
.mini-btn:hover { color: var(--ink); border-color: #c4cfdd; }
.mini-btn.is-disabled, .mini-btn:disabled {
  opacity: .42; cursor: not-allowed;
}
.mini-btn.is-disabled:hover, .mini-btn:disabled:hover { color: var(--ink-3); border-color: var(--line); }

.plot-host { width: 100%; overflow: hidden; }
.plot-host .uplot { width: auto; }
.plot-body { padding: 4px 10px 0; }
.plot-foot { padding: 0 10px 10px; position: relative; }

/* ---------------------------------------------------------- range slider */
.rb { position: relative; height: 26px; margin: 0; user-select: none; touch-action: none; }
.rb.is-disabled { opacity: .38; pointer-events: none; }
.rb-track {
  position: absolute; left: 0; right: 0; top: 12px; height: 4px;
  background: var(--panel-3); border-radius: 3px; box-shadow: inset 0 0 0 1px #e6ebf2;
}
.rb-mask { position: absolute; top: 12px; height: 4px; background: #e3e9f1; border-radius: 3px; }
.rb-sel {
  position: absolute; top: 10px; height: 8px; border-radius: 5px;
  background: color-mix(in srgb, var(--accent) 30%, #fff);
  box-shadow: inset 0 0 0 1px #a9c5f5; cursor: grab;
}
.rb-sel:active { cursor: grabbing; }
.rb-h {
  position: absolute; top: 4px; width: 12px; height: 20px; margin-left: -6px;
  background: var(--panel); border: 1.5px solid var(--accent); border-radius: 5px;
  box-shadow: var(--shadow-s); cursor: ew-resize; transition: transform .1s, box-shadow .1s;
}
.rb-h::after {
  content: ""; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: 3.5px; height: 9px; border-radius: 1px;
  border-left: 1px solid #9fbef0; border-right: 1px solid #9fbef0;
}
.rb-h:hover { transform: scaleY(1.08); }
.rb-h:focus-visible, .rb-sel:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* -------------------------------------------------------------- lag slider */
.lagbar {
  display: flex; align-items: center; gap: 12px; padding: 4px 4px 2px;
  width: 100%; flex-wrap: nowrap;
}
.lagbar .lb-label {
  font-size: 11px; color: var(--ink-2); font-weight: 600; white-space: nowrap;
  display: flex; align-items: center; gap: 6px;
}
.lagbar .lb-label .tag {
  font-size: 8.5px; font-weight: 750; letter-spacing: .5px; background: var(--warm-soft);
  color: #9a5b0b; border: 1px solid #f3dfbd; border-radius: 4px; padding: 1px 5px;
}
.lb-track-wrap { position: relative; flex: 1; min-width: 120px; height: 31px; user-select: none; touch-action: none; }
.lb-track {
  position: absolute; left: 0; right: 0; top: 14px; height: 4px; border-radius: 3px;
  background: linear-gradient(90deg, #e7edf6 0%, #eef2f7 50%, #fceee0 100%);
  box-shadow: inset 0 0 0 1px #e6ebf2;
}
.lb-zero { position: absolute; top: 9px; width: 1.5px; height: 14px; background: #b4c0cf; margin-left: -.75px; }
.lb-fill { position: absolute; top: 14px; height: 4px; background: #eeaa5e; border-radius: 3px; opacity: .85; }
.lb-knob {
  position: absolute; top: 6px; width: 12px; height: 20px; margin-left: -6px;
  background: var(--panel); border: 1.5px solid var(--warm); border-radius: 5px;
  box-shadow: var(--shadow-s); cursor: ew-resize;
}
.lb-knob::after {
  content: ""; position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
  width: 3.5px; height: 9px; border-radius: 1px;
  border-left: 1px solid #eec389; border-right: 1px solid #eec389;
}
.lb-knob:focus-visible { outline: 2px solid var(--warm); outline-offset: 2px; }
.lb-scale {
  position: absolute; left: 2px; right: 2px; top: 21px; display: flex; justify-content: space-between;
  font-size: 9px; color: var(--ink-4); font-family: var(--mono); pointer-events: none;
}
.lb-val {
  font-family: var(--mono); font-size: 11px; font-weight: 620; color: var(--warm);
  min-width: 76px; text-align: right; font-variant-numeric: tabular-nums;
}
.lag-hint { font-size: 11.5px; color: var(--ink-3); padding: 5px 4px 3px; }

/* ------------------------------------------------------------------ drawer */
#backdrop {
  position: fixed; inset: 0; background: rgba(24, 34, 48, .32); opacity: 0;
  pointer-events: none; transition: opacity .24s; z-index: 40; backdrop-filter: blur(2px);
}
#backdrop.open { opacity: 1; pointer-events: auto; }

#drawer {
  position: fixed; top: 0; right: 0; height: 100%; width: min(580px, 94vw);
  background: var(--panel); box-shadow: var(--shadow-l); z-index: 50;
  transform: translateX(103%); transition: transform .3s cubic-bezier(.4,0,.2,1);
  display: flex; flex-direction: column;
}
#drawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: center; gap: 10px; padding: 16px 22px;
  border-bottom: 1px solid var(--line);
}
.drawer-head h2 { margin: 0; font-size: 15px; font-weight: 660; }
.drawer-head .dh-sub { font-size: 11.5px; color: var(--ink-3); margin-top: 2px; }
.drawer-head .close-btn {
  margin-left: auto; border: 1px solid var(--line); background: var(--panel);
  width: 30px; height: 30px; border-radius: 50%; color: var(--ink-2); font-size: 16px;
  display: grid; place-items: center; line-height: 1; transition: background .14s;
}
.drawer-head .close-btn:hover { background: var(--panel-3); }
.drawer-body { overflow-y: auto; padding: 18px 22px 48px; }

.dsec { margin-bottom: 26px; }
.dsec > h3 {
  margin: 0 0 10px; font-size: 10.5px; font-weight: 720; letter-spacing: 1px;
  text-transform: uppercase; color: var(--ink-4);
  display: flex; align-items: center; gap: 8px;
}
.dsec > h3::after { content: ""; flex: 1; height: 1px; background: var(--line-2); }

.info-grid { display: grid; grid-template-columns: 108px 1fr; gap: 7px 14px; font-size: 12.5px; }
.info-grid dt { color: var(--ink-3); font-size: 12px; }
.info-grid dd { margin: 0; color: var(--ink); line-height: 1.5; }
.info-grid dd a { color: var(--accent); text-decoration: none; }
.info-grid dd a:hover { text-decoration: underline; }

.cite-card {
  border: 1px solid var(--line); border-left: 3px solid var(--c, var(--accent));
  border-radius: var(--r-m); padding: 11px 13px; margin-bottom: 10px; background: var(--panel-2);
}
.cite-card .cc-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 4px; }
.cite-card .cc-ind { font-size: 12.5px; font-weight: 640; }
.cite-card .cc-author { font-size: 11px; color: var(--ink-3); }
.cite-card .cc-author.is-selected { color: var(--accent-ink); font-weight: 640; }
.cite-card .cc-meta { font-size: 10.5px; color: var(--ink-4); font-family: var(--mono); margin-bottom: 7px; }
.cite-card ul { margin: 0; padding-left: 16px; }
.cite-card li { font-size: 11.5px; line-height: 1.6; color: var(--ink-2); margin-bottom: 4px; }
.cite-card li .doi { display: block; font-family: var(--mono); font-size: 10.5px; color: var(--ink-4); }
.cite-card li .doi a { color: var(--accent); text-decoration: none; }
.cite-card .cc-lim { margin-top: 8px; font-size: 11px; color: #8a5a17; background: #fdf8ee; border: 1px solid #f2e4c6; border-radius: 6px; padding: 7px 9px; line-height: 1.55; }
.cite-card .cc-lim b { font-weight: 660; }
.cite-card .cc-lim ul { padding-left: 15px; }

.note {
  font-size: 11.5px; line-height: 1.7; color: var(--ink-2); background: var(--panel-3);
  border: 1px solid var(--line-2); border-radius: var(--r-m); padding: 10px 13px;
}
.note code { font-family: var(--mono); font-size: 11px; background: #fff; padding: 1px 4px; border-radius: 4px; border: 1px solid var(--line-2); }
.note + .note { margin-top: 8px; }
.note.warn { background: #fdf8ee; border-color: #f2e4c6; color: #7a5216; }
.note b { font-weight: 640; }

/* ------------------------------------------------------------------- toast */
#toast {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 14px);
  background: #182230; color: #fff; font-size: 12.5px; padding: 9px 17px;
  border-radius: 22px; box-shadow: var(--shadow-l); opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 60; max-width: 80vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ------------------------------------------------------------- uPlot tweaks */
.uplot { font-family: var(--font); }
.uplot .u-wrap { background: transparent; }
.u-cursor-x, .u-cursor-y { will-change: transform; }
.u-select { background: rgba(37, 99, 235, .12); }
.u-axis { color: var(--ink-3); }
.u-legend { display: none; }

/* --------------------------------------------------------------- responsive */
@media (max-width: 960px) {
  :root { --sidebar-w: 272px; }
  .brand h1 { font-size: 13px; }
}
@media (max-width: 720px) {
  #app { flex-direction: column; height: auto; min-height: 100vh; }
  #sidebar { width: 100%; flex: 0 0 auto; border-right: 0; border-bottom: 1px solid var(--line); max-height: 48vh; }
  .dataset-list { max-height: 32vh; }
  #stage { flex: 1 1 auto; min-height: 60vh; }
  .plots { padding: 12px; }
  .topbar { padding: 10px 12px; flex-wrap: wrap; }
  .topbar-title .sub { display: none; }
}
