:root {
  --bg: #1a1713;
  --panel: rgba(28, 24, 20, 0.96);
  --panel2: rgba(48, 41, 34, 0.9);
  --ink: #e9e0d0;
  --muted: #9c917f;
  --gold: #e0b45a;
  --accent: #c8713a;
  --good: #7fc97f;
  --bad: #e07a6a;
  --border: #4a4034;
  --top: 48px;
  --peek: 104px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--ink);
  font: 15px/1.4 -apple-system, "Segoe UI", system-ui, sans-serif; overscroll-behavior: none; position: fixed; width: 100%; }
#view { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

button, select, input { font: inherit; color: var(--ink); background: var(--panel2); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 12px; min-height: 42px; cursor: pointer; touch-action: manipulation; }
button.active { background: var(--gold); color: #1a1713; border-color: var(--gold); }
button:active { background: var(--gold); color: #1a1713; }
button:disabled { opacity: 0.4; }
button.small { padding: 5px 10px; min-height: 34px; font-size: 14px; }
.btnrow { display: flex; flex-wrap: wrap; gap: 6px; }
.btnrow button { display: flex; align-items: center; gap: 6px; }
.row { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; align-items: center; }
.swatch { display: inline-block; width: 11px; height: 11px; border-radius: 3px; border: 1px solid rgba(0,0,0,0.4); }
.cost { color: var(--muted); font-size: 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.big { font-size: 18px; font-weight: 700; color: var(--gold); }
.good { color: var(--good); }
.bad { color: var(--bad); }
.gold { color: var(--gold); }
h3 { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.hidden { display: none !important; }
a { color: var(--gold); }

/* top bar */
#topbar { position: absolute; top: 0; left: 0; right: 0; height: calc(var(--top) + var(--sat)); padding: var(--sat) 8px 0; display: flex; align-items: center; gap: 8px;
  background: var(--panel); border-bottom: 1px solid var(--border); font-size: 14px; white-space: nowrap; }
#topbar button { min-height: 36px; padding: 4px 10px; }
#menubtn { font-size: 18px; padding: 2px 10px; }
#date { color: var(--muted); }
#cash { font-weight: 700; color: var(--gold); margin-left: auto; }
#macro { padding: 2px 8px; border-radius: 10px; background: var(--panel2); font-size: 12px; }
#macro.boom { color: var(--good); } #macro.recession { color: var(--bad); }
#speedbtn { min-width: 46px; padding: 4px 6px; }
#pausebtn { min-width: 42px; padding: 4px 6px; }
@media (max-width: 480px) {
  #topbar { gap: 6px; font-size: 13px; }
  #macro { font-size: 0; width: 12px; height: 12px; padding: 0; border-radius: 50%; background: #9c917f; }
  #macro.boom { background: var(--good); } #macro.recession { background: var(--bad); }
}
#pausebtn.paused { background: var(--gold); color: #1a1713; border-color: var(--gold); }

/* toast (latest log line) */
#log { position: absolute; left: 8px; right: 8px; bottom: calc(var(--peek) + var(--sab) + 8px); pointer-events: none; font-size: 13px; }
#log div { display: none; }
#log div:last-child { display: block; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px;
  animation: toast 6s forwards; }
@keyframes toast { 0% { opacity: 0; transform: translateY(6px); } 8% { opacity: 1; transform: none; } 80% { opacity: 1; } 100% { opacity: 0; } }
#tooltip { display: none; }

/* bottom sheet */
#sheet { position: absolute; left: 0; right: 0; bottom: 0; background: var(--panel); border-top: 1px solid var(--border);
  border-radius: 14px 14px 0 0; box-shadow: 0 -6px 24px rgba(0,0,0,0.5); display: flex; flex-direction: column;
  height: calc(var(--peek) + var(--sab)); transition: height 0.22s ease; padding-bottom: var(--sab); z-index: 3; }
#sheet.half { height: 52%; }
#sheet.full { height: calc(100% - var(--top) - var(--sat) - 6px); }
#sheet.dragging { transition: none; }
#sheet.peek #sheetbody { visibility: hidden; }
#sheethandle { padding: 8px 14px 4px; text-align: center; touch-action: none; cursor: grab; }
.grip { width: 40px; height: 4px; border-radius: 2px; background: var(--border); margin: 0 auto 6px; }
#sheettitle { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#tabs { display: flex; gap: 4px; padding: 0 8px 6px; }
#tabs button { flex: 1; padding: 6px 2px; min-height: 36px; font-size: 13px; border-radius: 8px; }
#sheetbody { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 14px 16px; overscroll-behavior: contain; }
#sheetbody section { display: none; }
#sheetbody section.active { display: block; }

/* selection content (shared markup from ui.js) */
#selbody h4 { margin: 4px 0 4px; font-size: 17px; }
#selbody table { border-collapse: collapse; width: 100%; margin: 6px 0; }
#selbody td { padding: 5px 4px; border-bottom: 1px solid rgba(255,255,255,0.06); }
#selbody td:last-child { text-align: right; }
#selbody details { margin: 6px 0; }
#selbody summary { padding: 8px 0; cursor: pointer; color: var(--gold); }
.cargo-filter label { display: inline-flex; align-items: center; gap: 5px; margin: 4px 10px 4px 0; min-height: 34px; }
.cargo-filter input { width: 20px; height: 20px; min-height: 0; padding: 0; }
.ledger-item { display: flex; justify-content: space-between; padding: 10px 6px; border-bottom: 1px solid rgba(255,255,255,0.06); }
.opp { display: grid; grid-template-columns: auto 1fr auto; gap: 6px 8px; font-size: 14px; align-items: center; }
.opp .to { color: var(--muted); }
#loglist div { padding: 3px 0; border-bottom: 1px solid rgba(255,255,255,0.05); }

/* tutorial card */
#tutorial { position: absolute; left: 8px; right: 8px; top: calc(var(--top) + var(--sat) + 8px); background: var(--panel); border: 1px solid var(--gold);
  border-radius: 10px; padding: 10px 12px; box-shadow: 0 4px 18px rgba(0,0,0,0.5); z-index: 5; font-size: 14px; }
