:root {
  color: #1d1914;
  background: #c5d7e2;
  font-family: "Segoe UI", "Helvetica Neue", sans-serif;
  --ink: #1d1914;
  --muted: #6e6256;
  --paper: rgba(246, 240, 228, 0.94);
  --paper-solid: #f6f0e4;
  --line: rgba(29, 25, 20, 0.12);
  --seal: #d63b4a;
  --moss: #1f8a52;
  --gold: #e0a100;
  --good: #1f9d5a;
  --sea: #2b7fd4;
  --berry: #d23b86;
  --plum: #6d4cc4;
  --clay: #e07a32;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body { user-select: none; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
#view { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
body:not(.ready) #view,
body:not(.ready) header,
body:not(.ready) #dock,
body:not(.ready) #ledger,
body:not(.ready) #zoom,
body:not(.ready) #compass,
body:not(.ready) #soloBtn { pointer-events: none; }

#app {
  position: fixed; inset: 0; z-index: 2;
  display: grid; grid-template-rows: auto minmax(0, 1fr);
  gap: 10px; padding: 12px;
  pointer-events: none;
}
header, #dock, #ledger, #zoom, #compass, #soloBtn, #hint, #toast, #coach, #guide, .modal { pointer-events: auto; }

header {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 8px 10px 8px 12px;
  background: linear-gradient(105deg, #ffe9a8 0%, #ffd0c4 32%, #d9f5df 64%, #d4e8ff 100%);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 18px;
  box-shadow: 0 10px 28px rgba(24, 32, 28, 0.14), inset 0 1px 0 rgba(255, 255, 255, 0.7);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
#miniFlag { width: 54px; height: 36px; border-radius: 4px; box-shadow: 0 0 0 1px rgba(29, 25, 20, 0.18); }
#flagCanvas { width: 100%; height: auto; border-radius: 8px; box-shadow: 0 8px 18px rgba(29, 25, 20, 0.12); }
.brand p { margin: 0; font-size: 0.68rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.brand h1 {
  margin: 0; max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: Georgia, "Palatino Linotype", serif; font-size: 1.45rem; font-weight: 600; line-height: 1.05;
}
#facts { display: flex; gap: 6px; flex-wrap: wrap; }
#facts span { border-radius: 999px; padding: 6px 10px; font-size: 0.82rem; font-weight: 650; }
#factYear { background: #ffd56a; }
#factArea { background: #b7efc5; }
#factPop { background: #ffc2d8; }
#factFood { background: #c9f27a; }
#factGold { background: #ffcf70; }
#factStone { background: #d9d3c7; }
#facts span.warn { background: #ffb4a8; color: #8e2f2a; }
.actions { margin-left: auto; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
#saveState { font-size: 0.75rem; color: var(--muted); padding-right: 4px; }
.actions button, #coach button, #confirmNo {
  border: 0; border-radius: 999px; padding: 8px 12px; background: rgba(29, 25, 20, 0.06);
}
.actions button:hover, #coach button:hover, #confirmNo:hover { background: rgba(29, 25, 20, 0.1); }
#undoBtn:disabled { opacity: 0.4; cursor: default; }

#stage { position: relative; min-height: 0; pointer-events: none; }
#dock, #ledger {
  position: absolute; top: 0; bottom: 0;
  display: flex; flex-direction: column; gap: 8px;
  padding: 12px; overflow: hidden;
  background: var(--paper);
  border: 1px solid var(--line);
  border-top-width: 5px;
  border-radius: 18px;
  box-shadow: 0 16px 36px rgba(24, 32, 28, 0.16), inset 0 1px 0 rgba(255, 255, 255, 0.65);
}
#dock { left: 0; width: 236px; border-top-color: #1f9d5a; background: linear-gradient(180deg, #e5f8ee, var(--paper) 110px); }
#ledger { right: 0; width: 400px; border-top-color: #e07a32; background: linear-gradient(180deg, #fff0e2, var(--paper) 120px); }
#dock h2 { color: #157a45; }
#dock h3 { color: #c4531c; }
#ledger h3 { color: #6d4cc4; }
.dockHead h2, h3, .kicker {
  margin: 0; font-size: 0.72rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); font-weight: 600;
}
#landMix, #buildMix, #neighborMix, #toolHelp, .note, #forecast, #report, #eventText, #bloomText, #confirm p, #worlds p {
  margin: 0; color: var(--muted); font-size: 0.8rem; line-height: 1.35;
}
#toolHelp { color: var(--ink); }
#forecast.warn { color: var(--seal); }
#trade { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
#trade button {
  border-radius: 999px; border: 1px solid var(--line); background: var(--paper);
  padding: 8px 10px; font-size: 0.78rem; font-weight: 650;
}
.census { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; }
.census label { display: grid; gap: 2px; font-size: 0.72rem; color: var(--muted); }
.census input {
  width: 100%; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.7);
  border-radius: 8px; padding: 4px 6px; color: var(--ink); font-size: 0.84rem; font-weight: 600;
}

