:root {
  --bg: #0f141a;
  --panel: #161e27;
  --card: #1c2631;
  --line: #2c3a48;
  --text: #dfe6ee;
  --muted: #8a9aab;
  --gold: #e0b54a;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text); overflow: hidden;
  font-family: -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif; font-size: 14px;
}
button {
  font: inherit; color: var(--text); background: #26323f; border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 12px; cursor: pointer; transition: background .15s;
}
button:hover:not(:disabled) { background: #314152; }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: #b8892a; border-color: #d9a640; color: #1a1205; font-weight: 700; }
button.primary:hover:not(:disabled) { background: #d09c33; }
.hidden { display: none !important; }

#app { display: grid; grid-template-rows: auto 1fr; height: 100vh; height: 100dvh; }
#topbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 6px 12px;
  background: linear-gradient(#1e2833, #18212b); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 900; font-size: 20px; letter-spacing: 4px; color: var(--gold); text-shadow: 0 1px 0 #000; margin-right: 6px; }
.pill { background: #222d39; border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; white-space: nowrap; }
#funds { display: flex; gap: 6px; flex-wrap: wrap; }
#funds .pill { border-left: 4px solid var(--c); opacity: .7; }
#funds .pill.active { opacity: 1; box-shadow: 0 0 0 1px var(--c); }
#funds small { color: #8fd18f; }
.spacer { flex: 1; }
.sep { width: 1px; height: 22px; background: var(--line); margin: 0 2px; }

main { display: grid; grid-template-columns: 1fr 300px; min-height: 0; }
#mapWrap { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#map { width: 100%; height: 100%; display: block; touch-action: none; cursor: pointer; }
#panel {
  background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto;
  padding: 10px; display: flex; flex-direction: column; gap: 10px; min-height: 0;
}
#mini { width: 100%; display: block; border-radius: 6px; border: 1px solid var(--line); cursor: crosshair; touch-action: none; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 10px; }
.card h3 { margin: 0 0 6px; font-size: 14px; color: var(--gold); }
.stats { display: flex; flex-wrap: wrap; gap: 4px 10px; margin: 4px 0; }
.stats.small { font-size: 12px; color: var(--muted); }
.warn { color: #ffb347; }
.desc { color: var(--muted); font-size: 12px; margin-top: 4px; line-height: 1.5; }
.uhead { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; }
.tag { margin-left: auto; font-size: 11px; background: #2b3947; padding: 1px 6px; border-radius: 4px; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }
.hpbar { position: relative; height: 16px; background: #0d1217; border-radius: 4px; overflow: hidden; }
.hpbar i { position: absolute; inset: 0 auto 0 0; transition: width .3s; }
.hpbar span { position: relative; font-size: 11px; padding-left: 6px; line-height: 16px; font-weight: 700; text-shadow: 0 1px 1px #000; }
.atk { display: grid; grid-template-columns: repeat(5, 1fr); gap: 3px; margin-top: 6px; }
.atk span { background: #2a3644; border-radius: 4px; text-align: center; font-size: 12px; padding: 2px 0; }
.atk span b { color: var(--muted); font-weight: 400; margin-right: 3px; font-size: 11px; }
.atk span.zero { opacity: .35; }
.fc { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; text-align: center; }
.fc > div { background: #222d39; border-radius: 6px; padding: 6px; display: flex; flex-direction: column; }
.fc b { font-size: 26px; line-height: 1.2; }
.fc small { color: var(--muted); font-size: 11px; }
.good { color: #ffd166; } .bad { color: #ff8a7a; }
.logcard { flex: 1; min-height: 120px; display: flex; flex-direction: column; }
#log { overflow-y: auto; flex: 1; font-size: 12px; max-height: 260px; }
#log div { border-left: 3px solid; padding: 2px 6px; margin-bottom: 3px; line-height: 1.45; }
#log small { color: var(--muted); }

#actionMenu {
  position: absolute; z-index: 5; display: flex; flex-direction: column; gap: 3px; padding: 5px;
  background: rgba(20, 28, 37, .96); border: 1px solid #44566a; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.5);
}
#actionMenu button { text-align: left; min-width: 112px; padding: 7px 12px; }
#actionMenu button.atk { background: #6e2727; border-color: #a33c3c; }
#actionMenu button.cap { background: #2c5a34; border-color: #3f8a4c; }
#actionMenu button.muted { color: var(--muted); }
#actionMenu small { color: #cfe8cf; }

#zoomCtl { position: absolute; right: 10px; bottom: 10px; display: flex; flex-direction: column; gap: 4px; }
#zoomCtl button { width: 36px; height: 36px; padding: 0; font-size: 18px; background: rgba(30,40,52,.85); }

#banner {
  position: absolute; left: 50%; top: 38%; transform: translate(-50%, -50%); pointer-events: none;
  padding: 14px 44px; font-size: 26px; font-weight: 900; letter-spacing: 3px; white-space: nowrap;
  background: linear-gradient(90deg, transparent, var(--c) 18%, var(--c) 82%, transparent);
  color: #fff; text-shadow: 0 2px 4px rgba(0,0,0,.6);
}
#banner.show { animation: bannerIn 1.4s ease forwards; }
@keyframes bannerIn {
  0% { opacity: 0; transform: translate(-30%, -50%); }
  18%, 75% { opacity: 1; transform: translate(-50%, -50%); }
  100% { opacity: 0; transform: translate(-70%, -50%); }
}

#modal {
  position: fixed; inset: 0; z-index: 20; background: rgba(5, 8, 12, .7);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
#modalBox {
  background: var(--panel); border: 1px solid #3a4b5e; border-radius: 12px; padding: 18px 20px;
  width: min(640px, 100%); max-height: 90vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.6);
}
#modalBox h2 { margin: 0 0 6px; color: var(--gold); }
.sub { color: var(--muted); margin-bottom: 12px; }
.row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.prod-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.prod { display: flex; gap: 10px; align-items: flex-start; text-align: left; padding: 8px; }
.prod canvas { width: 48px; height: 36px; flex: none; margin-top: 2px; }
.pinfo { flex: 1; min-width: 0; }
.pname { font-weight: 700; display: flex; justify-content: space-between; }
.pcost { color: var(--gold); }
.form { display: grid; gap: 10px; }
.form label { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.form select, .form input { font: inherit; background: #0f151c; color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; min-width: 170px; }
.inline { display: flex; gap: 6px; }
.inline input { min-width: 0; width: 124px; }
.help h4 { color: var(--gold); margin: 14px 0 4px; }
.help p, .help li { line-height: 1.6; color: #c8d2dc; }
.help ul { padding-left: 20px; margin: 4px 0; }
.ut { border-collapse: collapse; width: 100%; font-size: 12px; }
.ut th, .ut td { border: 1px solid var(--line); padding: 3px 5px; text-align: center; }
.ut th { background: #222d39; }
.ut td:first-child { text-align: left; }
.ut td.zero { color: #4b5866; }
.victory { text-align: center; padding: 20px 0 6px; }
.victory .big { font-size: 40px; font-weight: 900; color: #fff; text-shadow: 0 0 24px var(--c), 0 2px 0 var(--c); letter-spacing: 4px; }
.victory p { color: var(--muted); }

@media (max-width: 820px) and (orientation: portrait) {
  main { grid-template-columns: 1fr; grid-template-rows: 1fr 34vh; }
  #panel { border-left: none; border-top: 1px solid var(--line); display: grid; grid-template-columns: 1fr 1fr; align-content: start; }
  #mini { grid-column: 1 / -1; max-width: 260px; }
  .logcard { grid-column: 1 / -1; }
  .brand { font-size: 16px; }
  .sep { display: none; }
  #topbar button { padding: 5px 9px; font-size: 13px; }
  .prod-list { grid-template-columns: 1fr; }
}

/* ----- 劇情模式 ----- */
.brand { cursor: pointer; }
#zoomCtl #zLabel { font-size: 14px; font-weight: 700; color: var(--muted); }
#zoomCtl #zLabel.on { color: #1a1205; background: var(--gold); border-color: var(--gold); }
.obj-card { border-color: #6b5a2a; background: linear-gradient(#2a2617, var(--card)); }
ul.obj { list-style: none; padding: 0; margin: 6px 0 0; font-size: 13px; line-height: 1.7; }
ul.obj li.lose { color: #e79a90; font-size: 12px; }
ul.obj li.bonus { color: #a8dcff; }
ul.obj.big { font-size: 15px; margin: 14px 0; }
.mtitle { text-align: center; padding: 6px 0; }
.mtitle .chap { color: var(--muted); letter-spacing: 6px; }
.mtitle h2 { font-size: 30px; margin: 4px 0 !important; letter-spacing: 6px; }
.dhead { color: var(--muted); font-size: 12px; letter-spacing: 3px; margin-bottom: 10px; }
.dialog { display: flex; gap: 14px; align-items: flex-start; min-height: 96px; }
.portrait {
  flex: none; width: 72px; height: 72px; border-radius: 12px; display: grid; place-items: center;
  font-size: 32px; font-weight: 900; color: #fff; background: linear-gradient(145deg, var(--c), #10161d);
  border: 2px solid var(--c); box-shadow: 0 0 18px color-mix(in srgb, var(--c) 40%, transparent);
}
.dname { font-weight: 800; font-size: 16px; margin-bottom: 6px; }
.dname small { color: var(--muted); font-weight: 400; margin-left: 8px; font-size: 12px; }
.dtext { font-size: 16px; line-height: 1.75; color: #e8edf2; animation: fadeIn .25s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
button.muted { color: var(--muted); background: transparent; }
.debrief { text-align: center; padding: 10px 0; }
.debrief .big { font-size: 36px; font-weight: 900; letter-spacing: 6px; }
.debrief.win .big { color: var(--gold); text-shadow: 0 0 20px rgba(224,181,74,.5); }
.debrief.lose .big { color: #e2574c; }
.debrief p { color: var(--muted); }
.bonuslist { margin: 8px auto; color: #a8dcff; font-size: 13px; }
.branch { margin-top: 14px; padding: 12px; background: #1c2631; border-radius: 8px; line-height: 1.8; }
.branch b { color: var(--gold); }
.ending { text-align: center; padding: 18px 6px; }
.ending .rank { color: var(--c); letter-spacing: 8px; font-size: 14px; }
.ending .big { font-size: 38px; font-weight: 900; color: #fff; text-shadow: 0 0 24px var(--c); margin: 8px 0 16px; letter-spacing: 4px; }
.ending p { line-height: 1.9; color: #d5dde5; text-align: left; }
.tree { display: flex; flex-direction: column; gap: 8px; margin: 14px 0; }
.tier { display: flex; align-items: center; gap: 10px; }
.tlabel { width: 56px; flex: none; color: var(--muted); font-size: 12px; text-align: right; }
.tnodes { display: flex; gap: 8px; flex-wrap: wrap; flex: 1; }
.node { flex: 1; min-width: 90px; text-align: center; padding: 8px 6px; border-radius: 8px; border: 1px dashed #3a4b5e; color: #56667a; font-size: 13px; }
.node.seen { border-style: solid; color: var(--text); background: #1f2a36; }
.node.end.seen { border-color: var(--gold); color: var(--gold); }
.node.cur { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold); color: var(--gold); }
.mainmenu { display: flex; flex-direction: column; gap: 10px; align-items: stretch; padding: 10px 20px; }
.mainmenu .logo { text-align: center; font-size: 52px; font-weight: 900; letter-spacing: 14px; color: var(--gold); text-shadow: 0 3px 0 #000, 0 0 30px rgba(224,181,74,.35); }
.mainmenu .tagline { text-align: center; color: var(--muted); margin-bottom: 12px; letter-spacing: 3px; }
.mainmenu button.big { font-size: 17px; padding: 12px; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.mainmenu button small { font-size: 12px; opacity: .75; font-weight: 400; }

/* ----- 陣營 ----- */
.fac { font-size: 11px; color: var(--muted); margin-left: 6px; }
.facs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.fac-card { border: 1px solid var(--c); border-left-width: 4px; border-radius: 8px; padding: 8px 10px; background: #18212b; }
.fac-card b { display: block; }
.fac-card small { color: var(--muted); }
.fac-card p { margin: 6px 0 0; font-size: 12px; color: #c8d2dc; line-height: 1.5; }
.ut tr.uniq td { color: var(--gold); font-weight: 700; }
.tablewrap { overflow-x: auto; }

/* ----- 共用新元件 ----- */
#btnMore { display: none; font-size: 16px; padding: 4px 10px; }
#btnPanel {
  position: absolute; right: 10px; top: 10px; width: 36px; height: 36px; padding: 0; font-size: 18px;
  background: rgba(30,40,52,.85); z-index: 4;
}
#app.panel-off main { grid-template-columns: 1fr 0; }
#app.panel-off #panel { display: none; }
#toast {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 6; pointer-events: none;
  background: rgba(10,14,20,.92); border: 1px solid var(--gold); color: var(--gold); padding: 6px 14px; border-radius: 999px; font-weight: 700;
}
.moregrid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.moregrid button { padding: 12px; font-size: 15px; }
#rotateHint { display: none; }
button, #map { touch-action: manipulation; }
#map { touch-action: none; }

/* 直向手機：提示橫放 */
@media (max-width: 600px) and (orientation: portrait) {
  #rotateHint {
    display: flex; position: fixed; inset: 0; z-index: 50; flex-direction: column; align-items: center; justify-content: center;
    background: rgba(8,11,15,.96); color: var(--text); text-align: center; gap: 12px;
  }
  #rotateHint .phone { font-size: 56px; animation: rot 2s ease-in-out infinite; }
  #rotateHint p { font-size: 18px; line-height: 1.6; }
  body.rotate-ok #rotateHint { display: none; }
}
@keyframes rot { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }

/* ----- 手機橫向 ----- */
@media (max-height: 520px) and (orientation: landscape) {
  body { font-size: 12px; }
  #app { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
  #topbar { flex-wrap: nowrap; gap: 6px; padding: 4px 8px; min-height: 40px; overflow: hidden; }
  .brand { font-size: 15px; letter-spacing: 2px; margin-right: 0; }
  #turnInfo { padding: 3px 8px; font-size: 12px; }
  #funds { flex-wrap: nowrap; gap: 4px; }
  #funds .pill { padding: 3px 7px; font-size: 12px; }
  #funds .pill small { display: none; }
  #btnCampaign, #btnNew, #btnSave, #btnLoad, #btnHelp, .sep { display: none; }
  #btnMore { display: inline-block; }
  #topbar button { padding: 5px 10px; font-size: 13px; white-space: nowrap; }
  main { grid-template-columns: 1fr 210px; }
  #panel { padding: 6px; gap: 6px; }
  #mini { margin: 0 auto; }
  .card { padding: 6px 8px; border-radius: 6px; }
  .card h3 { font-size: 12px; margin-bottom: 3px; }
  .stats { gap: 2px 8px; }
  .desc { font-size: 11px; }
  ul.obj { font-size: 11.5px; line-height: 1.55; }
  .atk span { font-size: 11px; padding: 1px 0; }
  .logcard { min-height: 80px; }
  #log { max-height: 120px; font-size: 11px; }
  #forecast { display: none; }
  #zoomCtl { right: 8px; bottom: 8px; gap: 3px; }
  #zoomCtl button, #btnPanel { width: 34px; height: 34px; font-size: 15px; }
  #btnPanel { right: 8px; top: 8px; }
  #actionMenu button { min-height: 40px; min-width: 120px; font-size: 14px; }
  #banner { font-size: 18px; padding: 8px 30px; }
  #modal { padding: 8px; }
  #modalBox { padding: 12px 16px; max-height: calc(100dvh - 16px); width: min(760px, 100%); border-radius: 10px; }
  #modalBox h2 { font-size: 18px; }
  .row { margin-top: 10px; }
  .row button { padding: 8px 16px; }
  .prod-list { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .prod { padding: 6px; gap: 6px; }
  .prod .desc { display: none; }
  .prod canvas { width: 40px; height: 30px; }
  .mainmenu { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 4px; }
  .mainmenu .logo, .mainmenu .tagline { grid-column: 1 / -1; }
  .mainmenu .logo { font-size: 34px; letter-spacing: 10px; }
  .mainmenu .tagline { margin-bottom: 4px; }
  .mainmenu button.big { font-size: 15px; padding: 10px; }
  .dialog { min-height: 70px; gap: 10px; }
  .portrait { width: 54px; height: 54px; font-size: 24px; border-radius: 10px; }
  .dtext { font-size: 14px; line-height: 1.6; }
  .dname { font-size: 14px; margin-bottom: 3px; }
  .mtitle h2 { font-size: 22px; }
  ul.obj.big { font-size: 13px; margin: 8px 0; columns: 2; }
  .tree { gap: 5px; margin: 8px 0; }
  .node { padding: 5px; font-size: 12px; }
  .debrief .big, .ending .big { font-size: 26px; }
  .ending p { font-size: 13px; line-height: 1.7; }
  .facs { margin-top: 8px; }
  .fac-card p { display: none; }
  .form { gap: 6px; grid-template-columns: 1fr 1fr; column-gap: 16px; }
  .form select, .form input { min-width: 0; padding: 5px; }
}
/* 很矮的螢幕（如 iPhone SE 橫向）：側欄更窄 */
@media (max-height: 400px) and (orientation: landscape) {
  main { grid-template-columns: 1fr 190px; }
}
@media (max-width: 820px) and (orientation: portrait) {
  #btnCampaign, #btnNew, #btnSave, #btnLoad, #btnHelp, .sep { display: none; }
  #btnMore { display: inline-block; }
  #funds .pill small { display: none; }
}
