:root {
  --ink: #12101c;
  --panel: rgba(20, 17, 33, 0.92);
  --panel2: rgba(38, 32, 58, 0.9);
  --line: rgba(232, 200, 130, 0.26);
  --gold: #e8c882;
  --text: #ece6d8;
  --dim: #a59fb4;
  --good: #7fe0a0;
  --bad: #ff8070;
  --side: 232px;
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #05050c; color: var(--text); user-select: none;
  font: 13px/1.35 "Segoe UI", system-ui, -apple-system, sans-serif; }
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
.dim { color: var(--dim); }
.grow { flex: 1; }
canvas#gl, canvas#overlay { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
canvas#overlay { pointer-events: none; }

#side { position: fixed; left: 0; top: 0; bottom: 0; width: var(--side); background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 6px; padding: 8px; overflow: hidden; }
#mapWrap { display: flex; justify-content: center; }
#map { width: 208px; height: 208px; cursor: crosshair; }
#manaBox .row { display: flex; align-items: baseline; gap: 6px; }
#manaBox b { color: var(--gold); font-size: 15px; font-variant-numeric: tabular-nums; }
.label { color: var(--dim); }
#manaRate { margin-left: auto; font-size: 12px; font-variant-numeric: tabular-nums; }
.bar { height: 5px; background: rgba(255, 255, 255, 0.09); border-radius: 3px; overflow: hidden; margin-top: 3px; }
.bar i { display: block; height: 100%; width: 0; background: var(--gold); }
#manaBox.wasted .bar i { background: var(--bad); animation: blink 0.7s infinite; }
#manaBox.wasted b { color: var(--bad); }
@keyframes blink { 50% { opacity: 0.35; } }
#tribeBars { display: grid; gap: 2px; }
.tribeRow { display: grid; grid-template-columns: 56px 1fr 26px; align-items: center; gap: 5px; font-size: 11px; }
.tribeRow .bar { margin: 0; }
.tribeRow .n { text-align: right; font-variant-numeric: tabular-nums; }
.tribeRow.out { opacity: 0.4; text-decoration: line-through; }

#tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; }
#tabs button { background: transparent; border: 0; border-bottom: 2px solid transparent; padding: 5px 0; color: var(--dim); }
#tabs button:hover { color: var(--text); }
#tabs button.on { color: var(--gold); border-bottom-color: var(--gold); }
#panel { flex: 1; min-height: 0; overflow-y: auto; scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
.hint { color: var(--dim); font-size: 11px; margin: 6px 2px 0; }

#spellGrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.spell { position: relative; aspect-ratio: 1; padding: 0; background: var(--panel2); border: 1px solid var(--line); border-radius: 5px; color: var(--text); overflow: hidden; }
.spell svg { width: 66%; height: 66%; display: block; margin: 8% auto 0; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.spell:hover { border-color: var(--gold); }
.spell.empty { color: #76708a; }
.spell.off { opacity: 0.5; background: #15131f; }
.spell.active { border-color: #fff; box-shadow: 0 0 0 2px var(--gold) inset; color: var(--gold); }
.spell .pips { position: absolute; right: 3px; top: 1px; font-size: 11px; font-weight: 700; color: var(--gold); }
.spell .prog { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--good); }

.listBtn { display: flex; align-items: center; gap: 7px; width: 100%; text-align: left; background: var(--panel2); border: 1px solid var(--line); border-radius: 5px; padding: 6px 8px; margin-bottom: 4px; }
.listBtn:hover { border-color: var(--gold); }
.listBtn.active { border-color: #fff; box-shadow: 0 0 0 1px var(--gold) inset; color: var(--gold); }
.listBtn:disabled { opacity: 0.45; cursor: default; }
.listBtn .swatch { width: 10px; height: 10px; border-radius: 50%; background: var(--gold); flex: none; }
.listBtn .n { margin-left: auto; color: var(--gold); font-variant-numeric: tabular-nums; }
.listBtn small { color: var(--dim); }

#selBox { min-height: 56px; border-top: 1px solid var(--line); padding-top: 6px; font-size: 12px; }
#selBox h4 { margin: 0 0 3px; color: var(--gold); font-size: 13px; font-weight: 600; }
#selBox .chips { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 4px; }
#selBox .chips button { padding: 1px 6px; background: var(--panel2); border: 1px solid var(--line); border-radius: 3px; font-size: 11px; }
#selBox .chips button:hover { border-color: var(--gold); }

#topBar { position: fixed; left: var(--side); right: 0; top: 0; display: flex; align-items: center; gap: 12px; padding: 5px 10px;
  background: linear-gradient(rgba(10, 8, 20, 0.8), rgba(10, 8, 20, 0)); pointer-events: none; }
#topBar button { pointer-events: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 4px; padding: 2px 9px; }
#topBar button:hover { border-color: var(--gold); }
#levelName { color: var(--gold); font: 600 15px Georgia, "Times New Roman", serif; }
#clock, #speed { font-variant-numeric: tabular-nums; }

#stepText { position: fixed; left: calc(var(--side) + 14px); top: 40px; max-width: 520px; padding: 9px 13px; background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--gold); border-radius: 4px; pointer-events: none; }
#notices { position: fixed; left: calc(var(--side) + (100% - var(--side)) / 2); transform: translateX(-50%); bottom: 42px; display: flex; flex-direction: column; align-items: center; gap: 4px; pointer-events: none; }
.notice { padding: 4px 13px; background: var(--panel); border-radius: 4px; border: 1px solid var(--line); animation: rise 0.2s ease-out; max-width: 560px; text-align: center; }
.notice.good { color: var(--good); }
.notice.bad { color: var(--bad); }
@keyframes rise { from { transform: translateY(8px); opacity: 0; } }
#hoverInfo { position: fixed; left: calc(var(--side) + 12px); bottom: 10px; padding: 3px 10px; background: var(--panel); border-radius: 4px; pointer-events: none; font-size: 12px; }
#pausedTag { position: fixed; top: 18%; left: calc(var(--side) + (100% - var(--side)) / 2); transform: translateX(-50%); font: 600 32px Georgia, serif; color: var(--gold); text-shadow: 0 2px 10px #000; pointer-events: none; }

#tip { position: fixed; z-index: 30; max-width: 280px; padding: 7px 10px; background: #0d0b18; border: 1px solid var(--gold); border-radius: 4px; pointer-events: none; font-size: 12px; }
#tip b { color: var(--gold); display: block; }
#tip em { color: var(--dim); font-style: normal; display: block; margin-top: 3px; }

#screen { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; padding: 24px; background: radial-gradient(ellipse at center, rgba(6, 5, 16, 0.25), rgba(6, 5, 16, 0.75)); }
#screen.flat { background: rgba(6, 5, 16, 0.62); }
#card { width: min(640px, 100%); max-height: 100%; overflow-y: auto; padding: 26px 32px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
#card.wide { width: min(900px, 100%); }
#card.title { background: transparent; border: 0; text-align: center; width: min(560px, 100%); overflow: visible; }
#card.title .menuBtn { width: min(340px, 100%); margin-left: auto; margin-right: auto; }
#card h1 { margin: 0; font: 600 60px Georgia, "Times New Roman", serif; letter-spacing: 0.05em; color: var(--gold); text-shadow: 0 3px 24px rgba(0, 0, 0, 0.8); }
#card h2 { margin: 0 0 10px; font: 600 27px Georgia, "Times New Roman", serif; color: var(--gold); }
#card h3 { margin: 18px 0 6px; font: 600 16px Georgia, serif; color: var(--gold); }
#card p { margin: 0 0 11px; font-size: 14px; }
#card .tag { color: var(--text); text-shadow: 0 2px 8px #000; margin-bottom: 24px; font-size: 15px; }
.menuBtn { display: block; width: 100%; padding: 10px 14px; margin-bottom: 7px; background: var(--panel2); border: 1px solid var(--line); border-radius: 5px; font-size: 15px; text-align: left; }
#card.title .menuBtn { text-align: center; background: var(--panel); }
.menuBtn:hover:not(:disabled) { border-color: var(--gold); color: var(--gold); }
.menuBtn:disabled { opacity: 0.4; cursor: default; }
.menuBtn small { display: block; color: var(--dim); font-size: 12px; }
.primary { background: #6a4f1c; border-color: var(--gold); text-align: center; }
#card.title .primary { background: #6a4f1c; }
.btnRow { display: flex; gap: 8px; margin-top: 12px; }
.btnRow .menuBtn { margin: 0; text-align: center; }
.form { display: grid; grid-template-columns: 110px 1fr; gap: 10px 12px; align-items: center; margin: 12px 0; }
.form input, .form select { background: #0d0b18; color: var(--text); border: 1px solid var(--line); border-radius: 4px; padding: 6px 8px; font: inherit; width: 100%; }
.keys { display: grid; grid-template-columns: 190px 1fr; gap: 3px 12px; font-size: 13px; }
.keys b { color: var(--gold); font-weight: 600; }
.ency { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(255px, 100%), 1fr)); gap: 8px; }
.ency div { padding: 8px 10px; background: var(--panel2); border-radius: 5px; font-size: 12px; }
.ency b { color: var(--gold); display: block; font-size: 13px; }
.ency small { color: var(--dim); display: block; margin-top: 3px; }
.goal { color: var(--good); }

#fatal { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 40px; background: #0a0914; font-size: 16px; text-align: center; }

/* ---------------------------------------------------------------- touch */

/* The game handles every gesture on the view itself. Lists and cards may still scroll. */
html, body { touch-action: none; overscroll-behavior: none; -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; -webkit-user-select: none; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
#panel, #card, #sheet { touch-action: pan-y; overscroll-behavior: contain; }
button, select, input { touch-action: manipulation; }
#map { touch-action: none; }

.onlyTouch { display: none; }
body.touch .onlyMouse { display: none; }
body.touch p.onlyTouch { display: block; }

#actions { gap: 4px; flex-wrap: wrap; }
body.touch #actions { display: flex; }
#actions button { flex: 1 1 0; min-width: 46px; min-height: 44px; padding: 2px 1px; font-size: 12px; background: var(--panel2); border: 1px solid var(--line); border-radius: 5px; }
#actions button.lit { border-color: #fff; color: var(--gold); box-shadow: 0 0 0 1px var(--gold) inset; }
#actions button.primary { background: #6a4f1c; border-color: var(--gold); font-weight: 600; }
#actions button:disabled { opacity: 0.45; }

#groupRow { grid-template-columns: repeat(6, 1fr); gap: 4px; margin-top: 2px; }
body.touch #groupRow { display: grid; }
#groupRow button { min-height: 44px; padding: 0; background: #15131f; border: 1px solid var(--line); border-radius: 5px; color: var(--dim); }
#groupRow button.has { background: var(--panel2); border-color: var(--gold); color: var(--gold); }

.cancelPlan { margin-top: 5px; padding: 3px 9px; background: var(--panel2); border: 1px solid var(--line); border-radius: 4px; }
.cancelPlan:hover { border-color: var(--gold); }

#sheet { position: fixed; z-index: 25; left: 50%; transform: translateX(-50%); bottom: calc(var(--dockB, 0px) + var(--sab) + 8px); width: min(340px, calc(100vw - 16px)); max-height: 80vh; max-height: 80dvh; overflow-y: auto; padding: 11px 13px; background: #0d0b18; border: 1px solid var(--gold); border-radius: 8px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.6); }
.sheetBody b { display: block; color: var(--gold); font-size: 14px; }
.sheetBody em { display: block; margin-top: 3px; color: var(--dim); font-style: normal; font-size: 12px; }
.sheetBtns { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 9px; }
.sheetBtns button { flex: 1 1 40%; min-height: 44px; padding: 2px 6px; background: var(--panel2); border: 1px solid var(--line); border-radius: 5px; }

body.touch #tabs button { min-height: 42px; }
body.touch .listBtn { min-height: 44px; }
body.touch .menuBtn { min-height: 48px; }
body.touch #selBox .chips button, body.touch .cancelPlan { min-height: 38px; padding-left: 10px; padding-right: 10px; }
body.touch #topBar button { min-width: 44px; min-height: 38px; }
body.touch #map { cursor: default; }
body.touch .form input, body.touch .form select { font-size: 16px; min-height: 42px; } /* iOS zooms into anything smaller */
body.touch .spell:hover, body.touch .listBtn:hover, body.touch #actions button:hover { border-color: var(--line); }

/* ---------------------------------------------------------------- small screens: keep in step with COMPACT in js/ui.js */

@media (max-width: 820px), (max-height: 520px) {
  :root { --side: 0px; --mapSize: 104px; --barTop: calc(44px + var(--sat)); }
  html, body { height: 100vh; height: 100dvh; }

  #side { top: auto; right: 0; width: auto; max-height: 62vh; max-height: 62dvh; gap: 4px; padding: 5px calc(8px + var(--sar)) calc(5px + var(--sab)) calc(8px + var(--sal)); border-right: 0; border-top: 1px solid var(--line); }
  #panel { flex: 0 1 auto; max-height: 34vh; max-height: 34dvh; }
  #panel.collapsed { display: none; }
  #spellGrid { grid-template-columns: repeat(6, 1fr); }
  .hint { margin-top: 4px; }

  /* The map, mana and tribe rows float over the view, and only the map takes touches. */
  #mapWrap { position: fixed; left: calc(6px + var(--sal)); top: var(--barTop); }
  #map { width: var(--mapSize); height: var(--mapSize); }
  #manaBox, #tribeBars { position: fixed; left: calc(6px + var(--sal)); width: var(--mapSize); padding: 2px 5px; background: var(--panel); border-radius: 4px; pointer-events: none; font-size: 11px; }
  #manaBox { top: calc(var(--barTop) + var(--mapSize) + 3px); }
  #manaBox b { font-size: 13px; }
  #manaRate { font-size: 10px; }
  #tribeBars { top: calc(var(--barTop) + var(--mapSize) + 36px); gap: 1px; }
  .tribeRow { grid-template-columns: 36px 1fr 18px; gap: 3px; font-size: 10px; }
  .tribeRow span:first-child { overflow: hidden; white-space: nowrap; }

  #selBox { min-height: 0; border-top: 0; padding-top: 0; font-size: 12px; }
  #selBox h4 { display: inline; margin-right: 6px; font-size: 12px; }
  #selBox .dim { display: inline; }
  #selBox .idle { display: none; }

  #topBar { left: 0; gap: 8px; padding: calc(4px + var(--sat)) calc(6px + var(--sar)) 4px calc(6px + var(--sal)); }
  #levelName { display: none; }
  #stepText { left: calc(var(--mapSize) + 12px + var(--sal)); right: calc(var(--dockR, 0px) + 6px + var(--sar)); top: var(--barTop); max-width: none; padding: 5px 8px; font-size: 12px; }
  #notices { left: calc((100% - var(--dockR, 0px)) / 2); bottom: calc(var(--dockB, 0px) + 38px + var(--sab)); width: calc(100% - var(--dockR, 0px) - 16px); font-size: 12px; }
  .notice { max-width: 100%; padding: 3px 9px; }
  #hoverInfo { left: calc(6px + var(--sal)); bottom: calc(var(--dockB, 0px) + 8px + var(--sab)); max-width: calc(100% - var(--dockR, 0px) - 12px); }
  #pausedTag { left: calc((100% - var(--dockR, 0px)) / 2); font-size: 26px; }

  #screen { padding: calc(8px + var(--sat)) calc(8px + var(--sar)) calc(8px + var(--sab)) calc(8px + var(--sal)); }
  #card { padding: 16px; }
  #card.title { overflow-y: auto; max-height: 100%; padding: 4px; }
  #card h1 { font-size: 40px; }
  #card h2 { font-size: 22px; }
  #card .tag { margin-bottom: 14px; font-size: 14px; }
  .keys { grid-template-columns: 1fr; gap: 1px; }
  .keys span { margin-bottom: 7px; }
  .form { grid-template-columns: 90px 1fr; }
}

/* A phone on its side has no height to spare, so the dock moves to the right edge. */
@media (max-height: 520px) and (orientation: landscape) {
  :root { --mapSize: 88px; --barTop: calc(42px + var(--sat)); }
  #side { left: auto; right: 0; bottom: 0; width: calc(190px + var(--sar)); max-height: calc(100vh - var(--barTop)); max-height: calc(100dvh - var(--barTop)); border-top: 1px solid var(--line); border-left: 1px solid var(--line); border-radius: 6px 0 0 0; }
  #panel { max-height: none; }
  #spellGrid { grid-template-columns: repeat(3, 1fr); }
  #actions button { min-width: 50px; min-height: 40px; }
  body.touch #tabs button { min-height: 36px; font-size: 12px; }
  #tabs { order: -1; }
  #selBox, #actions { order: 3; }
  #card h1 { font-size: 30px; }
  #card .tag { margin-bottom: 8px; }
  body.touch .menuBtn { min-height: 42px; padding-top: 6px; padding-bottom: 6px; }
}