#tools { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; overflow: auto; flex: 1 1 auto; min-height: 0; }
#neighborMix {
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
#upgradeToggle {
  border: 0; border-radius: 12px; padding: 8px 10px; flex: none;
  background: rgba(29, 25, 20, 0.06); font-weight: 650; text-align: left;
}
#upgradeToggle.on { background: var(--moss); color: #f6f1e4; }
#tools[hidden],
#toolHelp[hidden],
#warBox[hidden],
#upgradeBox[hidden] { display: none !important; }
#upgradeBox { display: grid; gap: 8px; overflow: auto; flex: 1 1 auto; min-height: 240px; }
#upgradePlaces { display: flex; flex-wrap: wrap; gap: 6px; }
#upgradePlaces:empty { display: none; }
#upgradePlaces button {
  border: 1px solid transparent; border-radius: 999px; padding: 6px 10px;
  background: rgba(29, 25, 20, 0.05); font-size: 0.75rem; text-align: left;
}
#upgradePlaces button.on { border-color: var(--moss); background: rgba(35, 72, 56, 0.12); color: var(--moss); }
.tool {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  padding: 8px; border: 0; border-radius: 12px; text-align: left; background: rgba(29, 25, 20, 0.04);
}
.tool b { font-weight: 600; font-size: 0.84rem; }
.tool span, .tool kbd, .tool em { font-family: inherit; font-size: 0.68rem; color: #8a7b6b; font-style: normal; }
.tool em { color: var(--moss); font-weight: 650; }
.tool.afford { background: #c8e8b8; }
.tool.broke { background: #e24b4b; color: #fff8f6; }
.tool.broke span, .tool.broke kbd, .tool.broke em { color: rgba(255, 248, 246, 0.92); }
.tool.on { box-shadow: inset 0 0 0 2px #1d1914; }
.tool.afford:hover { background: #b6dea4; }
.tool.broke:hover { background: #cf3d3d; }

#meters { display: grid; grid-template-columns: 1fr 1fr; gap: 5px 12px; }
.meter { display: grid; grid-template-columns: 1fr auto; gap: 1px 6px; align-items: center; }
.meter span, .meter b { font-size: 0.72rem; }
.meter b { color: var(--muted); font-weight: 600; }
.meter div { grid-column: 1 / -1; height: 5px; border-radius: 999px; background: rgba(29, 25, 20, 0.08); overflow: hidden; }
.meter i { display: block; height: 100%; width: 0; background: var(--gold); }
.meter[data-id="wellbeing"] i { background: #e85d8c; }
.meter[data-id="wealth"] i { background: #e0a100; }
.meter[data-id="stability"] i { background: #2b7fd4; }
.meter[data-id="culture"] i { background: #6d4cc4; }
.meter[data-id="nature"] i { background: #1f9d5a; }
.meter[data-id="defense"] i { background: #e07a32; }

.tabs { display: flex; gap: 4px; padding: 3px; background: rgba(29, 25, 20, 0.05); border-radius: 999px; }
.tabs button { flex: 1; border: 0; background: transparent; border-radius: 999px; padding: 7px 4px; color: var(--muted); }
.tabs button.on { background: var(--moss); color: #f6f1e4; }
.sheet { min-height: 0; overflow: auto; flex: 1; display: flex; flex-direction: column; gap: 8px; padding-right: 2px; }
.sheet section { display: flex; flex-direction: column; gap: 8px; }
.sheet section[hidden] { display: none; }
label.field, .colorRow label { display: grid; gap: 4px; font-size: 0.75rem; color: var(--muted); }
input[type="text"] {
  width: 100%; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.55);
  border-radius: 10px; padding: 8px 10px; color: var(--ink); font-size: 0.95rem;
}
#govList, #lawList, #budgetList, #upgradeList { display: grid; gap: 6px; }
.upgrade { display: grid; gap: 6px; }
.upgrade[hidden] { display: none; }
#soldierBox[hidden] { display: none; }
#soldierBox { display: grid; gap: 6px; }
#warBox { display: grid; gap: 6px; overflow: auto; flex: 0 0 auto; min-height: 128px; max-height: 220px; }
#siteNote { margin: 0; color: var(--ink); font-size: 0.8rem; font-weight: 650; }
#siteNote[hidden], #siteBar[hidden] { display: none; }
#siteBar { height: 8px; border-radius: 99px; background: rgba(29, 25, 20, 0.08); overflow: hidden; }
#siteFill { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #e0a100, #e07a32); }
#voyage { height: 8px; border-radius: 99px; background: rgba(29, 25, 20, 0.08); overflow: hidden; }
#voyage[hidden] { display: none; }
#voyageFill { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #2b7fd4, #1f9d5a); }
#garrison {
  display: flex; gap: 4px; flex: 0 0 auto; overflow-x: auto; overflow-y: hidden; min-height: 0;
}
#garrison:empty { display: none; }
#garrison button, #warList button {
  text-align: left; border-radius: 12px; padding: 6px 8px;
  background: rgba(29, 25, 20, 0.04); border: 1px solid transparent; display: grid; gap: 2px;
}
#garrison button {
  flex: 0 0 auto; display: inline-flex; align-items: center;
  padding: 4px 8px; border-radius: 999px; font-size: 0.72rem; white-space: nowrap;
}
#warList button b { font-size: 0.86rem; }
#warList button small { color: var(--muted); font-size: 0.72rem; }
#warList button.best {
  border-color: #e07a32;
  background: #fff1e4;
}
#warMore {
  border-radius: 999px;
  padding: 6px 10px;
  background: var(--paper);
  font-size: 0.75rem;
}
.upgrade h4 { margin: 4px 0 0; font-size: 0.82rem; }
.upgrade .pair { display: grid; grid-template-columns: 1fr; gap: 6px; }
.upgrade button, #soldierBtn {
  text-align: left; border-radius: 12px; padding: 8px 10px;
  background: rgba(29, 25, 20, 0.04); border: 1px solid transparent; display: grid; gap: 2px;
}
.upgrade button.on { border-color: var(--moss); background: rgba(35, 72, 56, 0.1); }
.upgrade button b { font-size: 0.86rem; }
.upgrade button span, .upgrade button small { color: var(--muted); font-size: 0.72rem; }
.gov, .law, .choice, .budget {
  text-align: left; border-radius: 12px; padding: 8px 10px;
  background: rgba(29, 25, 20, 0.04); border: 1px solid transparent;
}
.gov { display: grid; gap: 2px; }
.gov.on, .layout.on, .emblem.on, .value.on { border-color: var(--moss); background: rgba(35, 72, 56, 0.1); }
.gov span, .law span, .budget small { color: var(--muted); font-size: 0.76rem; }
.law { display: grid; grid-template-columns: auto 1fr; column-gap: 8px; row-gap: 2px; align-items: start; }
.law input { grid-row: 1 / span 2; }
.law span { grid-column: 2; }
.law input { margin-top: 3px; accent-color: var(--moss); }
.law b, .gov b { font-size: 0.9rem; }
#valueList, #langList, #layoutList, #emblemList, #presets, #swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.value, .layout, .emblem {
  border: 1px solid transparent; border-radius: 999px; padding: 6px 10px; background: rgba(29, 25, 20, 0.05);
}
.colorRow { display: flex; gap: 8px; }
.colorRow label { flex: 1; }
input[type="color"] { width: 100%; height: 36px; border: 0; background: transparent; padding: 0; }
.swatch {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid white;
  box-shadow: 0 0 0 1px var(--line); padding: 0;
}
.swatch.on { box-shadow: 0 0 0 2px var(--ink); }
.preset {
  display: grid; gap: 4px; justify-items: center; border: 0; background: transparent; padding: 0; color: var(--muted); font-size: 0.72rem;
}
.preset canvas { width: 72px; height: 48px; border-radius: 4px; box-shadow: 0 0 0 1px var(--line); }
.budget { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.budget input { grid-column: 1 / -1; accent-color: var(--seal); }
.budget input[data-id="edu"] { accent-color: #6d4cc4; }
.budget input[data-id="health"] { accent-color: #e85d8c; }
.budget input[data-id="defense"] { accent-color: #e07a32; }
.budget input[data-id="roads"] { accent-color: #2b7fd4; }
.budget input[data-id="culture"] { accent-color: #d23b86; }
.budget input[data-id="nature"] { accent-color: #1f9d5a; }
.budget small { grid-column: 1 / -1; }
#charter {
  margin: 0; font-family: Georgia, "Palatino Linotype", serif;
  font-size: 0.98rem; line-height: 1.45; color: var(--ink);
}
#goals, #chronicle { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
#goals li, #chronicle li { display: flex; gap: 8px; align-items: flex-start; font-size: 0.86rem; }
#goals li { align-items: center; }
#goals .mark {
  width: 16px; height: 16px; border-radius: 50%; border: 1.5px solid #b5a89a; flex: none; padding: 0; background: transparent;
}
#goals li.done { color: var(--moss); }
#goals li.done .mark { background: var(--moss); border-color: var(--moss); box-shadow: inset 0 0 0 2px #f6f1e4; }
#goals .text { flex: 1; text-align: left; }
#goals .drop { border: 0; background: transparent; color: var(--muted); padding: 0 4px; }
#goals .empty { color: var(--muted); }
.goalForm { display: flex; gap: 6px; }
.goalForm input { flex: 1; }
.goalForm button { border-radius: 999px; padding: 8px 12px; background: var(--moss); color: #f6f1e4; border: 0; }
#chronicle li { display: grid; gap: 2px; }
#chronicle b { font-size: 0.72rem; color: var(--gold); font-weight: 600; }
#chronicle .empty { color: var(--muted); }
#ledger footer { border-top: 1px solid var(--line); padding-top: 8px; display: grid; gap: 8px; }
#hook {
  margin: 0;
  padding: 8px 10px;
  border-radius: 12px;
  background: #f3e6c4;
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 650;
  line-height: 1.35;
}
button.primary {
  width: 100%; border: 0; border-radius: 999px; padding: 12px 16px;
  background: var(--seal); color: #f8f1e6; font-weight: 650;
}
button.primary:hover { background: #b4323e; }
button:disabled { opacity: 0.55; cursor: default; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

#hint, #toast {
  position: absolute; left: 50%; transform: translateX(-50%);
  margin: 0; border-radius: 999px; padding: 8px 14px; font-size: 0.82rem;
}
#hint {
  bottom: 8px; max-width: min(520px, calc(100% - 680px));
  background: #e7f2ff; border: 1px solid #b9d4f5; color: #24527a; text-align: center; font-weight: 650;
}
#toast { top: 8px; z-index: 4; background: #6d4cc4; color: #f7f3ff; }
#toast[hidden], .modal[hidden], #coach[hidden], #guide[hidden] { display: none; }

#zoom { position: absolute; left: 250px; top: 8px; display: flex; flex-direction: column; gap: 6px; }
#zoom button {
  width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--paper); font-size: 1.3rem;
  box-shadow: 0 8px 18px rgba(24, 32, 28, 0.12);
}
#compass {
  position: absolute; left: 298px; top: 8px; width: 84px; height: 84px;
  border-radius: 50%; border: 1px solid var(--line); background: var(--paper);
  touch-action: none; display: grid; place-items: center;
  box-shadow: 0 8px 18px rgba(24, 32, 28, 0.12), inset 0 0 0 7px rgba(29, 25, 20, 0.05);
}
#compass::before {
  content: ""; position: absolute; top: 5px; left: 50%;
  border-left: 5px solid transparent; border-right: 5px solid transparent;
  border-top: 7px solid var(--ink); transform: translateX(-50%);
}
#compassRose { position: relative; width: 68px; height: 68px; }
#compassRose::before {
  content: ""; position: absolute; left: 50%; top: 6px; width: 2px; height: 28px;
  background: var(--seal); transform: translateX(-50%);
}
#compassRose b, #compassRose span {
  position: absolute; left: 50%; top: 50%; width: 14px; height: 14px;
  margin: -7px 0 0 -7px; text-align: center; line-height: 14px;
  font-size: 0.72rem; font-weight: 700;
}
#compassRose b { color: var(--seal); transform: translateY(-24px); }
#compassRose span:nth-child(2) { transform: translateX(24px); }
#compassRose span:nth-child(3) { transform: translateY(24px); }
#compassRose span:nth-child(4) { transform: translateX(-24px); }
#soloBtn {
  position: absolute; left: 250px; top: 102px; height: 40px; padding: 0 12px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--paper); white-space: nowrap;
  box-shadow: 0 8px 18px rgba(24, 32, 28, 0.12);
}
#soloBtn[aria-pressed="true"] { background: var(--moss); color: #f6f1e4; }

#coach, #guide {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%);
  width: min(440px, calc(100% - 680px));
  display: flex; gap: 10px; align-items: center;
  padding: 12px 12px 12px 14px; border-radius: 16px;
  background: var(--moss); color: #f6f1e4;
}
#coach p, #guide p { margin: 0; font-size: 0.9rem; line-height: 1.35; }
#coach button, #guide button { background: #f6f1e4; color: var(--ink); white-space: nowrap; }
#guide {
  display: grid; gap: 6px; align-items: start;
  width: min(480px, calc(100% - 680px));
  padding: 14px 16px 12px;
  background: var(--paper-solid); color: var(--ink);
  border: 2px solid var(--moss);
  box-shadow: 0 16px 40px rgba(24, 32, 28, 0.22);
  z-index: 4;
}
#guide h2 { margin: 0; font-family: Georgia, "Palatino Linotype", serif; font-size: 1.35rem; font-weight: 600; }
#guideNameLabel { display: grid; gap: 6px; font-size: 0.85rem; font-weight: 650; }
#guideNameLabel[hidden], #guideGo[hidden] { display: none; }
#guideName {
  font: inherit; font-size: 1.2rem; font-weight: 650; color: var(--ink);
  padding: 12px 14px; border-radius: 12px; border: 2px solid var(--moss); background: #fff;
}
#guide button.primary { background: var(--moss); color: #f6f1e4; }
#guide button.primary:hover { background: #176b40; }
#guide .kicker { color: var(--moss); }
#guideSteps { list-style: none; display: flex; flex-wrap: wrap; gap: 4px; margin: 0; padding: 0; }
#guideSteps li {
  border-radius: 999px; padding: 3px 8px; font-size: 0.72rem;
  background: rgba(29, 25, 20, 0.06); color: var(--muted);
}
#guideSteps li.on { background: var(--moss); color: #f6f1e4; }
#guideSteps li.done { color: var(--moss); }
#guideSkip { justify-self: start; border: 0; border-radius: 999px; padding: 7px 12px; }
.guide-target { outline: 3px solid var(--seal); outline-offset: 3px; animation: guidePulse 1.3s ease-in-out infinite; }
@keyframes guidePulse {
  50% { outline-color: transparent; }
}
body.coach-off #hint { visibility: visible; }
body:not(.coach-off) #hint { visibility: hidden; }

