:root {
  --ink: #3b2414;
  --ink-soft: #6e4a2c;
  --paper: #fff0d4;
  --good: #2f7d32;
  --bad: #b3261e;
  --font: 'Pixelify Sans', ui-rounded, system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #1d4a2b;
  font-family: var(--font);
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
}

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  image-rendering: pixelated;
  cursor: url(assets/cursors/pointer_a.png) 6 3, default;
}

.hidden { display: none !important; }

.panel {
  border: 12px solid transparent;
  border-image: url(Resources/UI/panel_brown.png) 24 fill / 12px stretch;
  image-rendering: pixelated;
}
.panel-dark {
  border: 8px solid transparent;
  border-image: url(Resources/UI/panel_border_brown.png) 24 / 8px stretch;
}

.btn, #speed button {
  font: 600 15px var(--font);
  color: var(--ink);
  border: 8px solid transparent;
  border-image: url(Resources/UI/button_brown.png) 14 fill / 8px stretch;
  background: none;
  padding: 0 6px;
  height: 36px;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}
.btn:hover, #speed button:hover { filter: brightness(1.06); }
.btn:active, #speed button:active { transform: translateY(1px); }
#speed button.active {
  border-image-source: url(Resources/UI/button_grey.png);
  color: #fff;
}

/* ---------- top bar ---------- */
#topbar {
  position: fixed;
  top: 6px; left: 6px; right: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 4px;
  min-height: 56px;
  z-index: 5;
}
#resources { display: flex; gap: 4px; flex-wrap: nowrap; }
.spacer { flex: 1; }
#speed { display: flex; gap: 2px; }
#speed button { min-width: 40px; }

.chip {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px 2px 2px;
  background: rgba(110, 74, 44, .12);
  border-radius: 6px;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.chip small { font-size: 12px; color: var(--ink-soft); font-weight: 400; }
.chip.full { color: var(--bad); }
.chip.flash { animation: flash .4s; }
@keyframes flash { 50% { background: rgba(255, 220, 120, .8); } }

/* crops a small sprite out of its 64px canvas */
.ico {
  width: 30px; height: 30px;
  overflow: hidden;
  position: relative;
  flex: none;
}
.ico img {
  position: absolute;
  width: 64px; height: 64px;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  image-rendering: pixelated;
}
.ico.tight img { width: 80px; height: 80px; }

/* ---------- banner & toasts ---------- */
#banner {
  position: fixed;
  top: 76px; left: 50%;
  transform: translateX(-50%);
  width: min(520px, calc(100vw - 32px));
  height: 92px;
  background: url(Resources/UI/banner_hanging.png) center / 100% 100% no-repeat;
  image-rendering: pixelated;
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  text-align: center;
  padding: 26px 40px 0;
  text-shadow: 0 2px 0 rgba(0,0,0,.35);
  z-index: 4;
  pointer-events: none;
}

#toasts {
  position: fixed;
  top: 76px; left: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 6;
  pointer-events: none;
}
.toast {
  background: rgba(59, 36, 20, .88);
  color: var(--paper);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 15px;
  max-width: 320px;
  animation: toast-in .2s ease-out;
  transition: opacity .4s;
}
.toast.bad { background: rgba(140, 30, 24, .92); }
.toast.good { background: rgba(36, 100, 40, .92); }
@keyframes toast-in { from { transform: translateX(-20px); opacity: 0; } }

/* ---------- build bar ---------- */
#buildbar {
  position: fixed;
  left: 6px;
  right: 214px;
  bottom: 6px;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  gap: 4px 10px;
  z-index: 5;
}
#tools {
  grid-row: 1 / 3;
  display: flex;
  flex-direction: column;
  gap: 4px;
  justify-content: center;
}
.tool {
  width: 42px; height: 42px;
  border: 6px solid transparent;
  border-image: url(Resources/UI/button_brown.png) 14 fill / 6px stretch;
  background: none;
  padding: 0;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}
.tool img { width: 24px; height: 24px; image-rendering: pixelated; }
.tool.active { border-image-source: url(Resources/UI/button_grey.png); }

#tabs { display: flex; gap: 4px; }
.tab {
  font: 600 14px var(--font);
  color: var(--ink-soft);
  background: rgba(110, 74, 44, .14);
  border: 0;
  border-radius: 6px 6px 0 0;
  padding: 4px 12px;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}
