:root {
  --bg: #f3ead9; --panel: #fffaf0; --card: #ffffff; --line: #e2d3b8; --text: #2e2218; --muted: #7a6650;
  --accent: #b5662b; --accent2: #3f7d4a; --good: #2b8a3e; --warn: #c58a1a; --bad: #b23b2e; --chip: #f4ead6;
  --shadow: 0 2px 10px rgba(60, 40, 20, 0.12);
  --top-h: 52px; --tabs-h: 0px;
  color-scheme: light;
}
[data-iht-theme="dark"] {
  --bg: #15110d; --panel: #221a13; --card: #2a2219; --line: #3d3024; --text: #efe6d8; --muted: #b8a68f;
  --accent: #e08a45; --accent2: #6fb37c; --good: #69db7c; --warn: #e3b04b; --bad: #e0675a; --chip: #2c2219;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.4); color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body { overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
img, canvas { max-width: 100%; }
#app { display: flex; flex-direction: column; height: 100dvh; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }

/* Top bar */
#top { display: flex; align-items: center; gap: 10px; padding: 6px 10px; min-height: var(--top-h); background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
#top .home { font-size: 24px; text-decoration: none; color: var(--text); min-width: 44px; min-height: 44px; display: grid; place-items: center; border-radius: 10px; }
#top .home:hover { background: var(--chip); }
.brand { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.brand b { font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand span { font-size: 0.8rem; color: var(--muted); }
.speed { display: flex; gap: 2px; background: var(--chip); border-radius: 12px; padding: 2px; }
.speed button { border: 0; background: none; min-width: 44px; min-height: 40px; border-radius: 10px; font-size: 16px; }
.speed button.on { background: var(--accent); color: #fff; }
.money { font-size: 0.85rem; text-align: right; line-height: 1.2; }
.money b { font-size: 1rem; }

#coach { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: #fff3bf; color: #3b2f00; border-bottom: 1px solid #ffe066; font-size: 0.95rem; }
[data-iht-theme="dark"] #coach { background: #3a3013; color: #ffe8a3; border-color: #5c4b14; }
#coach .msg { flex: 1; }
#coach button { border: 0; border-radius: 10px; padding: 8px 12px; min-height: 40px; background: var(--accent); color: #fff; font-weight: 600; }
#coach .hide { background: none; color: inherit; min-width: 40px; }

main#main { flex: 1; display: flex; min-height: 0; }
#stage { position: relative; flex: 1; min-width: 0; overflow: hidden; }
#stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
#crumbs { position: absolute; top: 8px; left: 8px; z-index: 3; display: flex; gap: 6px; flex-wrap: wrap; }
#crumbs button { border: 0; border-radius: 999px; padding: 8px 14px; min-height: 40px; background: rgba(255, 250, 241, 0.94); box-shadow: var(--shadow); font-weight: 600; }
[data-iht-theme="dark"] #crumbs button { background: rgba(32, 26, 20, 0.94); }
#crumbs button.on { background: var(--accent); color: #fff; }
.zoom { position: absolute; right: 10px; top: 8px; z-index: 3; display: flex; flex-direction: column; gap: 8px; }
.zoom button { width: 44px; height: 44px; border-radius: 12px; border: 0; font-size: 22px; background: rgba(255, 250, 241, 0.94); box-shadow: var(--shadow); }
[data-iht-theme="dark"] .zoom button { background: rgba(32, 26, 20, 0.94); }
#palette { position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 4; display: flex; gap: 8px; overflow-x: auto; padding: 8px; background: rgba(255, 250, 241, 0.96); border-radius: 14px; box-shadow: var(--shadow); align-items: center; }
[data-iht-theme="dark"] #palette { background: rgba(32, 26, 20, 0.96); }
#palette .pb { flex: 0 0 auto; border: 2px solid transparent; border-radius: 12px; background: var(--card); padding: 6px 10px; min-height: 48px; text-align: left; font-size: 0.85rem; }
#palette .pb.on { border-color: var(--accent); }
#palette .pb small { display: block; color: var(--muted); }
#palette .pb.lock { opacity: 0.55; }
#palette .act { flex: 0 0 auto; border: 0; border-radius: 12px; padding: 10px 14px; min-height: 48px; font-weight: 700; background: var(--good); color: #fff; }
#palette .act.no { background: var(--chip); color: var(--text); }
#tip { position: absolute; left: 50%; transform: translateX(-50%); top: 58px; z-index: 5; background: rgba(20, 24, 30, 0.88); color: #fff; padding: 8px 14px; border-radius: 10px; font-size: 0.9rem; max-width: 90%; text-align: center; }

/* Side panel */
#panel { width: 400px; max-width: 46vw; border-left: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
#panelBody { overflow-y: auto; padding: 12px 14px 24px; flex: 1; }
#tabs { display: flex; gap: 2px; overflow-x: auto; background: var(--panel); border-top: 1px solid var(--line); padding: 4px 6px; }
#tabs button { flex: 0 0 auto; border: 0; background: none; border-radius: 10px; min-width: 64px; min-height: 52px; padding: 4px 8px; font-size: 0.78rem; display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--muted); }
#tabs button .i { font-size: 20px; }
#tabs button.on { background: var(--chip); color: var(--text); font-weight: 700; }
#tabs button .dot { position: absolute; }

h2 { font-size: 1.15rem; margin: 4px 0 10px; }
h3 { font-size: 1rem; margin: 18px 0 8px; }
p { margin: 6px 0; }
.muted { color: var(--muted); font-size: 0.9rem; }
.small { font-size: 0.85rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px; margin: 10px 0; box-shadow: var(--shadow); }
.card.hot { border-color: var(--accent); }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row.sp { justify-content: space-between; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat { background: var(--chip); border-radius: 10px; padding: 8px 10px; }
.stat b { display: block; font-size: 1.1rem; }
.stat span { font-size: 0.8rem; color: var(--muted); }
.btn { border: 0; border-radius: 10px; padding: 10px 14px; min-height: 44px; background: var(--accent); color: #fff; font-weight: 600; }
.btn.alt { background: var(--chip); color: var(--text); }
.btn.blue { background: var(--accent2); }
.btn.good { background: var(--good); color: #fff; }
.btn.sm { min-height: 36px; padding: 6px 10px; font-size: 0.85rem; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.75rem; font-weight: 700; background: var(--chip); }
.badge.m { background: #ffe8cc; color: #8a3b00; }
[data-iht-theme="dark"] .badge.m { background: #4a2f12; color: #ffc078; }
.badge.ok { background: #d3f9d8; color: #1b5e20; }
.badge.wait { background: #fff3bf; color: #6b5100; }
[data-iht-theme="dark"] .badge.ok { background: #183b1f; color: #8ce99a; }
[data-iht-theme="dark"] .badge.wait { background: #3d3311; color: #ffe066; }
.bar { height: 8px; background: var(--chip); border-radius: 99px; overflow: hidden; margin: 6px 0; }
.bar i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
td, th { padding: 5px 4px; border-bottom: 1px solid var(--line); text-align: left; }
td.n, th.n { text-align: right; font-variant-numeric: tabular-nums; }
tr.tot td { font-weight: 700; }
input[type=range] { width: 100%; min-height: 32px; }
input[type=text], input[type=number], select { font-size: 16px; padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--text); min-height: 44px; width: 100%; }
label.tog { display: flex; align-items: center; gap: 10px; min-height: 44px; }
label.tog input { width: 22px; height: 22px; }
.seg { display: flex; background: var(--chip); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 9px; min-height: 40px; font-weight: 600; }
.seg button.on { background: var(--accent); color: #fff; }
.log p { border-bottom: 1px solid var(--line); padding: 6px 0; margin: 0; font-size: 0.9rem; }
.log p.big { font-weight: 700; }
.log time { color: var(--muted); font-size: 0.78rem; display: block; }
.scene { width: 100%; aspect-ratio: 16/9; object-fit: cover; border-radius: 12px; display: block; }
.lockbox { border: 2px dashed var(--accent); border-radius: 14px; padding: 12px; margin: 10px 0; }

/* Title screen: the same look as HomeTown's own */
#title { position: fixed; inset: 0; z-index: 60; background: #1d150e center/cover no-repeat; display: flex; align-items: center; justify-content: center; }
#title[hidden] { display: none; }
#title .box { background: var(--panel); border-radius: 18px; padding: 22px 26px; width: min(560px, 94%); box-shadow: 0 6px 24px rgba(40, 25, 10, .22); max-height: 94%; overflow-y: auto; }
#title h1 { margin: 0; font-size: 34px; letter-spacing: .5px; }
#title .tag { color: var(--muted); margin: 2px 0 14px; }
#title h3 { margin: 16px 0 6px; }
#title .row { margin: 6px 0; }
#title .row.sp { justify-content: space-between; }
#title .muted { font-size: 13.5px; }
#title input { min-height: 40px; width: auto; }
#title .card { box-shadow: none; margin: 6px 0; padding: 8px 10px; border-radius: 10px; background: var(--chip); }
#title .card.hot { border: 1px solid var(--accent); }
.saves .card { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.saves .card b { display: inline; }
.biomes { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }
.biomes button { border: 1px solid var(--line); background: var(--chip); border-radius: 10px; padding: 10px 6px; font-size: 14px; white-space: pre-line; min-height: 48px; }
.biomes button.on { outline: 2px solid var(--accent); }
.warn { color: var(--bad); }
.btn.red { background: var(--bad); color: #fff; }
.btn.small { min-height: 40px; padding: 6px 10px; font-size: 0.875rem; }
.pick { display: grid; gap: 10px; }
.pick button.opt { text-align: left; border: 2px solid var(--line); background: var(--card); border-radius: 14px; padding: 14px; min-height: 64px; }
.pick button.opt b { display: block; font-size: 1.02rem; }
.regionPick { position: relative; height: 340px; border-radius: 14px; overflow: hidden; border: 1px solid var(--line); margin: 8px 0; }
.regionPick canvas { width: 100%; height: 100%; display: block; touch-action: none; }
@media (max-width: 760px) {
  #title .box { padding: calc(16px + env(safe-area-inset-top)) 16px 18px; width: 100%; max-height: 100%; height: 100%; border-radius: 0; }
  #title h1 { font-size: 28px; }
  .saves .card { flex-wrap: wrap; }
  .regionPick { height: 300px; }
}
#top .menu { border: 0; background: none; min-width: 44px; min-height: 44px; border-radius: 10px; font-size: 20px; }
#top .menu:hover { background: var(--chip); }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,0.45); display: grid; place-items: center; padding: 12px; }
.modal[hidden] { display: none; }
.modal .sheet { position: relative; background: var(--panel); border-radius: 18px; max-width: 560px; width: 100%; max-height: 88dvh; overflow-y: auto; padding: 18px; box-shadow: 0 10px 40px rgba(0,0,0,0.3); }
.modal .x { position: absolute; top: 8px; right: 8px; border: 0; background: var(--chip); border-radius: 10px; width: 44px; height: 44px; font-size: 18px; }
#toast { position: fixed; left: 50%; bottom: 130px; transform: translateX(-50%); background: #2e2218; color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 50; max-width: 92vw; box-shadow: var(--shadow); }

/* Phones */
@media (max-width: 760px) {
  #top { gap: 6px; padding: 4px 6px; }
  .brand span { font-size: 0.75rem; }
  .money { order: 5; width: 100%; display: flex; gap: 12px; justify-content: space-between; text-align: left; }
  .speed button { min-width: 44px; }
  main#main { flex-direction: column; }
  #panel { width: 100%; max-width: none; border-left: 0; border-top: 1px solid var(--line); height: 44dvh; }
  #panel.min { height: 0; overflow: hidden; border: 0; }
  #stage { min-height: 38dvh; }
  #coach { flex-wrap: wrap; font-size: 0.9rem; padding: 6px 10px; gap: 6px; }
  #coach .msg { flex: 1 1 calc(100% - 60px); }
  #coach button:not(.hide) { margin-left: auto; min-height: 40px; padding: 6px 12px; }
  #toast { top: calc(8px + env(safe-area-inset-top)); bottom: auto; }
  #tip { font-size: 0.85rem; top: 104px; }
  #palette { bottom: 6px; }
}
@media (min-width: 1000px) { #panel { width: 430px; } }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
