:root {
  --ink: #30281e;
  --muted: #645745;
  --paper: #fff4df;
  --linen: #f4e5cb;
  --edge: #bca37a;
  --river: #245e5a;
  --river-dark: #174541;
  --light-river: #d5e6d9;
  --gold: #b66e1c;
  --red: #943a32;
  --shadow: 0 3px 0 #49361d34, 0 8px 20px #382b1929;
  --left: max(12px, env(safe-area-inset-left));
  --right: max(12px, env(safe-area-inset-right));
  --top: max(12px, env(safe-area-inset-top));
  --bottom: max(12px, env(safe-area-inset-bottom));
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #e2c48e; }
body { font: 600 14px/1.35 "Trebuchet MS", "Avenir Next", sans-serif; color: var(--ink); user-select: none; }
[hidden] { display: none !important; }
.symbols { position: absolute; width: 0; height: 0; overflow: hidden; }
svg { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.65; stroke-linecap: round; stroke-linejoin: round; }
button, summary { touch-action: manipulation; }
button:focus-visible, summary:focus-visible, a:focus-visible, input:focus-visible { outline: 3px solid #137e77; outline-offset: 2px; }
#view { position: fixed; inset: 0; width: 100%; height: 100%; display: block; cursor: pointer; touch-action: none; }
#view.placing { cursor: crosshair; }
#view.panning { cursor: grabbing; }
.panel { background: var(--paper); border: 1px solid var(--edge); border-radius: 12px; box-shadow: var(--shadow); }
.eyebrow { margin: 0; color: var(--muted); font-size: 10px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; }
.ui-button, #accountPanel button {
  min-height: 44px; padding: 7px 12px; display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: 1px solid var(--edge); border-radius: 9px; background: var(--paper); color: var(--ink); cursor: pointer; font: inherit;
}
.ui-button:hover:not(:disabled), #accountPanel button:hover:not(:disabled) { background: #f8e8cb; }
.ui-button:active:not(:disabled), #accountPanel button:active:not(:disabled) { transform: translateY(1px); }
.ui-button.primary { background: var(--river); border-color: var(--river-dark); color: #fff8e8; }
.ui-button.primary:hover:not(:disabled) { background: var(--river-dark); }
.ui-button.quiet { background: transparent; border-color: transparent; }
.ui-button.icon-button { min-width: 44px; padding: 7px; }
.ui-button:disabled, #accountPanel button:disabled { opacity: .55; cursor: default; }
.danger, #accountPanel .danger { color: var(--red); }

#hud {
  position: fixed; top: var(--top); left: var(--left); right: var(--right); z-index: 5;
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(260px, 360px) minmax(0, 1fr);
  gap: 8px 16px; align-items: start; pointer-events: none;
}
#hud > * { pointer-events: auto; }
.hud-identity { display: flex; align-items: flex-start; gap: 8px; min-width: 0; width: fit-content; }
.wordmark {
  background: var(--river); border: 1px solid var(--river-dark); color: #fff8e8; padding: 7px 12px;
  border-radius: 10px; font: 700 23px/1.2 Georgia, serif; letter-spacing: -.04em; box-shadow: var(--shadow);
}
#stats { display: flex; align-items: center; min-height: 44px; padding: 3px 4px; font-variant-numeric: tabular-nums; }
.stat { display: inline-flex; align-items: center; justify-content: center; gap: 5px; padding: 0 6px; white-space: nowrap; }
.stat + .stat { border-inline-start: 1px solid #e4d3b7; }
.stat svg { width: 16px; height: 16px; color: var(--river); }
.stat:first-child svg { color: var(--gold); }
.day-clock { display: inline-grid; }
.day-clock svg { grid-area: 1 / 1; }
.day-icon { opacity: calc(1 - var(--night, 0)); }
.night-icon { opacity: var(--night, 0); }
#crewToggle { min-height: 36px; border: 0; border-inline-start: 1px solid #e4d3b7; background: transparent; color: inherit; font: inherit; cursor: pointer; }
#crewToggle:hover { background: var(--light-river); }
#goal { padding: 8px 11px; min-width: 0; }
.journal-mission { display: flex; align-items: baseline; gap: 8px; }
.journal-mission .eyebrow { flex: none; }
#goalText { min-width: 0; }
#goal .bar { height: 4px; border-radius: 4px; background: #e5d5b9; margin-top: 5px; overflow: hidden; }
#goal .fill { height: 100%; width: 0; background: var(--river); transition: width .2s; }
#season { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 4px; font-size: 11px; color: var(--muted); }
#season > span:first-child { display: inline-flex; align-items: center; gap: 5px; color: var(--river); }
#season svg { width: 14px; height: 14px; }
#seasonNext { white-space: nowrap; }
#weather { display: flex; align-items: center; gap: 5px; margin-top: 4px; padding-top: 4px;
  border-top: 1px solid #e4d3b7; font-size: 12px; color: #78501e; }
#weather[data-severity="3"], #weather[data-severity="4"] { color: var(--red); }
#weather svg { width: 15px; height: 15px; }
#weatherImpact { display: block; font-size: 11px; }
#weatherTime { margin-inline-start: auto; white-space: nowrap; font-variant-numeric: tabular-nums; }
#townMenu { position: relative; z-index: 1; justify-self: end; }
#townMenu > summary { list-style: none; box-shadow: var(--shadow); }
#townMenu > summary::-webkit-details-marker { display: none; }
#townMenu[open] > summary { background: var(--light-river); }
.menu-panel { position: absolute; top: calc(100% + 6px); right: 0; width: max-content; min-width: 150px; padding: 5px; display: grid; }
.menu-panel .ui-button { justify-content: flex-start; }

#controls {
  position: fixed; left: var(--left); right: var(--right); bottom: var(--bottom); z-index: 6;
  display: flex; justify-content: space-between; align-items: end; gap: 8px; pointer-events: none;
}
#townInfo { width: min(100%, 350px); min-width: 0; display: grid; justify-items: start; gap: 7px; }
#townInfo > *, #cameraControls { pointer-events: auto; }
#dock, #cameraControls { display: flex; gap: 5px; }
#dock .ui-button, #cameraControls .ui-button { box-shadow: var(--shadow); }
#dock .ui-button { padding-inline: 13px; }
#dock button[aria-expanded="true"], #dock button[aria-pressed="true"] { outline: 2px solid var(--river); outline-offset: 2px; }
#cameraControls { flex-direction: column; }
#work, #placement { width: 100%; }
#work { padding: 8px 11px 10px; max-height: min(54dvh, 280px); overflow: auto; }
#work header, #buildPanel header { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#workTitle { display: block; font: 700 19px/1.15 Georgia, serif; }
#work p:not(.eyebrow) { margin: 4px 0; color: var(--muted); font-size: 13px; }
#work progress { width: 100%; height: 9px; accent-color: var(--river); }
.work-actions { display: flex; flex-wrap: wrap; gap: 5px; }
.work-actions:has(button:not([hidden])) { margin-top: 7px; }
.work-actions .ui-button { flex: 1 1 auto; font-size: 13px; }
#hint { padding: 5px 9px; font-size: 12px; pointer-events: none; }
#placement { padding: 4px 5px 4px 10px; display: flex; align-items: center; gap: 4px; }
#placement > div { flex: 1; min-width: 0; }
#placementTitle, #placementHint { display: block; }
#placementHint { font-size: 11px; color: var(--muted); }
#placementZone { display: block; color: var(--river); font-size: 11px; }
#placementZone::before { content: ''; display: inline-block; width: 12px; margin-right: 4px; vertical-align: middle; border-top: 2px dashed currentColor; }
#placement .ui-button { padding-inline: 6px; font-size: 13px; }

#buildPanel {
  position: fixed; z-index: 8; left: var(--left); bottom: calc(var(--bottom) + 52px);
  width: min(550px, calc(100vw - var(--left) - var(--right))); max-height: min(64dvh, 490px);
  display: flex; flex-direction: column; padding: 8px 12px 12px;
}
#buildPanel h2, #away h2, #accountPanel h2, #projectsPanel h2, #buildingPicker h2 { margin: 0; font: 700 24px/1.15 Georgia, serif; }
#build { display: grid; grid-template-columns: repeat(auto-fit, minmax(86px, 1fr)); gap: 6px; overflow: auto; padding: 3px; }
#build .build-card {
  position: relative; min-width: 0; min-height: 72px; display: grid; grid-template-rows: auto 1fr auto;
  justify-items: center; align-items: center; gap: 2px; padding: 5px 3px; border-color: #dfcaaa; background: #f8ead2;
}
#build .build-card:hover:not(:disabled) { border-color: var(--river); }
#build .build-card[aria-pressed="true"] { border-color: var(--river); background: var(--light-river); }
#build .build-card > svg { color: var(--river); width: 24px; height: 24px; }
#build .palace-card { border-color: var(--edge); background: #f0dfbd; }
#build .name { text-align: center; line-height: 1.15; font-size: 12px; }
#build .key { position: absolute; top: 5px; right: 6px; color: var(--muted); font-size: 9px; }
#build .cost { color: var(--muted); display: flex; align-items: center; gap: 3px; font-size: 11px; font-variant-numeric: tabular-nums; }
#build .cost svg { width: 12px; height: 12px; color: var(--gold); }
#build .poor .cost { color: var(--red); }
#build .build-card:disabled .cost svg { color: var(--muted); }
#build .build-card:disabled .price { display: none; }
#build .build-card:not(:disabled) .locked, #build .build-card:disabled .coin-mark { display: none; }

