:root {
  color-scheme: dark;
  --bg: #0d1513;
  --panel: #121d1a;
  --panel-2: #17251f;
  --ink: #e4eadf;
  --muted: #8d9d90;
  --faint: #4a5a50;
  --line: #718276;
  --green: #87b26f;
  --grass: #64a86a;
  --gold: #dcc16f;
  --orange: #df8f5c;
  --cyan: #72c4c8;
  --blue: #6599c7;
  --cream: #d8d1b1;
  --danger: #dd7668;
  --shadow: rgb(0 0 0 / 48%);
  --cell-size: clamp(11px, 1.12vw, 14px);
  --cell-line: 1.16;
}

* { box-sizing: border-box; }
html, body { width: 100%; min-height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, a { font: inherit; }
button { color: inherit; }
button:focus-visible, a:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

.app {
  width: 100%;
  height: 100vh;
  height: 100svh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  background: radial-gradient(circle at 50% 0, #14241f 0, var(--bg) 55%);
}
.topbar {
  z-index: 5;
  min-height: 54px;
  display: grid;
  grid-template-columns: minmax(118px, 1fr) auto minmax(44px, 1fr);
  align-items: center;
  gap: 12px;
  padding: max(9px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 9px max(12px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: rgb(13 21 19 / 94%);
}
.brand { min-width: 0; display: flex; align-items: center; gap: 9px; color: inherit; text-decoration: none; }
.brand span { color: var(--gold); font-size: .72rem; }
.brand strong { overflow: hidden; font-size: clamp(.7rem, 2.5vw, .9rem); letter-spacing: .1em; white-space: nowrap; text-overflow: ellipsis; }
.status { display: flex; align-items: center; justify-content: center; gap: clamp(8px, 2.5vw, 22px); color: var(--muted); font-size: clamp(.57rem, 2vw, .72rem); white-space: nowrap; }
.status strong { color: #9fda88; font-size: .82rem; }
.icon-button { justify-self: end; min-width: 44px; min-height: 36px; padding: 6px; border: 1px solid var(--line); background: transparent; cursor: pointer; }

.game-area { min-width: 0; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; overflow: hidden; }
.action-bar {
  z-index: 3;
  min-height: 46px;
  display: grid;
  grid-template-columns: minmax(132px, auto) 1fr auto;
  align-items: stretch;
  border-bottom: 1px solid var(--faint);
  background: var(--panel);
}
.selection { min-width: 0; display: flex; align-items: center; gap: 7px; padding: 7px 13px; border-right: 1px solid var(--faint); }
.selection small { color: var(--muted); font-size: .52rem; letter-spacing: .12em; }
.selection strong { overflow: hidden; color: var(--cyan); font-size: .7rem; white-space: nowrap; text-overflow: ellipsis; }
.action-bar p { align-self: center; margin: 0; padding: 0 12px; color: var(--muted); font-size: .62rem; text-align: center; }
.shop-button { min-width: 92px; border: 0; border-left: 1px solid var(--faint); background: transparent; font-size: .65rem; font-weight: 800; letter-spacing: .08em; cursor: pointer; }
.shop-button:hover, .icon-button:hover { background: var(--panel-2); }

.world-viewport {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  scrollbar-width: none;
}
.world-viewport::-webkit-scrollbar { display: none; }
.world-viewport.dragging { cursor: grabbing; }
#world {
  width: max-content;
  min-width: 100%;
  min-height: 100%;
  margin: 0;
  padding: 8px 12px;
  font: 500 var(--cell-size)/var(--cell-line) ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  letter-spacing: 0;
  white-space: pre;
  pointer-events: none;
}
#world .sky { color: #657b73; }
#world .cloud { color: #aebbb4; }
#world .rain { color: var(--blue); }
#world .hill { color: #799779; }
#world .grass { color: var(--grass); }
#world .crop { color: var(--gold); }
#world .corn { color: #a9ce6a; }
#world .pumpkin { color: var(--orange); }
#world .flower { color: #e3b95f; }
#world .strange { color: #b895d6; }
#world .water { color: var(--cyan); }
#world .building { color: var(--cream); }
#world .ruin { color: #817e75; }
#world .animal { color: #e7e4d4; }
#world .blocker { color: #63875f; }
#world .effect { color: #d9eff0; }
#world .label { color: #68786e; }
#world .money { color: #9fda88; }
#world .danger { color: var(--danger); }
.pan-hint { display: none; height: 22px; align-items: center; justify-content: center; gap: 11px; border-top: 1px solid var(--faint); color: var(--faint); background: var(--bg); font-size: .5rem; letter-spacing: .1em; }

.toolbar {
  z-index: 6;
  min-width: 0;
  min-height: 68px;
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding: 7px max(10px, env(safe-area-inset-right)) max(7px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  overflow-x: auto;
  border-top: 1px solid var(--line);
  background: var(--panel);
  overscroll-behavior-x: contain;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.tool-button {
  position: relative;
  min-width: 88px;
  min-height: 52px;
  display: grid;
  grid-template-columns: auto auto;
  grid-template-rows: auto auto;
  align-content: center;
  justify-content: space-between;
  gap: 2px 8px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.tool-button strong { grid-column: 1 / -1; font-size: .63rem; letter-spacing: .03em; white-space: nowrap; }
.tool-button span { color: var(--muted); font-size: .67rem; }
.tool-button small { justify-self: end; color: var(--muted); font-size: .58rem; }
.tool-button.active { border-color: var(--cyan); background: var(--cyan); color: #0b1715; box-shadow: 0 0 0 1px var(--cyan); }
.tool-button.active span, .tool-button.active small { color: inherit; opacity: .72; }
.tool-button:disabled { opacity: .38; cursor: default; }

.scrim { position: fixed; inset: 0; z-index: 20; background: rgb(0 0 0 / 60%); }
.scrim[hidden], .sheet[hidden] { display: none; }
.sheet {
  position: fixed;
  z-index: 21;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(100%, 480px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border-left: 1px solid var(--line);
  background: var(--bg);
  box-shadow: -18px 0 55px var(--shadow);
}
.sheet-header { min-height: 74px; display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: max(15px, env(safe-area-inset-top)) 16px 13px; border-bottom: 1px solid var(--line); }
.sheet-header p { margin: 0 0 3px; color: var(--muted); font-size: .54rem; letter-spacing: .17em; }
.sheet-header h1 { margin: 0; font-size: 1rem; }
.close-button { width: 44px; height: 44px; border: 1px solid var(--line); background: transparent; font-size: 1.3rem; cursor: pointer; }
.shop-list, .settings-list { min-height: 0; overflow-y: auto; padding: 8px 14px max(22px, env(safe-area-inset-bottom)); }
.shop-card { min-height: 83px; display: grid; grid-template-columns: 58px minmax(0, 1fr) auto; align-items: center; gap: 11px; padding: 10px 2px; border-bottom: 1px solid var(--faint); }
.shop-card .art { color: var(--gold); font-size: .9rem; text-align: center; }
.shop-copy h2 { margin: 0; font-size: .7rem; letter-spacing: .04em; }
.shop-copy p { margin: 5px 0 0; color: var(--muted); font-size: .58rem; line-height: 1.4; }
.buy-button, .setting-row > button, .danger-button { min-width: 72px; min-height: 42px; padding: 7px 9px; border: 1px solid var(--line); background: transparent; font-size: .61rem; font-weight: 800; cursor: pointer; }
.buy-button:disabled { opacity: .34; cursor: default; }
.buy-button.owned { border-color: transparent; color: var(--muted); }
.setting-row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 16px; padding: 18px 0; border-bottom: 1px solid var(--faint); }
.setting-row strong { display: block; font-size: .68rem; }
.setting-row small { display: block; margin-top: 5px; color: var(--muted); font-size: .58rem; }
.save-note { padding: 20px 0; color: var(--muted); font-size: .61rem; line-height: 1.5; }
.danger-button { width: 100%; color: var(--danger); }
.danger-button.confirming { border-color: var(--danger); background: rgb(221 118 104 / 10%); }
.toast { position: fixed; z-index: 30; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); max-width: min(90vw, 450px); padding: 10px 14px; border: 1px solid var(--line); background: var(--panel-2); box-shadow: 0 8px 28px var(--shadow); font-size: .66rem; line-height: 1.35; text-align: center; opacity: 0; translate: -50% 8px; pointer-events: none; transition: opacity 150ms ease, translate 150ms ease; }
.toast.show { opacity: 1; translate: -50% 0; }

@media (max-width: 700px) {
  :root { --cell-size: 12px; }
  .pan-hint { display: flex; }
  .sheet { top: auto; width: 100%; height: min(88svh, 680px); border-top: 1px solid var(--line); border-left: 0; box-shadow: 0 -18px 55px var(--shadow); }
}
@media (max-width: 510px) {
  .topbar { grid-template-columns: minmax(100px, 1fr) auto 40px; gap: 7px; }
  .status span:first-child { display: none; }
  .action-bar { grid-template-columns: 1fr auto; }
  .action-bar p { display: none; }
  .selection { border-right: 0; }
  .shop-button { min-width: 76px; }
  .brand span { display: none; }
  .brand strong { font-size: .67rem; }
  .shop-card { grid-template-columns: 45px minmax(0, 1fr) 66px; gap: 7px; }
}
@media (max-height: 580px) and (orientation: landscape) {
  :root { --cell-size: clamp(7px, 1.55svh, 9px); --cell-line: 1.05; }
  .topbar { min-height: 40px; padding-block: 5px; }
  .action-bar { min-height: 36px; }
  .toolbar { min-height: 52px; padding-block: 4px; }
  .tool-button { min-height: 44px; }
  .pan-hint { display: none; }
  #world { padding-block: 4px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; }
}