.tab.active { background: rgba(110, 74, 44, .35); color: var(--ink); }

#cards {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: thin;
}
.card {
  flex: none;
  width: 92px;
  background: rgba(255,255,255,.35);
  border: 2px solid rgba(110, 74, 44, .35);
  border-radius: 6px;
  padding: 2px 4px 4px;
  text-align: center;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
  font-family: var(--font);
  color: var(--ink);
}
.card:hover { background: rgba(255,255,255,.6); }
.card.active { border-color: #e0a52a; background: #ffe7a8; box-shadow: 0 0 0 2px #e0a52a; }
.card.poor { opacity: .55; }
.card.poor .cost .miss { color: var(--bad); font-weight: 600; }
.card .pic { width: 52px; height: 52px; image-rendering: pixelated; display: block; margin: 0 auto; }
.card .name { font-size: 13px; font-weight: 600; line-height: 1.1; min-height: 15px; }
.cost { display: flex; flex-wrap: wrap; justify-content: center; gap: 0 4px; font-size: 12px; }
.cost span { display: inline-flex; align-items: center; }
.cost .ico { width: 16px; height: 16px; }
.cost .ico img { width: 40px; height: 40px; }
.cost .free { color: var(--good); font-weight: 600; }

/* ---------- info panel ---------- */
#info {
  position: fixed;
  right: 6px;
  top: 76px;
  width: 240px;
  padding: 2px 4px;
  z-index: 5;
  font-size: 14px;
}
#info h3 { margin: 0 0 2px; font-size: 19px; display: flex; align-items: center; gap: 6px; }
#info h3 img { width: 40px; height: 40px; image-rendering: pixelated; margin: -6px -4px; }
#info p { margin: 4px 0; line-height: 1.3; }
#info .row { display: flex; justify-content: space-between; gap: 10px; margin: 3px 0; }
#info .status { font-weight: 600; text-align: right; }
#info .status.bad { color: var(--bad); }
#info .status.good { color: var(--good); }
#info .actions { display: flex; gap: 6px; margin-top: 8px; }
#info .actions .btn { flex: 1; font-size: 13px; }
#info .close {
  position: absolute; right: -2px; top: -2px;
  width: 24px; height: 24px;
  background: #c8473a;
  color: #fff;
  font: 600 16px/1 var(--font);
  border: 2px solid #7a2a20;
  border-radius: 5px;
  padding: 0;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}
.workers { display: flex; gap: 0; }
.workers img { width: 32px; height: 32px; margin: -6px -6px; image-rendering: pixelated; }
.workers img.empty { opacity: .2; filter: grayscale(1); }
.bar { height: 8px; background: rgba(59,36,20,.2); border-radius: 4px; overflow: hidden; margin-top: 4px; }
.bar > div { height: 100%; background: var(--good); }

/* ---------- minimap ---------- */
#minimap {
  position: fixed;
  right: 6px;
  bottom: 6px;
  width: 208px;
  height: 160px;
  background: #1d4a2b;
  image-rendering: pixelated;
  z-index: 5;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}

/* ---------- tooltip ---------- */
#tooltip {
  position: fixed;
  z-index: 20;
  max-width: 260px;
  background: rgba(59, 36, 20, .94);
  color: var(--paper);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 14px;
  line-height: 1.3;
  pointer-events: none;
}
#tooltip b { color: #ffd77a; font-size: 15px; }
#tooltip .stat { color: #b7e3a0; }

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(20, 30, 20, .55);
  display: grid;
  place-items: center;
  z-index: 30;
  padding: 16px;
}
.modal-body {
  width: min(520px, 100%);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  padding: 4px 14px 10px;
  font-size: 16px;
  line-height: 1.4;
}
.modal-body h2 { margin: 4px 0; font-size: 28px; }
.modal-body ul { padding-left: 20px; margin: 8px 0; }
.modal-body li { margin: 4px 0; }
.modal-body .keys { color: var(--ink-soft); font-size: 14px; }
.modal-body .btn { width: 100%; height: 44px; font-size: 18px; }

/* ---------- town name ---------- */
#town-name {
  font: 600 18px var(--font);
  color: var(--ink);
  background: none;
  border: 0;
  border-right: 2px solid rgba(110, 74, 44, .25);
  padding: 0 12px 0 6px;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}