#toasts { position: absolute; top: calc(100% + 8px); left: 50%; transform: translateX(-50%);
  max-width: calc(100vw - 24px); width: max-content; display: grid; justify-items: center; gap: 5px; pointer-events: none; }
.toast { padding: 7px 12px; text-align: center; animation: toast 3.6s forwards; }
.toast.big { font-weight: 700; border-color: var(--gold); }
.toast.storm { border-color: var(--red); }
@keyframes toast { 0% { opacity: 0; transform: translateY(-5px); } 8%, 85% { opacity: 1; transform: none; } 100% { opacity: 0; } }
.float { position: fixed; pointer-events: none; color: #fff6d0; text-shadow: 0 2px 0 #8a5a14, 0 0 6px #8a5a14;
  animation: rise 1.1s ease-out forwards; transform: translate(-50%, -50%); font-size: 18px; }
.float.bonus { font-size: 22px; color: #fff2a8; }
.float.dig { color: #fff; }
@keyframes rise { to { opacity: 0; margin-top: -46px; } }

#accountPanel, #away { position: fixed; z-index: 12; width: min(385px, calc(100vw - var(--left) - var(--right)));
  max-height: calc(100dvh - var(--top) - var(--bottom) - 54px); overflow: auto; padding: 14px; }
#accountPanel { top: calc(var(--top) + 51px); right: var(--right); font-weight: 500; user-select: text; }
#accountPanel h3 { margin: 12px 0 4px; font-size: 14px; }
#accountPanel p { margin: 5px 0; }
#accountPanel .status { color: var(--muted); }
#accountPanel .code, #accountPanel form, #accountPanel footer { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 7px; }
#accountPanel code, #accountPanel input { min-width: 0; flex: 1 1 150px; padding: 8px; border: 1px solid var(--edge); border-radius: 8px; background: #fffbef; }
#accountPanel input { font: inherit; font-size: 16px; text-transform: uppercase; }
#accountPanel code { font: 700 15px/1.4 monospace; overflow-wrap: anywhere; }
#accountPanel a { color: var(--river); display: inline-flex; align-items: center; min-height: 44px; }
#accountPanel .link { background: transparent; border-color: transparent; text-decoration: underline; }
#accountPanel .error { color: var(--red); }
#accountPanel .choice { border-radius: 8px; background: var(--light-river); padding: 8px; }
#accountPanel .choice button { margin: 3px 4px 0 0; }
#accountPanel footer { align-items: center; }
#accountPanel [data-act="close"] { margin-left: auto; }
#away { z-index: 13; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center; }
#away .ui-button { margin-top: 6px; }
#away p { margin: 5px 0; }
#awayProject { padding-top: 6px; border-top: 1px solid #e4d3b7; color: var(--river-dark); }

#projectsPanel {
  color: var(--ink); width: min(420px, calc(100vw - var(--left) - var(--right)));
  max-height: calc(100dvh - var(--top) - var(--bottom)); padding: 12px; overflow: auto;
}
#projectsPanel::backdrop { background: #30281e66; }
#projectsPanel header, #buildingPicker header { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
#buildingPicker {
  position: fixed; inset: auto auto var(--bottom) var(--left); margin: 0; color: var(--ink);
  width: min(330px, calc(100vw - var(--left) - var(--right))); max-height: min(54dvh, 340px); padding: 10px; overflow: auto;
}
#buildingPicker::backdrop { background: #30281e33; }
#buildingChoices { list-style: none; margin: 7px 0 0; padding: 2px; display: grid; gap: 4px; }
#buildingChoices .ui-button { width: 100%; justify-content: space-between; text-align: left; }
#buildingChoices small { flex: none; color: var(--muted); }
#crewSummary { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); margin: 12px 0 6px; padding: 6px 0;
  background: var(--light-river); border-radius: 8px; color: var(--river-dark); text-align: center; }
