:root {
  --bg: #f4efe6;
  --panel: #fffaf2;
  --ink: #2b2620;
  --muted: #7a6f62;
  --line: #d9cdbb;
  --accent: #b5642a;
  --accent-ink: #fff;
  --hp: #c8483a;
  --mp: #3a6fc8;
  --world: #1d1a16;
  --glass: rgba(20, 17, 14, 0.62);
  --glass-ink: #f4ede2;
  --panel-w: 320px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1916; --panel: #25221e; --ink: #ece4d6; --muted: #a39887; --line: #3d3730; --accent: #d9874a;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1916; --panel: #25221e; --ink: #ece4d6; --muted: #a39887; --line: #3d3730; --accent: #d9874a;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font: 15px/1.5 system-ui, "Noto Sans TC", "Microsoft JhengHei", sans-serif; }
body { overscroll-behavior: none; }
button { font: inherit; background: var(--accent); color: var(--accent-ink); border: 0;
  border-radius: 8px; padding: 6px 14px; cursor: pointer; min-height: 36px; }
button:disabled { opacity: .5; cursor: default; }
button.icon { background: transparent; color: var(--muted); font-size: 20px; padding: 0 12px; }
input { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink); width: 100%; min-height: 36px; }
.hidden { display: none !important; }
.msg { color: var(--muted); min-height: 1.5em; margin: 6px 0 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }

#app { height: 100%; }
.screen.card { max-width: 380px; margin: 10vh auto 0; }
#login-form label { display: block; margin-bottom: 10px; }
h1 small { font-size: 60%; color: var(--muted); }
#char-list { list-style: none; padding: 0; }
#char-list li { display: flex; gap: 8px; justify-content: space-between; align-items: center;
  padding: 8px 0; border-bottom: 1px solid var(--line); }

/* ---- 遊戲畫面：地圖永遠填滿，其他東西浮在上面或靠右 ---- */
#scr-game { position: fixed; inset: 0; display: grid;
  grid-template-columns: 1fr var(--panel-w); }
#world { position: relative; overflow: hidden; background: var(--world); min-width: 0; }
#map { position: absolute; inset: 0; width: 100%; height: 100%; display: block;
  touch-action: none; image-rendering: pixelated; }

#hud { position: absolute; left: max(10px, env(safe-area-inset-left)); top: 10px; width: 210px;
  padding: 8px 10px; border-radius: 10px; background: var(--glass); color: var(--glass-ink);
  font-size: 13px; pointer-events: none; }
.hud-row { display: flex; gap: 8px; align-items: baseline; }
.hud-row.small { font-size: 12px; opacity: .85; margin-top: 2px; }
.bar { position: relative; height: 14px; background: rgba(255,255,255,.15); border-radius: 7px;
  overflow: hidden; margin-top: 4px; }
.bar i { position: absolute; inset: 0 auto 0 0; background: var(--hp); width: 0; }
.bar.mp i { background: var(--mp); }
.bar b { position: relative; font-size: 11px; font-weight: 500; padding-left: 6px; color: #fff; line-height: 14px; }

#chat-overlay { position: absolute; left: max(10px, env(safe-area-inset-left)); bottom: 10px;
  width: min(460px, 60%); display: flex; flex-direction: column; gap: 6px; }
#chat-log { margin: 0; padding: 6px 10px; list-style: none; max-height: 30vh; overflow: auto;
  background: var(--glass); color: var(--glass-ink); border-radius: 10px; font-size: 13px; }
#chat-log:empty { display: none; }
#chat-log li { word-break: break-all; }
#chat-log .sys { color: #e9c77b; }
#chat-form { display: flex; gap: 6px; }
#chat-form button { flex: none; white-space: nowrap; }
#chat-ch[data-ch="隊伍"] { background: #2a8a9a; }
#chat-ch[data-ch="家族"] { background: #3a5fc8; }
#chat-log li.ch-fm { color: #8fb3ff; }
#chat-log li.ch-party { color: #7fe0ea; }
#chat-log li.ch-occ { color: #b9a6ff; }
#chat-log li.ch-tell { color: #8fe88f; }
.ride-pets { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; padding: 8px 0; }
#chat-form input { background: rgba(255,255,255,.92); color: #222; border-color: transparent; }

