:root {
  --ink: #2c2a22; --paper: #f5eedc; --paper-2: #e9dfc6; --line: #cfc2a2;
  --accent: #3f6fb0; --good: #4c8a3a; --bad: #c0442f; --glass: rgba(34, 38, 26, 0.72);
  --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #3d5a30; color: var(--ink);
  font: 14px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; overscroll-behavior: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }
[hidden] { display: none !important; }
#view { position: fixed; inset: 0; touch-action: none; display: block; }

/* top bar */
#goods { display: contents; }
#topbar { position: fixed; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 4px;
  padding: calc(var(--safe-t) + 6px) calc(var(--safe-r) + 8px) 6px calc(var(--safe-l) + 8px);
  background: linear-gradient(var(--glass), rgba(34,38,26,0.45)); color: #f8f3e2; }
.stat { display: inline-flex; align-items: center; gap: 3px; padding: 4px 7px; border-radius: 9px; white-space: nowrap; font-size: 13px; }
.stat b { font-variant-numeric: tabular-nums; }
.stat.dim { opacity: 0.7; }
.stat.pop { background: rgba(255,255,255,0.12); }
.stat.pop.bad { background: rgba(192,68,47,0.75); }
.stat.pop i { font-style: normal; font-size: 10px; opacity: 0.85; }
.spacer { flex: 1; }
.ctl { min-width: 36px; height: 32px; border-radius: 9px; background: rgba(255,255,255,0.14); font-weight: 600; }
.ctl.on { background: var(--accent); }

/* objectives */
#goals { position: fixed; top: calc(var(--safe-t) + 50px); left: calc(var(--safe-l) + 8px); width: min(250px, 46vw);
  background: rgba(245,238,220,0.92); border-radius: 12px; padding: 7px 10px; font-size: 12px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); }
.goal-title { font-weight: 700; display: flex; justify-content: space-between; margin-bottom: 2px; }
.goal-title i { font-style: normal; opacity: 0.6; }
.goal { display: flex; justify-content: space-between; gap: 6px; padding: 1px 0; opacity: 0.6; }
.goal.next { opacity: 1; font-weight: 600; }
.goal.done { opacity: 0.45; text-decoration: line-through; }
.goal b { font-variant-numeric: tabular-nums; }
.hint { font-size: 11px; color: #5d5640; margin: 1px 0 4px 12px; }

/* bottom bars */
#buildbar { overflow-x: auto; scrollbar-width: none; }
#buildbar .build { flex: none; }
#buildbar, #placebar { position: fixed; bottom: calc(var(--safe-b) + 8px); left: 50%; transform: translateX(-50%);
  display: flex; gap: 6px; padding: 6px; border-radius: 16px; background: var(--glass); max-width: calc(100vw - 16px); }