#town-name:hover { color: var(--ink-soft); text-decoration: underline dotted; }

/* ---------- title screen ---------- */
body.on-title #topbar,
body.on-title #buildbar,
body.on-title #info,
body.on-title #minimap,
body.on-title #banner { display: none; }

#title {
  position: fixed; inset: 0;
  z-index: 25;
  display: grid;
  place-items: center;
  padding: 16px;
  background: radial-gradient(ellipse at center, rgba(15, 30, 18, .15) 0%, rgba(15, 30, 18, .65) 100%);
}
.title-inner { text-align: center; width: 100%; display: flex; flex-direction: column; align-items: center; }
.logo {
  margin: 0;
  font-size: clamp(44px, 11vw, 76px);
  font-weight: 600;
  color: #fff6d8;
  letter-spacing: 1px;
  text-shadow: 0 4px 0 #6e3b1c, 0 6px 0 #3b2414, 0 10px 24px rgba(0,0,0,.45);
  animation: logo-in .6s ease-out;
}
.tagline {
  margin: 4px 0 22px;
  color: #fff6d8;
  font-size: 18px;
  text-shadow: 0 2px 0 rgba(0,0,0,.4);
}
@keyframes logo-in { from { transform: translateY(-16px); opacity: 0; } }
.title-menu { display: flex; flex-direction: column; gap: 8px; padding: 6px; width: min(360px, 100%); }
.btn.big { height: 48px; font-size: 19px; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.05; }
.btn.big small { font-size: 12px; color: var(--ink-soft); font-weight: 400; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#t-continue { height: 56px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- dialogs ---------- */
.modal-body.narrow { width: min(340px, 100%); }
.modal-body.wide { width: min(600px, 100%); }
.stack { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.stack.row2 { flex-direction: row; }
.stack.row2 .btn { flex: 1; }
.modal-body .stack .btn { height: 40px; font-size: 16px; }
.btn.primary { border-image-source: url(Resources/UI/button_grey.png); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.small { height: 30px; font-size: 13px; }

.field { display: block; margin: 10px 0 4px; }
.field > span { font-size: 14px; color: var(--ink-soft); }
.field-row { display: flex; gap: 6px; margin-top: 4px; }
.field input {
  flex: 1;
  min-width: 0;
  font: 600 18px var(--font);
  color: var(--ink);
  background: #fffaf0;
  border: 2px solid rgba(110, 74, 44, .5);
  border-radius: 6px;
  padding: 6px 10px;
  outline: none;
  user-select: text;
  -webkit-user-select: text;
}
.field input:focus { border-color: #e0a52a; box-shadow: 0 0 0 2px rgba(224, 165, 42, .35); }
.btn.dice { width: 44px; height: 40px; font-size: 22px; padding: 0; }

/* ---------- save browser ---------- */
#save-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: min(60vh, 460px);
  overflow-y: auto;
  margin: 8px 0 10px;
  padding-right: 2px;
}
.save-row {
  display: grid;
  grid-template-columns: 96px 1fr auto;
  gap: 10px;
  align-items: center;
  background: rgba(255, 255, 255, .45);
  border: 2px solid rgba(110, 74, 44, .3);
  border-radius: 6px;
  padding: 6px;
}
.save-row.current { border-color: #e0a52a; background: #ffefc4; }
.save-row .thumb {
  width: 96px; height: 72px;
  border-radius: 4px;
  background: #3fa155;
  image-rendering: pixelated;
  display: block;
}
.save-meta { min-width: 0; }
.save-name { font-size: 18px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.save-name small { font-size: 12px; color: var(--ink-soft); font-weight: 400; }
.save-name .crown { color: #c98a10; }
.save-stats { font-size: 13px; color: var(--ink-soft); }
.save-actions { display: flex; flex-direction: column; gap: 4px; width: 96px; }
.save-actions .btn { width: 100%; font-size: 14px; }
.save-actions .btn.small { font-size: 13px; }
.empty { text-align: center; color: var(--ink-soft); padding: 20px 0; }
.save-rivals { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 8px; }
.save-rivals .rival { white-space: nowrap; }
.save-rivals .rival .swatch { width: 10px; height: 10px; margin-right: 3px; }
.save-rivals .rival.fallen { text-decoration: line-through; opacity: .6; }

/* ---------- pause ---------- */
.menu-town { margin: 0; font-size: 15px; }
.menu-town span { color: var(--ink-soft); font-size: 13px; }
#menu .modal-body h2 { letter-spacing: 1px; }
#paused-pill {
  position: fixed;
  top: 74px; left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  font: 600 15px var(--font);
  color: var(--paper);
  background: rgba(59, 36, 20, .88);
  border: 0;
  border-radius: 16px;
  padding: 6px 16px;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
  animation: pill-pulse 1.6s ease-in-out infinite;
}
@keyframes pill-pulse { 50% { opacity: .65; } }
body.on-title #paused-pill { display: none; }

/* ---------- trade ---------- */
.badge {
  display: inline-block;
  min-width: 18px;
  margin-left: 4px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--bad);
  color: #fff;
  font-size: 12px;
  line-height: 18px;
  vertical-align: 1px;
}
.trade-sec { border-top: 2px solid rgba(110, 74, 44, .2); padding: 6px 0 8px; }
.trade-sec h3 { margin: 0 0 6px; font-size: 18px; }
.trade-sec h3 small { font-size: 13px; color: var(--ink-soft); font-weight: 400; margin-left: 6px; }
.ex-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 15px; }
.modal-body .ex-row .btn { width: auto; height: 34px; font-size: 14px; padding: 0 12px; }
#trade select, #trade input[type=number] {
  font: 600 14px var(--font);
  color: var(--ink);
  background: #fffaf0;
  border: 2px solid rgba(110, 74, 44, .5);
  border-radius: 6px;
  padding: 3px 6px;
  user-select: text;
  -webkit-user-select: text;
}
#trade input[type=number] { width: 64px; }
.trade-note { margin: 6px 0 0; font-size: 13px; color: var(--ink-soft); min-height: 17px; }
.trade-note.bad { color: var(--bad); }
.trade-note.good { color: var(--good); }
.deal-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; }
.deal-row { display: grid; grid-template-columns: 22px 48px 64px 1fr; align-items: center; gap: 4px; margin-top: 3px; font-size: 14px; }
.deal-row .ico { width: 22px; height: 22px; }
.deal-row .ico img { width: 52px; height: 52px; }
.deal-row small { color: var(--ink-soft); font-size: 11px; white-space: nowrap; }
.deal-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 6px; }
.deal-foot .trade-note { margin: 0; }
.modal-body .deal-foot .btn { width: auto; padding: 0 16px; }
.request { display: flex; align-items: center; gap: 6px; padding: 6px; margin-top: 4px; border-radius: 6px; background: rgba(255, 231, 168, .6); font-size: 14px; }
.request-main { flex: 1; min-width: 0; }
.request-main small { color: var(--ink-soft); margin-left: 4px; }
.modal-body .request .btn { width: auto; height: 32px; font-size: 13px; padding: 0 10px; }
.res-amt { display: inline-flex; align-items: center; font-weight: 600; margin: 0 2px; }
.res-amt .ico { width: 18px; height: 18px; }
.res-amt .ico img { width: 44px; height: 44px; }
.muted { color: var(--ink-soft); }
@media (max-width: 520px) { .deal-cols { grid-template-columns: 1fr; } }