#dock { display: none; position: absolute; right: max(10px, env(safe-area-inset-right)); bottom: 10px;
  flex-direction: column; gap: 8px; }
#dock button { width: 64px; height: 44px; padding: 0; background: var(--glass); color: var(--glass-ink);
  border: 1px solid rgba(255,255,255,.18); }

#rotate-hint { display: none; }

#panel { display: flex; flex-direction: column; min-height: 0; border-left: 1px solid var(--line);
  background: var(--panel); }
#tabs { display: flex; border-bottom: 1px solid var(--line); }
#tabs button[data-tab] { flex: 1; border-radius: 0; background: transparent; color: var(--muted);
  border-bottom: 2px solid transparent; }
#tabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
#panel-close { display: none; }
.tab { flex: 1; min-height: 0; overflow: auto; padding: 10px; }
.grid { list-style: none; margin: 0; padding: 0; display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 6px; }
.grid li, #pet-list li { border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px;
  font-size: 13px; cursor: pointer; }
.grid li.empty { color: var(--muted); cursor: default; }
#pet-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
#status-list { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 0 0 10px; }
#status-list dt { color: var(--muted); }

#battle { position: absolute; inset: 0; background: #000; display: flex; flex-direction: column; }
#battle-field { flex: 1; min-height: 0; position: relative; }
#battle-cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: manipulation; }
#battle-bar { background: color-mix(in srgb, var(--panel) 94%, transparent); padding: 6px 12px 10px;
  display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; }
#battle-msg { margin: 0; }
#battle-cmds { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 8px; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex;
  align-items: center; justify-content: center; padding: 16px; z-index: 10; }
.modal .card { width: min(480px, 100%); max-height: 90vh; overflow: auto; }
#npc-text { white-space: pre-wrap; font: inherit; margin: 0 0 10px; }
#npc-options { padding-left: 1.4em; margin: 0 0 10px; }
#npc-options li { padding: 6px; border-radius: 6px; cursor: pointer; }
#npc-options li:hover { background: var(--bg); }
#npc-buttons { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
#npc-input { margin-bottom: 10px; }
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink);
  color: var(--bg); padding: 8px 16px; border-radius: 8px; z-index: 20; }

/* ---- 手機與窄螢幕：地圖全螢幕、面板改成右側抽屜、功能鈕在右下 ---- */
@media (max-width: 900px), (max-height: 520px) {
  #scr-game { grid-template-columns: 1fr; }
  #panel { position: fixed; top: 0; right: 0; bottom: 0; width: min(var(--panel-w), 86vw);
    z-index: 5; transform: translateX(100%); transition: transform .18s ease;
    box-shadow: -4px 0 16px rgba(0,0,0,.25); padding-right: env(safe-area-inset-right); }
  #panel.open { transform: none; }
  #panel-close { display: block; }
  #dock { display: flex; }
  #hud { width: 180px; padding: 6px 8px; }
  #chat-overlay { width: min(380px, 55%); }
  #chat-log { max-height: 22vh; }
  #chat-overlay.collapsed #chat-form { display: none; }
}
/* 手機橫向：螢幕矮，把字和按鈕再縮一點、聊天框只留三四行 */
@media (max-height: 520px) and (orientation: landscape) {
  #hud { font-size: 12px; width: 170px; }
  #chat-log { max-height: 26vh; font-size: 12px; }
  #dock button { width: 56px; height: 38px; font-size: 13px; }
  .modal .card { max-height: 94vh; padding: 12px; }
}
/* 手機直向：可以玩，但提示轉橫向 */
@media (max-width: 600px) and (orientation: portrait) {
  #rotate-hint { display: block; position: absolute; left: 50%; top: 10px; transform: translateX(-50%);
    background: var(--glass); color: var(--glass-ink); padding: 4px 10px; border-radius: 8px;
    font-size: 12px; white-space: nowrap; pointer-events: none; }
  #hud { top: 40px; }
  .screen.card { margin: 6vh 16px 0; }
}

/* 道具商店 */
.shop-list { list-style: none; margin: 6px 0; padding: 0; max-height: 46vh; overflow: auto;
  border: 1px solid var(--line); border-radius: 8px; }
.shop-list li { display: flex; justify-content: space-between; gap: 8px; padding: 6px 10px; cursor: pointer;
  border-bottom: 1px solid var(--line); }