.build { width: 56px; height: 54px; border-radius: 11px; background: var(--paper); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 1px; box-shadow: inset 0 -2px 0 var(--line); }
.build .ico { font-size: 22px; line-height: 1; }
.build .cost { font-size: 10px; font-weight: 600; }
.build.poor { opacity: 0.5; }
.build:active { transform: translateY(1px); }
#placebar { align-items: center; color: #f8f3e2; width: min(440px, calc(100vw - 16px)); }
#place-name { flex: 1; text-align: center; font-weight: 600; font-size: 14px; }
#place-name small { opacity: 0.8; font-weight: 500; margin-left: 4px; }
#place-name em { display: block; font-style: normal; font-weight: 400; font-size: 11px; opacity: 0.8; }
.round { width: 50px; height: 50px; border-radius: 50%; font-size: 22px; font-weight: 700; background: rgba(255,255,255,0.18); }
.round.ok { background: var(--good); }
.round.ok:disabled { background: #6b6b5e; opacity: 0.6; }

/* army */
body:not(.military) .mil { display: none !important; }
.ctl.army-on { background: #c0442f; }
#raid { position: fixed; top: calc(var(--safe-t) + 50px); left: 50%; transform: translateX(-50%); padding: 5px 12px; border-radius: 20px;
  background: rgba(34,38,26,0.8); color: #f8f3e2; font-weight: 600; font-size: 13px; }
#raid.live { background: #c0442f; animation: pulse 1s infinite alternate; }
@keyframes pulse { to { transform: translateX(-50%) scale(1.06); } }
#armybar { position: fixed; bottom: calc(var(--safe-b) + 8px); left: 50%; transform: translateX(-50%); display: flex; gap: 6px; padding: 6px;
  border-radius: 16px; background: rgba(60,30,26,0.8); max-width: calc(100vw - 16px); align-items: center; color: #f8f3e2; }
.squad { min-width: 56px; height: 54px; border-radius: 11px; background: var(--paper); color: var(--ink); display: flex; flex-direction: column;
  align-items: center; justify-content: center; font-weight: 700; box-shadow: inset 0 -2px 0 var(--line); padding: 0 6px; }
.squad small { font-weight: 500; font-size: 10px; }
.squad.on { background: #ffe680; box-shadow: 0 0 0 2px #fff; }
.squad.train { background: #dfe9f7; }
.squad.train.poor { opacity: 0.55; }
.army-hint { font-size: 12px; max-width: 120px; line-height: 1.2; padding: 0 4px; }

/* toasts */
#toasts { position: fixed; top: calc(var(--safe-t) + 54px); right: calc(var(--safe-r) + 10px); display: flex; flex-direction: column; gap: 6px; align-items: flex-end; pointer-events: none; }
.toast { background: rgba(245,238,220,0.96); padding: 7px 12px; border-radius: 10px; font-size: 13px; font-weight: 600; max-width: 260px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.25); transition: opacity 0.5s, transform 0.5s; }
.toast.out { opacity: 0; transform: translateX(20px); }

/* sheets */
#sheet-bg { position: fixed; inset: 0; background: rgba(20,24,14,0.45); display: flex; align-items: center; justify-content: center; padding: 12px; }
#sheet { background: var(--paper); border-radius: 18px; padding: 16px 18px; width: min(420px, 100%); max-height: calc(100vh - 24px); overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,0.35); -webkit-user-select: text; user-select: text; }
#sheet h2 { margin: 0 0 8px; font-size: 19px; }
#sheet h3 { margin: 12px 0 6px; font-size: 14px; }
#sheet p { margin: 6px 0; }
.muted { color: #6a6350; font-size: 13px; }
.row { display: flex; justify-content: space-between; padding: 5px 0; border-bottom: 1px solid var(--paper-2); }
.row.total { font-weight: 700; border-bottom: 0; }
.pos { color: var(--good); } .neg { color: var(--bad); }
.rations { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.rations.taxes { grid-template-columns: repeat(3, 1fr); }
.ration { background: var(--paper-2); border-radius: 9px; padding: 7px 2px; font-size: 12px; font-weight: 600; display: flex; flex-direction: column; align-items: center; }
.ration small { font-weight: 500; opacity: 0.7; }
.ration.on { background: var(--accent); color: #fff; }
.btns { display: flex; gap: 8px; margin-top: 12px; }
.btns.col { flex-direction: column; }
.btns button, #sheet > .primary { flex: 1; padding: 11px; border-radius: 11px; background: var(--paper-2); font-weight: 600; width: 100%; }
#sheet > .primary { margin-top: 12px; }
button.primary { background: var(--accent) !important; color: #fff; }
button.danger { background: #ecd2c9 !important; color: var(--bad); }

/* menu */
#menu { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: radial-gradient(circle at 50% 30%, #6f8f4f, #2f4626); }
.menu-card { width: min(420px, 100%); max-height: 100%; overflow-y: auto; background: var(--paper); border-radius: 20px; padding: 20px; box-shadow: 0 10px 40px rgba(0,0,0,0.4); }
.menu-card h1 { margin: 0; font-size: 30px; letter-spacing: 0.5px; }
.tag { margin: 2px 0 12px; color: #6a6350; }
.menu-card h3 { margin: 10px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: #8a8068; }
#missions { display: flex; flex-direction: column; gap: 6px; }
.mission { display: flex; gap: 10px; align-items: center; text-align: left; padding: 10px; border-radius: 12px; background: var(--paper-2); }
.mission .num { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; flex: none; }
.mission .t { font-weight: 600; display: flex; flex-direction: column; }
.mission small { font-weight: 400; color: #6a6350; font-size: 12px; }
.mission.soon { opacity: 0.55; }
.mission.soon .num { background: #8a8068; }
.mission.done .num { background: var(--good); }

@media (max-height: 430px) {   /* phone landscape */
  .squad { min-width: 50px; height: 46px; }
  #topbar { padding-top: calc(var(--safe-t) + 4px); padding-bottom: 4px; }
  #goals { top: calc(var(--safe-t) + 44px); }
  .build { width: 50px; height: 46px; }
  .build .ico { font-size: 19px; }
}
@media (max-width: 500px) {    /* phone portrait */
  .stat { padding: 3px 4px; font-size: 12px; }
  .stat.dim { display: none; }
  .army-hint { display: none; }
  .ctl { min-width: 28px; height: 30px; }
  #topbar { gap: 2px; overflow-x: auto; scrollbar-width: none; }
  .stat { flex: none; }
}
