:root {
  --paper: #f4ead2; --paper2: #ebdcb8; --ink: #2a2420; --indigo: #2f3e66; --vermilion: #c8452f; --gold: #d8a948; --moss: #6f8a43;
  --font: "Palatino Linotype", "Book Antiqua", Palatino, Georgia, "Hiragino Mincho ProN", serif;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
  --slot: 58px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; background: #2f6f8f; font-family: var(--font); color: var(--ink);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation; }
body { height: 100vh; height: 100dvh; }
#game { position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: block; outline: none; touch-action: none; }
button { font-family: var(--font); cursor: pointer; touch-action: manipulation; color: inherit; }
.hidden { display: none !important; }

/* ---------- top HUD */
#hud-top { position: fixed; top: calc(10px + var(--sat)); left: calc(12px + var(--sal)); right: calc(12px + var(--sar)); display: flex; align-items: flex-start; gap: 12px; pointer-events: none; z-index: 3; }
#hud-top > * { pointer-events: auto; }
.purse-box { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 6px 14px 6px 8px; background: var(--paper); border: 2px solid var(--indigo); border-radius: 22px; box-shadow: 0 3px 0 rgba(42,36,32,.35); font-size: 20px; }
.purse-box small { font-size: 13px; opacity: .7; }
.coin { width: 26px; height: 26px; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #ffe69a, var(--gold) 60%, #9c7a3c); border: 2px solid #8a6424; }
#mode-label { flex: 1; text-align: center; color: var(--paper); text-shadow: 0 1px 3px rgba(0,0,0,.6); font-size: 16px; padding-top: 10px; }
#mode-label:empty { visibility: hidden; }
.top-buttons { display: flex; gap: 6px; margin-left: auto; }
.top-buttons button, #touch button:not(#btn-action), #build-bar button { background: var(--paper); border: 2px solid var(--indigo); color: var(--indigo); border-radius: 12px; min-width: 44px; min-height: 44px; padding: 6px 12px; font-size: 15px; box-shadow: 0 3px 0 rgba(42,36,32,.35); }
.top-buttons button:active, #touch button:active, #build-bar button:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(42,36,32,.35); }
body.build-mode #btn-build { background: var(--vermilion); color: var(--paper); border-color: #7a2a1c; }
/* on touch the hotbar already has Build + Bag, and pinch replaces the zoom buttons */
body.touch #btn-zoom-in, body.touch #btn-zoom-out, body.touch #btn-build, body.touch #btn-bag { display: none; }

/* ---------- toasts */
#toasts { position: fixed; top: calc(64px + var(--sat)); left: 50%; transform: translateX(-50%); width: max-content; max-width: calc(100vw - 24px); display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; z-index: 5; }
.toast { display: flex; align-items: center; gap: 8px; background: rgba(244,234,210,.96); border: 2px solid var(--indigo); border-left: 6px solid var(--vermilion); border-radius: 10px; padding: 6px 14px; font-size: 15px; box-shadow: 0 4px 10px rgba(0,0,0,.18); animation: pop .25s ease-out; transition: opacity .5s, transform .5s; }
.toast.out { opacity: 0; transform: translateY(-8px); }
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

#bubble { position: fixed; transform: translate(-50%, -100%); background: var(--paper); border: 3px solid var(--vermilion); color: var(--vermilion); font-weight: bold; font-size: 26px; width: 42px; height: 42px; border-radius: 50%; display: flex; align-items: center; justify-content: center; animation: bounce .35s infinite alternate; pointer-events: none; z-index: 4; }
@keyframes bounce { to { margin-top: -6px; } }

/* ---------- hotbar */
#bottom { position: fixed; bottom: calc(10px + var(--sab)); left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; z-index: 3; }
#tool-hint { color: var(--paper); text-shadow: 0 1px 3px rgba(0,0,0,.7); font-size: 14px; text-align: center; max-width: 92vw; }
#hotbar { display: flex; gap: 6px; padding: 6px; background: rgba(244,234,210,.92); border: 2px solid var(--indigo); border-radius: 16px; box-shadow: 0 4px 0 rgba(42,36,32,.35); pointer-events: auto; max-width: calc(100vw - 16px - var(--sal) - var(--sar)); overflow-x: auto; scrollbar-width: none; }
#hotbar.compact { --slot: 50px; gap: 4px; padding: 4px; }
.slot { position: relative; width: var(--slot); height: var(--slot); flex: none; padding: 0; border-radius: 12px; border: 2px solid var(--paper2); background: #fbf5e6; display: flex; align-items: center; justify-content: center; }
.slot.sel { border-color: var(--vermilion); background: #fff3d6; box-shadow: inset 0 0 0 2px var(--vermilion); }
.slot.special { background: #efe3c4; }
.slot.empty .icon { opacity: .35; filter: grayscale(1); }
.slot .key { position: absolute; top: 1px; left: 5px; font-size: 11px; color: var(--indigo); opacity: .8; }
.slot .count { position: absolute; bottom: 1px; right: 4px; font-size: 13px; font-weight: bold; color: var(--ink); text-shadow: 0 0 3px #fff, 0 0 3px #fff; }
.slot .more { position: absolute; top: 0; right: 4px; font-size: 11px; color: var(--vermilion); }
.slot .glyph { font-size: 26px; color: var(--moss); }
.slot .tip { position: absolute; bottom: calc(var(--slot) + 8px); left: 50%; transform: translateX(-50%); white-space: nowrap; background: var(--ink); color: var(--paper); font-size: 12px; padding: 3px 8px; border-radius: 6px; opacity: 0; pointer-events: none; transition: opacity .15s; }
.icon { display: inline-block; width: calc(var(--slot) - 12px); height: calc(var(--slot) - 12px); background-repeat: no-repeat; background-position: center; flex: none; }
.icon.small { width: 26px; height: 26px; }
body.touch .slot .key { display: none; }

/* hover only where hover exists (no sticky hover after a tap on phones) */
@media (hover: hover) and (pointer: fine) {
  .top-buttons button:hover { background: #fff7e2; }
  .slot:hover { border-color: var(--gold); }
  .slot:hover .tip { opacity: 1; }
  .btn:hover:not(:disabled) { background: var(--gold); }
  .bag-item.clickable:hover, .row.tappable:hover { border-color: var(--gold); }
}

#seed-picker { position: fixed; z-index: 8; display: flex; flex-direction: column; gap: 4px; padding: 6px; width: 210px; background: var(--paper); border: 2px solid var(--indigo); border-radius: 14px; box-shadow: 0 6px 16px rgba(0,0,0,.25); animation: pop .15s ease-out; }
.seed-opt { display: flex; align-items: center; gap: 8px; min-height: 48px; border: 2px solid var(--paper2); background: #fbf5e6; border-radius: 10px; padding: 2px 10px 2px 4px; font-size: 15px; text-align: left; }
.seed-opt .icon { width: 40px; height: 40px; }
.seed-opt span { flex: 1; } .seed-opt.sel { border-color: var(--vermilion); }

/* ---------- context action button + rotate */
#touch { position: fixed; right: calc(14px + var(--sar)); bottom: calc(108px + var(--sab)); display: flex; pointer-events: none; z-index: 3; }
#touch > * { pointer-events: auto; }
.actions { display: flex; gap: 10px; align-items: flex-end; }
#btn-action { width: 76px; height: 76px; border-radius: 50%; padding: 0; background: var(--vermilion); color: var(--paper); border: 3px solid #7a2a1c; box-shadow: 0 4px 0 rgba(42,36,32,.4), 0 6px 14px rgba(0,0,0,.25); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0; font-size: 13px; line-height: 1.1; }
#btn-action .icon.act { width: 40px; height: 40px; }
#btn-action .lbl { font-size: 13px; font-weight: bold; color: var(--paper); text-shadow: 0 1px 2px rgba(0,0,0,.4); }
#btn-action .icon.act { background-color: rgba(244,234,210,.9); border-radius: 50%; }
#btn-action.reel { background: var(--indigo); border-color: var(--indigo); }
#btn-action.reel .lbl { font-size: 18px; }
#btn-action.bite { width: 96px; height: 96px; background: var(--vermilion); animation: pulse .3s infinite alternate; }
@keyframes pulse { to { transform: scale(1.1); } }
#btn-rotate { width: 52px; height: 52px; border-radius: 50%; font-size: 22px; padding: 0; display: none; }
body.build-mode:not(.touch) #btn-rotate { display: block; }
@media (hover: hover) and (pointer: fine) { #btn-action { width: 64px; height: 64px; } #btn-action .icon.act { width: 32px; height: 32px; } }

/* ---------- touch build bar */
#build-bar { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(118px + var(--sab)); display: flex; gap: 10px; z-index: 4; }
#build-bar button { min-width: 56px; min-height: 52px; font-size: 20px; border-radius: 16px; }
#bb-place { background: var(--moss) !important; color: var(--paper) !important; border-color: #3f5426 !important; font-size: 17px !important; padding: 6px 18px !important; }
#prop-menu { position: fixed; z-index: 8; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 8px; max-width: 300px; background: var(--paper); border: 2px solid var(--indigo); border-radius: 14px; box-shadow: 0 6px 16px rgba(0,0,0,.25); animation: pop .15s ease-out; }
#prop-menu .pm-title { width: 100%; font-weight: bold; color: var(--indigo); font-size: 15px; }
#prop-menu .btn { min-height: 44px; min-width: 56px; font-size: 15px; }

/* ---------- touch feedback */
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 2; overflow: hidden; }
.ripple { position: absolute; width: 16px; height: 16px; margin: -8px 0 0 -8px; border-radius: 50%; border: 3px solid rgba(255,246,220,.95); animation: ripple .5s ease-out forwards; }
.ripple.long { border-color: rgba(216,169,72,.95); }
@keyframes ripple { to { transform: scale(4); opacity: 0; } }
.float-label { position: absolute; transform: translate(-50%, -100%); white-space: nowrap; background: rgba(244,234,210,.95); border: 2px solid var(--indigo); border-radius: 10px; padding: 3px 10px; font-size: 14px; box-shadow: 0 3px 8px rgba(0,0,0,.2); }
.chip { position: absolute; transform: translate(-50%, -100%); width: 38px; height: 38px; border-radius: 50%; background: rgba(244,234,210,.95); border: 2px solid var(--gold); display: flex; align-items: center; justify-content: center; box-shadow: 0 3px 6px rgba(0,0,0,.2); }
.chip .icon.chip-icon { width: 28px; height: 28px; }
.chip b { position: absolute; top: -6px; right: -6px; min-width: 18px; height: 18px; border-radius: 9px; background: var(--vermilion); color: var(--paper); font-size: 11px; display: flex; align-items: center; justify-content: center; }
#joy { position: fixed; width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%; border: 3px solid rgba(244,234,210,.7); background: rgba(47,62,102,.18); pointer-events: none; z-index: 2; }
#joy .knob { position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px; border-radius: 50%; background: rgba(244,234,210,.9); border: 2px solid var(--indigo); }

/* ---------- panels / sheets */
#panel { position: fixed; inset: 0; background: rgba(31,41,69,.35); display: flex; align-items: center; justify-content: center; z-index: 10; }
.card { background: var(--paper); border: 3px solid var(--indigo); border-radius: 18px; box-shadow: 0 8px 0 rgba(42,36,32,.35), 0 20px 40px rgba(0,0,0,.25); padding: 16px 20px 18px; width: min(560px, 94vw); max-height: 86vh; max-height: 86dvh; overflow: auto; touch-action: pan-y; overscroll-behavior: contain;
  background-image: radial-gradient(rgba(140,110,60,.08) 1px, transparent 1px); background-size: 6px 6px; }
.grabber { display: none; }
.card-head { display: flex; align-items: center; gap: 10px; border-bottom: 2px solid var(--paper2); padding-bottom: 8px; margin-bottom: 8px; }
.card-head h2 { margin: 0; flex: 1; font-size: 22px; color: var(--indigo); }
.card-head h2::before { content: ""; display: inline-block; width: 10px; height: 10px; background: var(--vermilion); border-radius: 2px; transform: rotate(45deg); margin-right: 10px; vertical-align: middle; }
.purse { display: flex; align-items: center; gap: 4px; font-size: 15px; }
.close { background: none; border: none; font-size: 22px; color: var(--indigo); min-width: 44px; min-height: 44px; }
.npc { margin: 4px 0 10px; font-size: 14px; }
.tabs { display: flex; gap: 6px; margin-bottom: 8px; }
.tab { flex: 1; min-height: 44px; border: 2px solid var(--indigo); background: #fbf5e6; border-radius: 10px; padding: 6px; font-size: 15px; color: var(--indigo); }
.tab.on { background: var(--indigo); color: var(--paper); }
.list { display: flex; flex-direction: column; gap: 4px; }
.row { display: flex; align-items: center; gap: 10px; padding: 4px 6px; border-radius: 10px; background: rgba(255,255,255,.45); border: 2px solid transparent; }
.row.tappable { cursor: pointer; }
.row .icon { width: 40px; height: 40px; }
.row .name { flex: 1; font-size: 15px; }
.row .right { display: flex; align-items: center; gap: 6px; }
.qty { font-size: 13px; opacity: .7; min-width: 46px; text-align: right; }
.price { font-size: 13px; color: #7a5a1c; min-width: 64px; text-align: right; }
.btn { border: 2px solid var(--indigo); background: var(--paper); color: var(--indigo); border-radius: 9px; padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .4; cursor: default; }
.btn.big { background: var(--vermilion); color: var(--paper); border-color: #7a2a1c; padding: 8px; font-size: 15px; margin-bottom: 4px; }
body.touch .btn { min-height: 44px; min-width: 52px; padding: 8px 12px; font-size: 15px; }
.muted { opacity: .7; font-size: 14px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.bag-item { position: relative; display: flex; flex-direction: column; align-items: center; padding: 6px; border-radius: 12px; background: rgba(255,255,255,.5); border: 2px solid var(--paper2); }
.bag-item .icon { width: 46px; height: 46px; }
.bag-item.clickable { cursor: pointer; }
.bag-item .count { position: absolute; top: 4px; right: 8px; font-weight: bold; }
.bag-item .label { font-size: 12px; text-align: center; }
#help { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(31,41,69,.3); z-index: 9; padding: var(--sat) var(--sar) var(--sab) var(--sal); }
.controls { padding-left: 18px; line-height: 1.7; font-size: 14px; }
body.touch .help-desktop, body:not(.touch) .help-touch { display: none; }
kbd { background: #fff; border: 1px solid #b9a77c; border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; font-size: 12px; font-family: inherit; }
.link { background: none; border: none; color: var(--vermilion); text-decoration: underline; font-size: 14px; padding: 0; min-height: 32px; }
#dialog { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(31,41,69,.45); z-index: 30; }
.card.dlg { width: min(380px, 90vw); text-align: center; }
.dlg-text { font-size: 17px; margin: 6px 0 16px; }
.dlg-buttons { display: flex; gap: 10px; justify-content: center; }
.dlg-buttons .btn { min-height: 44px; min-width: 110px; font-size: 15px; margin: 0; }

/* ---------- loading */
#loading { position: fixed; inset: 0; background: #f4ead2; display: flex; align-items: center; justify-content: center; z-index: 20; transition: opacity .7s; }
#loading.done { opacity: 0; pointer-events: none; }
.loading-inner .title { font-size: 30px; color: var(--indigo); margin-bottom: 14px; letter-spacing: 1px; text-align: center; }
.loading-inner .bar { width: 260px; height: 10px; border: 2px solid var(--indigo); border-radius: 6px; overflow: hidden; margin: 0 auto; }
#loading-bar { height: 100%; width: 0; background: var(--vermilion); transition: width .2s; }

/* ---------- narrow / portrait phones: bottom sheets */
@media (max-width: 760px) and (orientation: portrait) {
  #hotbar.compact { --slot: 48px; }
  #mode-label { display: none; }
  .purse-box { font-size: 18px; padding: 4px 12px 4px 6px; }
  #tool-hint { font-size: 12px; }
  #panel { align-items: flex-end; }
  .card.sheet { width: 100%; max-height: 78vh; max-height: 78dvh; border-radius: 22px 22px 0 0; border-bottom: none; padding: 6px 16px calc(16px + var(--sab)); animation: sheet-up .22s ease-out; box-shadow: 0 -8px 30px rgba(0,0,0,.25); }
  .grabber { display: block; width: 48px; height: 5px; border-radius: 3px; background: var(--paper2); margin: 2px auto 6px; padding: 8px 0; background-clip: content-box; touch-action: none; }
  .card.sheet .card-head { touch-action: none; }
  .row .right .qty { display: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); }
}
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@media (max-width: 400px) { #hotbar.compact { --slot: 46px; gap: 3px; } .purse-box small { display: none; } }

/* ---------- landscape phones: side sheets, slimmer HUD */
@media (orientation: landscape) and (max-height: 520px) {
  #hotbar, #hotbar.compact { --slot: 46px; gap: 3px; padding: 3px; }
  #tool-hint { display: none; }
  #toasts { top: calc(56px + var(--sat)); }
  .toast { font-size: 13px; padding: 4px 10px; }
  #touch { bottom: calc(16px + var(--sab)); }
  #build-bar { bottom: calc(70px + var(--sab)); }
  .purse-box { min-height: 40px; font-size: 17px; }
  #mode-label { padding-top: 8px; font-size: 14px; }
  #panel { justify-content: flex-end; align-items: stretch; }
  .card.sheet { width: min(460px, 52vw); max-height: none; height: 100%; border-radius: 22px 0 0 22px; border-right: none; padding: calc(8px + var(--sat)) calc(14px + var(--sar)) calc(10px + var(--sab)) 16px; animation: sheet-left .22s ease-out; }
  .card.sheet .card-head { touch-action: none; }
  .npc { display: none; }
  .card-head h2 { font-size: 19px; }
  #help .card { max-height: 92dvh; }
}
.card.sheet.static { animation: none !important; }
@keyframes sheet-left { from { transform: translateX(100%); } to { transform: none; } }
