/* Econ Sandbox — market simulation: index.html + style.css + sim.js + ui.js (selftest.js on #selftest).
   Spec: SPEC.md. Decisions: DECISIONS.md. */
:root {
  --bg: #eef1f6; --card: #fff; --text: #1a2030; --muted: #6b7a99;
  --border: #dde3ef; --accent: #4f7ef8; --bad: #d03b3b; --good: #0a8a0a;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1a9e77; --s4: #8b5cc4; --grid: #edf0f5; --axis: #8a96b0;
  color-scheme: light;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 13px; color: var(--text); background: var(--bg);
  height: 100dvh; overflow: hidden;
  scrollbar-width: thin; scrollbar-color: #c9d1e2 transparent;
}
* { scrollbar-width: thin; scrollbar-color: #c9d1e2 transparent; }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #c9d1e2; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: #a9b4cc; }
button, input, select { font: inherit; color: inherit; }

.page-wrap { height: 100%; display: flex; flex-direction: column; padding: 12px; gap: 10px; }
header { display: flex; align-items: center; gap: 14px; flex: 0 0 auto; }
.home-link { flex: 0 0 auto; font: 13px/1.4 system-ui, -apple-system, sans-serif; white-space: nowrap; }
.home-link a { color: #1F4E79; text-decoration: none; padding: 6px 0; display: inline-block; }
.home-link a:hover { text-decoration: underline; }
#phoneNote { position: fixed; inset: 0; z-index: 9999; background: #f7f8fa; display: flex; align-items: center; justify-content: center; padding: 24px; font: 16px/1.5 system-ui, -apple-system, sans-serif; color: #1a1a1a; }
#phoneNote div { max-width: 340px; text-align: center; }
#phoneNote h2 { font-size: 22px; margin: 0 0 8px; }
#phoneNote p { margin: 0 0 18px; color: #5b6270; }
#phoneNote button, #phoneNote a { display: block; width: 100%; margin: 8px 0; padding: 12px; border-radius: 10px; font: inherit; text-decoration: none; }
#phoneNote button { background: #1F4E79; color: #fff; border: 0; }
#phoneNote a { border: 1px solid #e2e5eb; color: #1F4E79; background: #fff; }
h1 { font-size: 1.25rem; letter-spacing: -0.4px; white-space: nowrap; }
.tiles { display: flex; gap: 6px; flex: 1; min-width: 0; }
.tiles .tile { flex: 0 0 96px; }
.tile { background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  padding: 5px 7px; flex: 1 1 0; min-width: 0; }
.tile .k { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .v { font-size: 15px; font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.tile .v.ok { color: var(--good); } .tile .v.bad { color: var(--bad); }

.grid { flex: 1 1 auto; min-height: 0; display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr); gap: 10px; }
.toolbar { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }
.settings-pop { position: fixed; z-index: 50; width: 300px; overflow-y: auto; background: var(--card);
  border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px; box-shadow: 0 8px 28px rgba(20,30,60,.18); }
.sp-h { display: flex; align-items: center; margin-bottom: 10px; }
.sp-h b { font-size: 14px; }
#bSettings.changed { border-color: #c5d3f5; background: #eaf0ff; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 12px 14px; min-height: 0; min-width: 0; display: flex; flex-direction: column;
  overflow: clip; overflow-clip-margin: 12px; position: relative; }
@media (max-width: 1023px) {
  body { overflow: auto; height: auto; }
  .grid { display: flex; flex-direction: column; }
  .card { min-height: 600px; }
  header { flex-wrap: wrap; }
}
.card-h { font-weight: 700; font-size: 13px; margin-bottom: 8px; white-space: nowrap; }

/* ---- Left: controls ---- */
.btn { border: 1px solid var(--border); background: #f6f8fc; border-radius: 9px; padding: 5px 10px;
  cursor: pointer; white-space: nowrap; }
.btn:hover { background: #eaf0ff; border-color: #c5d3f5; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { background: #3f6de6; }
.btn:disabled { opacity: .45; cursor: default; background: #f6f8fc; }
.btn.sm { padding: 3px 8px; font-size: 12px; border-radius: 7px; }
.run-row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; margin-bottom: 6px; }
.run-row .btn { padding: 5px 4px; }
.seed-row { display: flex; align-items: center; gap: 6px; margin-left: 10px; }
.seed-row input { width: 74px; border: 1px solid var(--border); border-radius: 7px; padding: 3px 6px; }
.seed-row label { color: var(--muted); }
.controls { padding-right: 4px; }

.control-row { margin-bottom: 13px; }
.control-label { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; gap: 6px; }
.control-label span:first-child { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.value-badge { font-weight: 700; color: var(--accent); background: #eaf0ff; padding: 1px 8px;
  border-radius: 999px; font-size: 12px; white-space: nowrap; }
.toggle-row { cursor: pointer; margin-bottom: 0; }
.toggle-row .spacer { flex: 1; }
.switch { appearance: none; -webkit-appearance: none; flex: none; width: 34px; height: 20px; border-radius: 999px; background: #c9d1e2;
  position: relative; cursor: pointer; transition: background .15s; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,.2); transition: left .15s; }
.switch:checked { background: var(--accent); }
.switch:checked::after { left: 16px; }
.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.slider-track { position: relative; }
.slider-track input[type="range"] { position: relative; z-index: 2; width: 100%; margin: 0; }
.tick-notches { position: absolute; left: 0; right: 0; top: 50%; height: 10px;
  transform: translateY(-50%); z-index: 1; pointer-events: none; }
.tick-notches span { position: absolute; top: 0; width: 2px; height: 10px; border-radius: 1px;
  background: #b8c2d8; transform: translateX(-50%); }
.slider-ticks { position: relative; height: 1em; margin-top: 4px; }
.slider-ticks span { position: absolute; top: 0; font-size: 10.5px; color: var(--muted);
  white-space: nowrap; transform: translateX(-50%); }
.slider-ticks span:first-child { transform: none; }
.slider-ticks span:last-child { transform: translateX(-100%); }

/* ---- Middle: charts ---- */
.sec { display: flex; flex-direction: column; min-height: 0; }
.sec-h { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; white-space: nowrap; }
.sec-h b { font-size: 13px; }
.legend { display: flex; gap: 10px; color: var(--muted); font-size: 11.5px; }
.legend i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 4px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 7px; overflow: hidden; }
.seg button { border: 0; background: #f6f8fc; padding: 1px 8px; font-size: 11.5px; cursor: pointer; }
.seg button.on { background: var(--accent); color: #fff; }
.spacer { flex: 1; }
.cv-wrap { position: relative; flex: 1 1 auto; min-height: 0; }
.cv-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#mid-card { overflow-y: auto; overflow-x: hidden; }   /* short windows scroll instead of squashing the charts */
#mid-card > .sec, #mid-card > .row2 { flex-shrink: 0 !important; flex-basis: auto !important; }   /* size from content (the chart minimums), then share any spare room */
/* Every chart keeps a comfortable minimum height at any window size; the card scrolls instead. */
#mid-card .cv-wrap { min-height: 150px; flex: 1 0 150px; }
#mid-card .cv-wrap.prices { min-height: 430px; flex-basis: 430px; }   /* 3 rows of panels, each with >= 85px of plot */
#mid-card .cv-wrap.bars { min-height: 250px; flex-basis: 250px; }     /* 12 rows at >= 18px each plus the axis */
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; min-height: 0; }
.chart-tip { position: fixed; z-index: 60; pointer-events: none; background: #3b3f47; color: #fff;
  font-size: 12px; line-height: 1.4; padding: 5px 8px; border-radius: 6px; box-shadow: 0 3px 12px rgba(0,0,0,.22);
  display: none; white-space: nowrap; font-variant-numeric: tabular-nums; }
.chart-tip .sw { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; }

/* ---- Right: tables ---- */
.tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.toolbar[hidden] { display: none; }
.map-host { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: 6px; }
.map-host[hidden] { display: none; }
.map-bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 11.5px; color: var(--muted); }
.map-bar .key { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.map-bar .key i { display: inline-block; width: 22px; height: 12px; border-radius: 6px; }
.map-bar .key i.sq { border-radius: 2px; box-sizing: border-box; }
.map-wrap { flex: 1 1 auto; min-height: 0; position: relative; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.dis-log { flex: 0 0 auto; font-size: 11.5px; color: var(--muted); line-height: 1.5; }
.dis-log div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dis-log b { color: var(--text, #111827); font-weight: 600; }
#disBanner { position: fixed; top: 10px; left: 50%; transform: translateX(-50%); z-index: 50; background: #7a2e12; color: #fff; padding: 8px 16px; border-radius: 8px; font-size: 13px; box-shadow: 0 4px 16px rgba(0,0,0,.25); max-width: 92vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; pointer-events: none; }
#disBanner[hidden] { display: none; }
.map-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.tab { border: 0; background: transparent; padding: 4px 10px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--muted); }
.tab.on { background: #eaf0ff; color: var(--accent); }
.toolbar { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; flex-wrap: nowrap; }
.count { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.table-host { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.table-host[hidden] { display: none; }
.dt-wrap { flex: 1 1 auto; min-height: 0; overflow: auto; border: 1px solid var(--border); border-radius: 10px; }
table.dt { border-collapse: separate; border-spacing: 0; table-layout: fixed; font-variant-numeric: tabular-nums; }
.dt th, .dt td { border-bottom: 1px solid var(--grid); padding: 4px 6px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; text-align: left; background: #fff; }
.dt th { position: sticky; top: 0; z-index: 3; background: #f6f8fc; font-weight: 600; font-size: 11.5px;
  vertical-align: bottom; cursor: pointer; user-select: none; border-bottom: 1px solid var(--border); white-space: normal; }
.dt th.num, .dt td.num { text-align: right; }
.dt th .th-in { display: flex; align-items: flex-end; gap: 2px; }
.dt th.num .th-in { justify-content: flex-end; }
.dt th .lbl { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.25; overflow-wrap: normal; word-break: normal; }
.dt th .arrow { font-size: 9px; color: var(--accent); flex: 0 0 auto; }
.dt th .fbtn { border: 0; background: transparent; color: #a3adc4; cursor: pointer; font-size: 10px; padding: 0 2px; flex: 0 0 auto; border-radius: 4px; }
.dt th .fbtn:hover { background: #e3e9f6; color: var(--text); }
.dt th.filtered { background: #e7efff; }
.dt th.filtered .fbtn { color: var(--accent); }
.dt th .rz { position: absolute; right: 0; top: 0; bottom: 0; width: 10px; cursor: col-resize; z-index: 4; }
.dt th .rz::after { content: ""; position: absolute; right: 0; top: 22%; bottom: 22%; width: 1px; background: #d3dae8; }
.dt th .rz:hover::after, .dt th .rz.on::after { top: 0; bottom: 0; width: 3px; background: var(--accent); }
.dt th.drop-l { box-shadow: inset 3px 0 0 var(--accent); }
.dt th.drop-r { box-shadow: inset -3px 0 0 var(--accent); }
.dt .frz { position: sticky; z-index: 2; }
.dt th.frz { z-index: 5; }
.dt td.frz-last, .dt th.frz-last { box-shadow: 1px 0 0 var(--border); }
.dt tbody tr { cursor: pointer; }
.dt tbody tr:hover td { background: #f5f8ff; }
.dt tbody tr.sel td { background: #eaf0ff; }
.dt td.drill { color: var(--accent); }
.dt td.drill:hover { text-decoration: underline; }
.dt td.neg { color: var(--bad); }

.pop { position: fixed; z-index: 80; background: #fff; border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 28px rgba(20,30,60,.18); padding: 10px; min-width: 200px; max-width: 300px; font-size: 12.5px; }
.pop h4 { font-size: 12.5px; margin-bottom: 6px; }
.pop input[type="search"], .pop input[type="number"] { width: 100%; border: 1px solid var(--border); border-radius: 7px; padding: 3px 7px; }
.pop .list { max-height: 220px; overflow-y: auto; margin: 6px 0; }
.pop .list label { display: flex; gap: 6px; align-items: center; padding: 2px 0; white-space: nowrap; cursor: pointer; }
.pop .links { display: flex; gap: 10px; margin: 4px 0; }
.pop .links a { color: var(--accent); cursor: pointer; font-size: 12px; }
.pop .rangebox { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 6px 0; }
.pop .rangebox span { font-size: 11px; color: var(--muted); }
.pop .blanks { display: flex; gap: 8px; margin-top: 6px; color: var(--muted); flex-wrap: wrap; }
.pop .blanks label { display: flex; gap: 3px; align-items: center; cursor: pointer; }
.pop .menu-item { padding: 5px 8px; border-radius: 6px; cursor: pointer; }
.pop .menu-item:hover { background: #eaf0ff; }
.pop .explain { color: var(--muted); padding: 6px 8px 2px; border-top: 1px solid var(--grid); margin-top: 4px; line-height: 1.4; white-space: normal; }
.pop .foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; gap: 8px; }

/* Citizen detail panel */
.detail { position: absolute; inset: 12px; background: #fff; z-index: 20; display: flex; flex-direction: column;
  border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; box-shadow: 0 8px 28px rgba(20,30,60,.14); }
.detail[hidden] { display: none; }
.detail-h { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.detail-h b { font-size: 15px; white-space: nowrap; }
.detail-h .sub { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.detail-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 8px; }
.detail-stats div { background: #f6f8fc; border-radius: 8px; padding: 4px 8px; min-width: 0; }
.detail-stats .k { font-size: 11px; color: var(--muted); white-space: nowrap; }
.detail-stats .v { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.detail-body { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; flex: 1 1 auto; min-height: 0; }
.detail-body > div { display: flex; flex-direction: column; min-height: 0; }
.wants { display: flex; flex-direction: column; gap: 3px; }
.want-row { display: grid; grid-template-columns: 96px 1fr 30px; gap: 6px; align-items: center; font-size: 11.5px; }
.want-row .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.want-row .bar { height: 7px; background: var(--grid); border-radius: 4px; overflow: hidden; }
.want-row .bar i { display: block; height: 100%; background: var(--s1); border-radius: 4px; }
.want-row .n { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.buys { overflow-y: auto; min-height: 0; flex: 1 1 auto; font-size: 12px; }
.buys div { display: flex; justify-content: space-between; padding: 2px 0; border-bottom: 1px solid var(--grid); gap: 8px; white-space: nowrap; }
.buys .d { color: var(--muted); }
.sub-h { font-weight: 600; font-size: 12px; margin: 2px 0 4px; white-space: nowrap; }

/* ---- Instant tooltip ---- */
.app-tip { position: fixed; z-index: 90; max-width: 260px; padding: 5px 9px; border-radius: 6px;
  background: #3b3f47; color: #fff; font-size: 12px; line-height: 1.35;
  box-shadow: 0 3px 12px rgba(0,0,0,.22); pointer-events: none; opacity: 0; white-space: normal; }
.app-tip.show { opacity: 1; }
.dt th[data-tip] { cursor: pointer; }