.modal {
  position: absolute; inset: 0; z-index: 5;
  display: grid; place-items: center; padding: 18px;
  background: rgba(22, 28, 30, 0.46);
}
.modal article {
  width: min(460px, 100%); background: var(--paper-solid);
  border-radius: 18px; padding: 18px; display: grid; gap: 10px;
  box-shadow: 0 20px 60px rgba(20, 16, 12, 0.28);
}
.modal h2 { margin: 0; font-family: Georgia, "Palatino Linotype", serif; font-size: 1.6rem; font-weight: 600; }
.modal article > p { margin: 0; line-height: 1.45; }
.choices { display: grid; gap: 8px; }
.choices button {
  text-align: left; border-radius: 12px; padding: 10px 12px;
  background: rgba(29, 25, 20, 0.05); border: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 2px;
}
.choices button:hover { background: rgba(35, 72, 56, 0.1); }
.choices button[aria-pressed="true"] { box-shadow: inset 0 0 0 2px var(--moss); }
#worldList { max-height: min(320px, 46vh); overflow: auto; }
.choices button span { color: var(--muted); font-size: 0.8rem; }
.row { display: flex; justify-content: flex-end; gap: 8px; }
.row .primary { width: auto; }

@media (max-width: 1100px) {
  #hint, #coach, #guide { width: min(440px, calc(100% - 660px)); }
  #saveState { display: none; }
}