/* ---------- nations ---------- */
.rivals { margin-top: 12px; }
.rival-row { display: flex; align-items: center; gap: 6px; margin-top: 5px; font-size: 15px; }
.rival-row > span, .rival-row > b { flex: 1; }
.rival-row b { font-weight: 600; }
.rivals select, .ai-level select {
  font: 600 14px var(--font);
  color: var(--ink);
  background: #fffaf0;
  border: 2px solid rgba(110, 74, 44, .5);
  border-radius: 6px;
  padding: 3px 6px;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}
.mode-row { display: flex; gap: 6px; margin: 4px 0 6px; }
.mode-card {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border: 2px solid rgba(110, 74, 44, .35);
  border-radius: 6px;
  background: rgba(255, 255, 255, .45);
  font-size: 15px;
  font-weight: 600;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}
.mode-card input { margin: 0; accent-color: #b3261e; }
.mode-card:has(input:checked) { border-color: #e0a52a; background: #ffe7a8; }
.mode-card.locked { opacity: .55; cursor: not-allowed; }
.mode-swatches { display: inline-flex; }
.mode-swatches .swatch { margin-right: 2px; }
.ub-icon { position: relative; display: inline-block; width: 26px; height: 26px; flex: none; vertical-align: middle; margin-right: 4px; }
.ub-icon img { position: absolute; image-rendering: pixelated; }
.ub-icon img:first-child { inset: 0; width: 100%; height: 100%; }
.ub-icon .ex { width: 8px; height: 16px; left: 50%; top: 50%; transform: translate(-50%, -50%); }
.ub-icon .lock { width: 18px; height: 18px; right: -6px; bottom: -6px; }
.ub-icon.locked img:not(.lock) { filter: grayscale(1); }
.ub-badge { display: inline-flex; align-items: center; font-weight: 600; color: var(--bad); }
.save-rivals .ub-icon { width: 16px; height: 16px; }
.save-rivals .ub-icon .ex { width: 5px; height: 10px; }
.never { color: var(--bad); font-weight: 600; }
.rival-desc { font-size: 13px; color: var(--ink-soft); margin: 6px 0 0; min-height: 18px; }
.ai-level { float: right; font-size: 13px; font-weight: 400; color: var(--ink-soft); }
.ai-level select { font-size: 13px; padding: 1px 4px; }
.swatch {
  display: inline-block;
  width: 12px; height: 12px;
  border-radius: 3px;
  border: 2px solid rgba(59, 36, 20, .6);
  margin-right: 5px;
  vertical-align: -1px;
  flex: none;
}
#army { cursor: url(assets/cursors/hand_point.png) 10 2, pointer; border: 0; font-family: var(--font); color: var(--ink); }
#army small { margin-left: 4px; }
#army.war, #btn-nations.war { color: var(--bad); }
#army.war { background: rgba(179, 38, 30, .15); }

.map-toggle {
  position: fixed;
  right: 14px;
  bottom: 170px;
  z-index: 6;
  font: 600 13px var(--font);
  color: var(--ink);
  background: var(--paper);
  border: 2px solid rgba(110, 74, 44, .7);
  border-radius: 6px;
  padding: 3px 10px;
  cursor: url(assets/cursors/hand_point.png) 10 2, pointer;
}
.map-toggle.active { background: #ffd77a; }
body.on-title .map-toggle { display: none; }

#nation-list { display: flex; flex-direction: column; gap: 8px; margin: 8px 0 10px; max-height: min(64vh, 520px); overflow-y: auto; }
.nation-row {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  background: rgba(255, 255, 255, .45);
  border: 2px solid rgba(110, 74, 44, .3);
  border-radius: 6px;
  padding: 8px;
}
.nation-row .swatch { width: 20px; height: 20px; margin-top: 2px; }
.nation-row.rel-war { border-color: rgba(179, 38, 30, .6); background: rgba(255, 220, 210, .6); }
.nation-row.rel-ally { border-color: rgba(47, 125, 50, .6); background: rgba(220, 245, 210, .6); }
.nation-row.fallen { opacity: .55; }
.nation-main { flex: 1; min-width: 0; }
.nation-name { font-size: 18px; font-weight: 600; }
.pill { font-size: 12px; font-weight: 600; padding: 1px 7px; border-radius: 10px; background: rgba(110, 74, 44, .18); vertical-align: 2px; }
.rel-war .pill { background: var(--bad); color: #fff; }
.rel-ally .pill { background: var(--good); color: #fff; }
.nation-stats { font-size: 13px; color: var(--ink-soft); margin-top: 2px; }
.nation-actions { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; margin-top: 6px; font-size: 13px; }
.modal-body .nation-actions .btn { width: auto; height: 32px; font-size: 13px; padding: 0 8px; }
.events { font-size: 13px; color: var(--ink-soft); }
.events ul { margin: 4px 0 0; padding-left: 18px; }
.btn.danger-fill { border-image-source: url(Resources/UI/button_red.png); color: #fff; }

@media (max-width: 760px) {
  #minimap { display: none; }
  #buildbar { right: 6px; }
  #topbar { flex-wrap: wrap; padding: 2px; }
  .spacer { display: none; }
  #info { top: auto; bottom: 170px; }
  #town-name { max-width: 120px; font-size: 15px; }
  .save-row { grid-template-columns: 64px 1fr; }
  .save-row .thumb { width: 64px; height: 48px; }
  .save-actions { grid-column: 1 / -1; flex-direction: row; width: auto; }
}