.shop-list li:last-child { border-bottom: 0; }
.shop-list li.on { background: color-mix(in srgb, var(--accent) 18%, transparent); }
.shop-list li.poor { color: var(--muted); }
.shop-list .price { white-space: nowrap; color: var(--muted); }
.shop-qty { width: 72px; }
label.qty { display: flex; align-items: center; gap: 6px; margin-right: auto; }

.grid li.equip { border-style: dashed; }
#party-box h4 { margin: 10px 0 4px; }
#party-list { list-style: none; margin: 0 0 8px; padding: 0; font-size: 13px; }
#party-list li { padding: 3px 0; border-bottom: 1px solid var(--line); }
label.row { display: flex; gap: 8px; align-items: center; margin: 6px 0; }
label.row select { flex: 1; min-height: 34px; }
.trade-list { list-style: none; margin: 0 0 8px; padding: 0; font-size: 13px; }
.trade-list li { padding: 3px 0; border-bottom: 1px solid var(--line); }
.trade-bar { display: flex; flex-wrap: wrap; gap: 6px; }
#tab-status h4, #tab-trade h4 { margin: 10px 0 4px; }
#flag-box label.row { margin: 2px 0; }
#flag-box input { width: auto; min-height: 0; }
.item-icon { width: 32px; height: 32px; object-fit: contain; image-rendering: pixelated; flex: none; }
.icon-blank { display: inline-block; width: 32px; height: 32px; flex: none; }
.with-icon { display: inline-flex; align-items: center; gap: 8px; }
.shop-list li { align-items: center; }

/* 自製視窗 */
.modal .card.wide { width: min(760px, 100%); }
.petstore { display: grid; grid-template-columns: minmax(160px, 1fr) 1.2fr; gap: 12px; }
.petstore-detail p { margin: 2px 0; }
.sprite-thumb { display: block; image-rendering: pixelated; }
.lucky-grid { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.lucky-card { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px; min-width: 110px;
  border: 1px solid var(--line); border-radius: 10px; }
.lucky-grid.multi .lucky-card { min-width: 120px; flex: 0 0 calc(20% - 10px); }
.lucky-card.big { border-color: #d6a400; background: color-mix(in srgb, #ffd54a 18%, transparent); }
.lucky-card b { color: #c08a00; }
.checkin-track { display: flex; flex-wrap: wrap; gap: 4px; margin: 8px 0; }
.checkin-track .day { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 50%;
  border: 1px solid var(--line); font-size: 12px; }
.checkin-track .day.done { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.checkin-track .day.next { outline: 2px dashed var(--accent); }
.checkin-track .day.mile { border: 2px solid #d6a400; }
.err { color: #c8483a; white-space: pre-wrap; }
.set-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
button.ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.dex-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; max-height: 50vh; overflow: auto; }
.dex-cell { display: flex; flex-direction: column; align-items: center; font-size: 12px; padding: 4px;
  border: 1px solid var(--line); border-radius: 8px; filter: grayscale(1); opacity: .55; cursor: pointer; }
.dex-cell.has { filter: none; opacity: 1; cursor: default; }
.share-card { display: grid; grid-template-columns: 140px 1fr; gap: 12px; }
.share-lines .h { color: var(--accent); font-weight: 600; margin-top: 6px; }
.share-lines .b { padding-left: 12px; color: #c08a00; }
.share-lines .cols { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; }
.share-lines hr { border: 0; border-top: 1px solid var(--line); }
#chat-log li.has-share { cursor: pointer; text-decoration: underline dotted; }
@media (max-width: 600px) { .petstore, .share-card { grid-template-columns: 1fr; } }
.menu-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 6px; }
#npc-options .tag { margin-left: 8px; color: #c8483a; }
.quiz-sil { display: flex; justify-content: center; }
.quiz-sil .silhouette { filter: brightness(0); }
.quiz-opts { display: flex; justify-content: center; gap: 10px; margin: 8px 0; }
.quiz-opt { display: flex; flex-direction: column; align-items: center; border: 1px solid var(--line); border-radius: 8px; padding: 4px; cursor: pointer; }
.quiz-opt.picked { outline: 2px solid var(--accent); }
.quiz-opt.right { outline: 3px solid #3f9a3a; }
#battle-timer { margin: 0; text-align: right; }