@media (max-width: 860px) {
  #app { padding: 8px; gap: 8px; }
  header { padding: 8px; }
  .brand h1 { font-size: 1.15rem; max-width: 42vw; }
  #facts span { padding: 4px 8px; }
  #stage {
    display: grid;
    grid-template-rows: auto minmax(150px, 1fr) minmax(210px, 44vh);
    gap: 8px;
  }
  #dock, #ledger { position: relative; width: auto; top: auto; right: auto; bottom: auto; left: auto; }
  #dock { grid-row: 1; max-height: 46vh; }
  #ledger { grid-row: 3; min-height: 0; }
  #tools { display: flex; overflow: auto; min-height: 120px; }
  .tool { width: 108px; flex: none; }
  #landMix, #buildMix, #neighborMix, #hint { display: none; }
  #coach, #guide {
    position: relative; left: auto; bottom: auto; transform: none;
    width: auto; grid-row: 2; align-self: end;
  }
  #zoom {
    position: relative; grid-row: 2; justify-self: end; align-self: start;
    left: auto; right: auto; top: auto; flex-direction: row;
  }
  #compass { left: auto; right: 8px; top: 58px; }
  #soloBtn { left: auto; right: 8px; top: 154px; }
  #meters { grid-template-columns: 1fr 1fr; }
}