.tut-head { display: flex; justify-content: space-between; align-items: center; color: var(--gold); font-size: 11px;
  text-transform: uppercase; letter-spacing: 1px; margin-bottom: 4px; }
#tut-text { margin-bottom: 6px; }
#tut-text b { color: var(--gold); }

/* help */
#help { position: absolute; inset: 0; background: rgba(0,0,0,0.65); display: flex; align-items: flex-end; justify-content: center; z-index: 10; }
.helpbox { background: var(--panel); border: 1px solid var(--border); width: 100%; max-height: 88%; overflow-y: auto; padding: 16px 18px calc(16px + var(--sab));
  border-radius: 14px 14px 0 0; font-size: 14px; }
.helpbox h2 { margin-top: 0; color: var(--gold); }
.helpbox ul { padding-left: 18px; }

/* ---- wide screens: the sheet docks as a right-hand panel and the map gets the rest ---- */
@media (min-width: 900px) {
  :root { --panelw: 380px; }
  html, body { font-size: 14px; }
  #topbar { padding-left: 12px; gap: 12px; }
  #cash { margin-left: 12px; }
  #speedbtn { margin-left: auto; }
  #sheet { top: calc(var(--top) + var(--sat)); left: auto; right: 0; width: var(--panelw); height: auto !important; border-radius: 0;
    border-top: none; border-left: 1px solid var(--border); box-shadow: none; transition: none; }
  #sheet.peek #sheetbody { visibility: visible; }
  #sheethandle { cursor: default; padding-top: 10px; }
  .grip { display: none; }
  #tabs button { font-size: 13px; }
  #log { left: 12px; right: calc(var(--panelw) + 12px); bottom: 12px; width: auto; max-width: 520px; }
  #tutorial { left: 12px; right: auto; width: 360px; }
  #help { align-items: center; }
  .helpbox { width: auto; max-width: 560px; border-radius: 10px; padding: 18px 24px; }
  button:active { transform: translateY(1px); }
}

/* left drawer: game and settings */
#drawerbg { position: absolute; inset: 0; background: rgba(0,0,0,0.5); z-index: 6; }
#drawer { position: absolute; top: 0; bottom: 0; left: 0; width: min(340px, 86vw); background: var(--panel); border-right: 1px solid var(--border);
  box-shadow: 6px 0 24px rgba(0,0,0,0.5); z-index: 7; padding: calc(var(--sat) + 10px) 16px calc(var(--sab) + 16px); overflow-y: auto;
  transform: translateX(0); transition: transform 0.2s ease; }
#drawer.hidden, #drawerbg.hidden { display: block !important; pointer-events: none; }
#drawer.hidden { transform: translateX(-105%); }
#drawerbg.hidden { opacity: 0; }
#drawerbg { transition: opacity 0.2s; }
.drawer-head { display: flex; justify-content: space-between; align-items: center; font-weight: 700; color: var(--gold); font-size: 16px; margin-bottom: 8px; }
#worthchart svg { width: 100%; height: 64px; display: block; }

/* ---- start screen and world-building progress ---- */
#start, #loading { position: absolute; inset: 0; background: rgba(0,0,0,0.72); display: flex; align-items: center; justify-content: center; z-index: 12; overflow-y: auto; padding: 16px; box-sizing: border-box; }
#start.hidden, #loading.hidden { display: none; }
.startbox, .loadbox { margin: auto; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 22px 26px; width: min(420px, calc(100vw - 32px)); box-shadow: 0 8px 32px rgba(0,0,0,0.6); }
.startbox h1 { margin: 0 0 6px; color: var(--gold); font-size: 26px; letter-spacing: 0.5px; }
.startbox h3 { margin-top: 18px; }
.startbox .wide { display: block; width: 100%; margin-top: 10px; padding: 10px; font-size: 16px; }
.startbox .btnrow { margin-top: 6px; }
.startbox .btnrow { display: grid; grid-template-columns: 1fr 1fr 1fr; }
.startbox .btnrow select, .startbox .btnrow input { width: 100%; min-width: 0; box-sizing: border-box; padding-right: 4px; }
.startbox .small { margin-top: 8px; }
#load-title { color: var(--gold); font-weight: 700; font-size: 18px; margin-bottom: 4px; }
#load-phase { min-height: 1.4em; margin-bottom: 10px; }
.bar { height: 10px; background: var(--panel2); border: 1px solid var(--border); border-radius: 5px; overflow: hidden; }
#load-fill { height: 100%; width: 0; background: var(--gold); transition: width 0.15s linear; }
