:root {
  --bg: #0f1116;
  --panel: #151922;
  --panel-2: #1a1f2b;
  --border: #232a38;
  --text: #d8dde7;
  --muted: #7f8a9e;
  --accent: #2962ff;
  --accent-dim: #1f3f9e;
  --up: #26a69a;
  --warn: #f0a202;
  --danger: #ef5350;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

/* Author `display` rules outrank the UA stylesheet's [hidden] rule, so
   without this an element toggled with .hidden stays laid out — and an
   invisible full-size overlay silently eats every mouse event. */
[hidden] { display: none !important; }

body {
  /* A column layout rather than height arithmetic: the top bar wraps to two
     or three rows on a phone, and any hardcoded offset is wrong the moment
     it does. */
  display: flex;
  flex-direction: column;
}

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

/* ---------- top bar ---------- */

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 16px;
  height: 52px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 9px; }

.brand .mark {
  width: 10px; height: 18px;
  border-radius: 2px;
  background: linear-gradient(180deg, var(--up), var(--accent));
}

.topbar h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 650;
  letter-spacing: 0.02em;
}

.spacer { flex: 1; }

.field { display: flex; align-items: center; gap: 7px; }
.field > span { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.06em; }

select, input[type="search"] {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 5px 8px;
  font: inherit;
  outline: none;
}

select:focus, input[type="search"]:focus { border-color: var(--accent-dim); }

.coverage {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 11.5px;
  white-space: nowrap;
}

.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 6px 11px;
  font: inherit;
  cursor: pointer;
}