#crewSummary > div + div { border-inline-start: 1px solid #a9c7b4; }
#crewSummary dt { font-size: 11px; }
#crewSummary dd { margin: 0; font: 700 21px/1.2 Georgia, serif; }
#projectsNote, #projectsEmpty { color: var(--muted); font-size: 12px; margin: 6px 0; }
#projectList { list-style: none; padding: 0 2px 2px; margin: 0; display: grid; gap: 5px; }
.project-row { display: grid; justify-items: stretch; gap: 3px; width: 100%; text-align: left; }
.project-detail { color: var(--muted); font-size: 12px; }
.project-progress { display: block; height: 4px; border-radius: 4px; background: #e5d5b9; overflow: hidden; }
.project-progress span { display: block; height: 100%; background: var(--river); }

@media (max-width: 1200px) { .wordmark { display: none; } }
@media (max-width: 900px) {
  #hud { grid-template-columns: minmax(0, 1fr) auto; gap: 6px; }
  #goal { grid-column: 1 / -1; grid-row: 2; }
  #townMenu { grid-column: 2; grid-row: 1; }
  #buildPanel { width: calc(100vw - var(--left) - var(--right)); }
  #build { grid-template-columns: repeat(auto-fit, minmax(84px, 1fr)); }
}
@media (max-width: 360px) {
  :root { --left: max(8px, env(safe-area-inset-left)); --right: max(8px, env(safe-area-inset-right)); }
  #townMenu summary span, .day-clock { display: none; }
  #dock .ui-button { padding-inline: 10px; }
  .stat { padding-inline: 5px; }
}
@media (pointer: coarse) { #build .key { display: none; } }
@media (max-height: 530px) and (orientation: landscape) {
  #cameraControls { flex-direction: row; }
  .day-clock, #townMenu summary span { display: none; }
}
@media (min-width: 650px) and (max-height: 530px) and (orientation: landscape) {
  #hud { grid-template-columns: minmax(0, 1fr) minmax(220px, 310px) auto; }
  #goal { grid-column: 2; grid-row: 1; }
  #townMenu { grid-column: 3; grid-row: 1; }
}
@media (prefers-reduced-motion: reduce) {
  #goal .fill { transition: none; }
  .toast, .float { animation: none; }
}