.btn:hover { border-color: #33405a; background: #1f2534; }

.linkbtn {
  background: none; border: none; color: var(--muted);
  font: inherit; cursor: pointer; padding: 0; text-decoration: underline;
}
.linkbtn:hover { color: var(--text); }

/* ---------- layout ---------- */

main {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* ---------- sidebar ---------- */

.sidebar {
  width: 290px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

.sidebar-controls {
  padding: 11px 12px;
  display: flex;
  flex-direction: column;
  gap: 9px;
  border-bottom: 1px solid var(--border);
}

.sidebar-controls input[type="search"] { width: 100%; }

.seg {
  display: flex;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  overflow: hidden;
}

.seg button {
  flex: 1;
  background: none;
  border: none;
  color: var(--muted);
  padding: 5px 9px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}

.seg button:hover { color: var(--text); }
.seg button.on { background: var(--accent); color: #fff; font-weight: 600; }

.list-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 7px 12px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border-bottom: 1px solid var(--border);
}

.watchlist {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.watchlist li {
  display: grid;
  grid-template-columns: 8px 1fr auto;
  gap: 9px;
  align-items: center;
  padding: 7px 12px;
  border-bottom: 1px solid #1b212c;
  cursor: pointer;
}

.watchlist li:hover { background: var(--panel-2); }
.watchlist li.active { background: #172033; }

.watchlist .chip {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: #2f3847;
}

.watchlist li.active .chip { box-shadow: 0 0 0 3px rgba(41, 98, 255, 0.15); }

.watchlist .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.watchlist li.active .name { font-weight: 600; }

.watchlist .stats {
  text-align: right;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.3;
  color: var(--muted);
}

.watchlist .spike { color: var(--warn); font-weight: 600; }
.watchlist .peak { display: block; }

.watchlist .sub {
  grid-column: 2 / -1;
  font-family: var(--mono);
  font-size: 10.5px;
  color: #5d6678;
}

.empty-list { padding: 22px 14px; color: var(--muted); text-align: center; }

/* ---------- chart ---------- */

.chartwrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chart-toolbar {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.chart-toolbar .seg { flex: none; }
.chart-toolbar .seg button { min-width: 44px; }

.unit {
  color: var(--muted);
  font-size: 11.5px;
  font-family: var(--mono);
}

.chart-area { position: relative; flex: 1; min-height: 0; }
#chart { position: absolute; inset: 0; }

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  color: var(--muted);
  padding: 30px;
  z-index: 2;
}

.empty h2 { margin: 0; font-size: 15px; color: var(--text); font-weight: 600; }
.empty p { margin: 0; max-width: 430px; line-height: 1.6; }

/* ---------- drawing overlay ---------- */

#draw {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;   /* enabled only while a drawing tool is active */
}

/* ---------- sidebar extras ---------- */

.row2 { display: flex; gap: 8px; }
.row2 .mini { flex: 1; }

.mini { display: flex; flex-direction: column; gap: 3px; }
.mini > span {
  color: var(--muted); font-size: 10.5px;
  text-transform: uppercase; letter-spacing: 0.06em;
}
.mini select { width: 100%; }

.checkline {
  display: flex; align-items: center; gap: 7px;
  color: var(--text); font-size: 12px; cursor: pointer;
}
.checkline input { accent-color: var(--accent); }
.checkline.disabled { opacity: 0.45; cursor: not-allowed; }

.watchlist li { position: relative; }

.watchlist .expand {
  position: absolute;
  right: 4px; bottom: 4px;
  background: none; border: none; color: var(--muted);
  font-size: 11px; cursor: pointer; padding: 2px 4px; line-height: 1;
}
.watchlist .expand:hover { color: var(--text); }

.watchlist li.members {
  display: block;
  padding: 4px 12px 8px 28px;
  background: #121722;
  border-bottom: 1px solid #1b212c;
}

.member {
  padding: 3px 6px;
  border-radius: 3px;
  color: var(--muted);
  font-size: 11.5px;
  cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.member:hover { background: var(--panel-2); color: var(--text); }
.member.on { color: var(--text); font-weight: 600; }

/* ---------- detail panel ---------- */

.detail {
  border-top: 1px solid var(--border);
  background: var(--panel-2);
  padding: 10px 12px 12px;
  max-height: 44%;
  overflow-y: auto;
  flex: none;
}

.detail-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 8px; margin-bottom: 8px;
}
.detail-title { font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; }
.detail-loading { color: var(--muted); font-size: 12px; }

.kv { margin: 0; display: flex; flex-direction: column; gap: 4px; }
.kv > div { display: flex; justify-content: space-between; gap: 10px; font-size: 11.5px; }
.kv dt { color: var(--muted); margin: 0; }
.kv dd { margin: 0; font-family: var(--mono); text-align: right; }
.kv .dim { color: var(--muted); font-weight: 400; }

.tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 9px; }
.tag {
  background: #1d2431; border: 1px solid var(--border); border-radius: 3px;
  padding: 1px 6px; font-size: 10.5px; color: var(--muted);
}

.detail-note {
  margin-top: 9px; font-size: 11px; line-height: 1.5;
  color: var(--muted); border-left: 2px solid var(--accent-dim); padding-left: 8px;
}

/* ---------- toolbar density ---------- */

.chart-toolbar .btn { padding: 5px 9px; }
#tools button { min-width: 30px; font-size: 13px; }

/* ---------- checkboxes in the list ---------- */

.watchlist li {
  grid-template-columns: auto 8px 1fr auto;
}

.watchlist .pick {
  accent-color: var(--accent);
  width: 15px; height: 15px;
  margin: 0;
  cursor: pointer;
  flex: none;
}

.watchlist .sub { grid-column: 3 / -1; }

/* ---------- resizable sidebar ---------- */

.dragbar {
  width: 5px;
  flex: none;
  cursor: col-resize;
  background: var(--border);
  transition: background 0.15s;
}
.dragbar:hover, .dragbar.dragging { background: var(--accent); }

.iconbtn {
  display: none;              /* only needed once the sidebar collapses */
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 15px;
  line-height: 1;
  padding: 6px 9px;
  cursor: pointer;
}

/* ---------- nothing clipped, at any width ---------- */

/* The toolbars wrap instead of overflowing, so no control is ever cut off
   or pushed out of reach on a narrow screen. */
.topbar, .chart-toolbar {
  flex-wrap: wrap;
  row-gap: 6px;
}
.topbar { height: auto; min-height: 52px; padding: 7px 14px; }

.chart-toolbar .seg button { min-width: 38px; }

@media (max-width: 1100px) {
  .coverage { display: none; }      /* first thing to go; it's in the panel */
}

@media (max-width: 860px) {
  .iconbtn { display: block; }
  .dragbar { display: none; }

  main { position: relative; }

  /* Sidebar becomes an overlay drawer so the chart keeps the full width. */
  .sidebar {
    position: absolute;
    z-index: 20;
    top: 0; bottom: 0; left: 0;
    width: min(88vw, 330px);
    transform: translateX(-102%);
    transition: transform 0.18s ease;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.5);
  }
  main.drawer-open .sidebar { transform: translateX(0); }

  .topbar .field span { display: none; }   /* labels; the control is obvious */
  #source { max-width: 45vw; }
  
  
}

@media (max-width: 560px) {
  .topbar h1 { display: none; }
  .chart-toolbar { padding: 7px 10px; gap: 6px; }
  .chart-toolbar .seg button { min-width: 32px; padding: 6px 7px; font-size: 11.5px; }
  .unit { display: none; }
}

/* Touch targets need to be finger-sized, not mouse-sized. */
@media (pointer: coarse) {
  .seg button { padding: 9px 11px; }
  .btn { padding: 9px 12px; }
  .watchlist li { padding: 10px 12px; }
  .watchlist .pick { width: 19px; height: 19px; }
  .member { padding: 7px 6px; }
}

/* ---------- pointer tooltip ---------- */

.tip {
  position: absolute;
  z-index: 4;
  pointer-events: none;
  display: flex;
  align-items: center;
  flex-wrap: wrap;      /* lets the date drop onto its own line */
  gap: 6px;
  background: rgba(15, 17, 22, 0.96);
  border: 1px solid #33405a;
  border-radius: 4px;
  padding: 4px 9px;
  font-size: 12px;
  white-space: nowrap;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.5);
}
.tip .dot { width: 8px; height: 8px; border-radius: 50%; }
.tip .t { max-width: 230px; overflow: hidden; text-overflow: ellipsis; }
.tip .v { font-family: var(--mono); font-weight: 600; color: var(--text); }

/* uPlot needs the plot surface to own vertical gestures, or the page
   scrolls instead of the chart panning. */
.u-over { touch-action: none; }

/* Inline label + control pairs in the chart toolbar. */
.inline-field { display: flex; align-items: center; gap: 6px; }
.inline-field > span {
  color: var(--muted); font-size: 10.5px;
  text-transform: uppercase; letter-spacing: 0.06em;
}
.inline-field select { padding: 4px 6px; font-size: 12px; }
@media (max-width: 560px) { .inline-field > span { display: none; } }

/* ---------- why a term surged ---------- */

.why {
  display: inline-block;
  border-radius: 3px;
  padding: 0 5px;
  font-size: 9.5px;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  vertical-align: 1px;
  white-space: nowrap;
}

/* Muted, not alarming — these are facts about the world, not alerts. */
.why-death    { background: #2a2230; color: #c9a6d8; }
.why-crime    { background: #2e2224; color: #dba0a4; }
.why-election { background: #202a33; color: #9dc4e0; }
.why-film     { background: #2b2820; color: #d8c58f; }
.why-tv       { background: #2b2820; color: #d8c58f; }
.why-music    { background: #202c28; color: #97cfba; }
.why-sport    { background: #1f2b22; color: #9fd0a2; }
.why-disaster { background: #302520; color: #e0b394; }
.why-conflict { background: #2e2220; color: #dfab95; }
.why-other    { background: #262b35; color: var(--muted); }

.detail-head .why { margin-left: 6px; }

/* The date sits under the term and value, dimmer, so the tooltip reads as
   "what, how much" first and "when" second. */
.tip .d {
  display: block;
  width: 100%;
  margin-top: 3px;
  color: var(--muted, #8b93a7);
  font-family: var(--mono);
  font-size: 10px;
}

/* Why the line rose, in the hover readout — a story is easier to recognise
   by its cause than by its shape. */
.tip .why {
  display: block;
  width: 100%;
  margin-top: 3px;
  padding: 1px 5px;
  border-radius: 3px;
  background: rgba(120, 144, 200, 0.16);
  color: var(--text, #e6e9f0);
  font-size: 10px;
  letter-spacing: 0.02em;
  align-self: flex-start;
  width: fit-content;
}
