:root {
  --bg: #1d2124; --panel: #262b2f; --panel2: #2e3439; --ink: #e8e6e1; --muted: #9aa0a4; --line: #3a4146;
  --accent: #4fb89f; --warn: #ffb020;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;   /* 圖表色（已通過色盲檢查） */
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) { --bg: #e9e6df; --panel: #fbfaf7; --panel2: #f1eee8; --ink: #2b2b2b; --muted: #6f6f6f; --line: #d8d2c5; --accent: #2f8f78;
    --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body { background: var(--bg); color: var(--ink); font: 14px/1.5 system-ui, "Microsoft JhengHei", "PingFang TC", sans-serif; }

#app {
  height: 100dvh; display: grid;
  grid-template-columns: 150px 1fr 230px; grid-template-rows: 1fr auto;
  grid-template-areas: "pal stage side" "bar bar bar";
}
#palette { grid-area: pal; overflow-y: auto; padding: 8px; background: var(--panel); border-right: 1px solid var(--line); }
#stageWrap { grid-area: stage; position: relative; display: flex; align-items: center; justify-content: center; min-width: 0; min-height: 0; padding: 8px; }
#side { grid-area: side; overflow-y: auto; padding: 8px 10px; background: var(--panel); border-left: 1px solid var(--line); }
#bar { grid-area: bar; display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; padding: 8px 12px; background: var(--panel); border-top: 1px solid var(--line); }

#cv { display: block; border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.35); touch-action: none; cursor: default; }

h3 { font-size: 13px; margin: 10px 0 6px; color: var(--muted); font-weight: 600; letter-spacing: .05em; }
h3 small { float: right; font-weight: 400; }
h4 { margin: 0 0 6px; font-size: 15px; }
h4 small { color: var(--muted); font-weight: 400; }

.items { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 6px; }
.pitem { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 4px 2px 2px; text-align: center;
  cursor: grab; touch-action: none; user-select: none; }
.pitem:hover { border-color: var(--accent); }
.pitem canvas { width: 100%; max-width: 64px; height: auto; aspect-ratio: 64 / 44; display: block; margin: 0 auto; pointer-events: none; background: #dfe9e4; border-radius: 6px; }
.pitem span { font-size: 12px; display: block; pointer-events: none; }
.ghost { position: fixed; width: 64px; height: 44px; pointer-events: none; transform: translate(-50%, -50%) scale(1.3); z-index: 99; opacity: .85; }

section { border-bottom: 1px solid var(--line); padding-bottom: 8px; }
.kv { display: flex; justify-content: space-between; gap: 8px; }
.kv span { color: var(--muted); }
.bar { height: 6px; background: var(--panel2); border-radius: 3px; overflow: hidden; margin: 2px 0 6px; }
.bar i { display: block; height: 100%; }
.btns { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.muted { color: var(--muted); font-size: 13px; margin: 6px 0; }
#setPanel label { display: block; cursor: pointer; padding: 1px 0; }
#setPanel .sub { color: var(--muted); font-size: 12px; margin: 8px 0 2px; }
#setPanel select { width: 100%; }

button, select { font: inherit; color: var(--ink); background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 3px 10px; cursor: pointer; touch-action: manipulation; }
button:hover { border-color: var(--accent); }
button.on, button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.grp { display: flex; align-items: center; gap: 6px; }
.grp > span { color: var(--muted); }
.grp.right { margin-left: auto; }
#water { width: 140px; }

.floating { position: absolute; top: 16px; right: 16px; display: none; opacity: .4; z-index: 5; }
.floating:hover { opacity: 1; }

/* 觀賞模式：隱藏所有面板（模擬之後的桌面模式） */
body.view #app { grid-template-columns: 1fr; grid-template-rows: 1fr; grid-template-areas: "stage"; }
body.view #palette, body.view #side, body.view #bar { display: none; }
body.view .floating { display: block; }
body.view #stageWrap { padding: 0; }
body.view #cv { border-radius: 0; box-shadow: none; cursor: pointer; }

#toast { position: fixed; left: 50%; bottom: 70px; transform: translateX(-50%); background: rgba(0,0,0,.75); color: #fff; padding: 6px 14px; border-radius: 6px; opacity: 0; transition: opacity .2s; pointer-events: none; }
#toast.show { opacity: 1; }

#rotateHint { display: none; position: fixed; inset: 0; background: var(--bg); z-index: 100; align-items: center; justify-content: center; text-align: center; font-size: 20px; }
@media (orientation: portrait) and (max-width: 760px) { #rotateHint { display: flex; } }

/* 小螢幕（手機橫拿） */
@media (max-width: 1000px), (max-height: 520px) {
  #app { grid-template-columns: 112px 1fr 180px; }
  .items { grid-template-columns: minmax(0, 1fr); }
  #bar { gap: 4px 10px; padding: 4px 8px; }
  #bar button { padding: 2px 7px; }
  #water { width: 90px; }
}

/* 手機橫拿（畫面很矮）：工具列單行可滑動、右側資訊改成抽屜、左側面板變窄 */
#sideToggle { display: none; }
@media (max-height: 520px) {
  #app { grid-template-columns: 96px 1fr; grid-template-areas: "pal stage" "bar bar"; }
  #palette { padding: 4px; }
  #palette .tabs { grid-template-columns: 1fr 1fr; gap: 3px; margin-bottom: 6px; }
  #palette .tabs button { padding: 2px 0; font-size: 11px; line-height: 1.25; }
  .items { gap: 4px; }
  .pitem span { font-size: 11px; }
  #stageWrap { padding: 4px; }
  #side { position: fixed; top: 0; right: 0; bottom: 0; width: min(260px, 72vw); z-index: 30; padding-top: 34px;
    transform: translateX(105%); transition: transform .2s; box-shadow: -6px 0 20px rgba(0,0,0,.4); }
  body.sideOpen #side { transform: none; }
  #sideToggle { display: block; position: fixed; top: 6px; right: 6px; z-index: 31; padding: 2px 10px; opacity: .9; }
  body.sideOpen #sideToggle { background: var(--accent); border-color: var(--accent); color: #fff; }
  body.view #sideToggle { display: none; }
  #bar { flex-wrap: nowrap; overflow-x: auto; gap: 4px 8px; padding: 3px 6px; scrollbar-width: thin; }
  #bar .grp { flex: none; gap: 4px; }
  #bar .grp > span { display: none; }
  #bar button, #bar select { padding: 2px 6px; font-size: 13px; }
  #water { width: 80px; }
}
@media (max-height: 520px) and (prefers-reduced-motion: reduce) { #side { transition: none; } }

/* 族群圖、事件 */
.legend { display: flex; flex-wrap: wrap; gap: 2px 10px; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.legend i, #chartTip i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 4px; }
.chartBox { position: relative; }
#chart { display: block; cursor: crosshair; touch-action: none; }
#chartTip { display: none; position: absolute; top: 4px; width: 92px; background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; font-size: 12px; pointer-events: none; }
#chartTip div { display: flex; align-items: center; }
#chartTip div b { margin-left: auto; }
.ev { font-size: 12px; line-height: 1.45; padding: 1px 0; }
.ev time { color: var(--muted); margin-right: 6px; font-variant-numeric: tabular-nums; }

.sizeSlider { width: 100%; margin: 0 0 4px; }

/* 平衡面板 */
#balance { position: absolute; top: 16px; right: 16px; bottom: 16px; width: min(420px, calc(100% - 32px)); z-index: 6;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 30px rgba(0,0,0,.4);
  display: flex; flex-direction: column; }
#balance[hidden] { display: none; }
.balhead { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.balhead small { color: var(--muted); }
.balhead span { margin-left: auto; display: flex; gap: 4px; }
#balBody { overflow-y: auto; padding: 6px 12px 12px; }
#balBody summary { cursor: pointer; padding: 6px 0; font-weight: 600; }
.balgrid { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: center; padding: 0 0 8px 12px; font-size: 13px; }
.balgrid label { color: var(--muted); }
.balv input { width: 64px; font: inherit; color: var(--ink); background: var(--panel2); border: 1px solid var(--line); border-radius: 5px; padding: 1px 4px; }
.balv.changed input { border-color: var(--warn); }
button:disabled { opacity: .4; cursor: default; }

/* 物件面板頁籤 */
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin: 2px 0 10px; }
.tabs button { padding: 4px 2px; font-size: 13px; }
.items[hidden] { display: none; }
@media (max-width: 1000px), (max-height: 520px) { .tabs { grid-template-columns: 1fr; } }

/* 新魚缸：配置選單 */
#presetModal { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; padding: 16px; }
#presetModal[hidden] { display: none; }
.pmBox { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; width: min(900px, 100%); max-height: 100%; overflow: auto; padding: 14px; }
.pmHead { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.pmHead small { color: var(--muted); }
.pmHead button { margin-left: auto; }
#pmGrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.pmCard { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; text-align: left; padding: 10px; border-radius: 10px; }
.pmCard b { font-size: 15px; }
.pmCard span { font-size: 13px; color: var(--muted); line-height: 1.45; }
.pmCard small { font-size: 12px; color: var(--muted); }
.pmIcons { display: flex; gap: 4px; min-height: 44px; }
.pmIcons canvas { width: 64px; height: 44px; background: #dfe9e4; border-radius: 6px; }

/* 新手提示 */
#tut { position: fixed; inset: 0; z-index: 60; }
#tut[hidden] { display: none; }
.tutHole { position: fixed; border-radius: 10px; box-shadow: 0 0 0 9999px rgba(0,0,0,.55); outline: 2px solid var(--accent); outline-offset: 2px;
  transition: left .25s, top .25s, width .25s, height .25s; pointer-events: none; }
.tutBox { position: fixed; width: min(320px, calc(100vw - 16px)); background: var(--panel); color: var(--ink); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; box-shadow: 0 8px 30px rgba(0,0,0,.4); transition: left .25s, top .25s; }
.tutBox b { display: block; font-size: 16px; margin: 2px 0 4px; }
.tutBox p { margin: 0 0 12px; line-height: 1.6; }
.tutStep { font-size: 12px; color: var(--muted); }
.tutBtns { display: flex; gap: 6px; align-items: center; }
.tutBtns span { flex: 1; }
.tutBtns button[hidden] { display: none; }
@media (prefers-reduced-motion: reduce) { .tutHole, .tutBox { transition: none; } }

/* 成就 */
#achBox summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.achCat { font-size: 12px; color: var(--muted); margin: 8px 0 2px; }
.ach { display: flex; align-items: center; gap: 8px; padding: 3px 0; opacity: .5; }
.ach.done { opacity: 1; }
.ach .achIcon { font-size: 18px; width: 24px; text-align: center; filter: grayscale(1); }
.ach.done .achIcon { filter: none; }
.ach b { display: block; font-size: 13px; font-weight: 600; }
.ach em { display: block; font-style: normal; font-size: 12px; color: var(--muted); line-height: 1.35; }
.ach i { margin-left: auto; color: var(--accent); font-style: normal; font-weight: 700; }
#achPop { position: fixed; top: 14px; left: 50%; transform: translate(-50%, -140%); z-index: 70; display: flex; align-items: center; gap: 10px;
  background: var(--panel); color: var(--ink); border: 1px solid var(--accent); border-radius: 12px; padding: 8px 16px 8px 12px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4); transition: transform .3s; pointer-events: none; max-width: calc(100vw - 16px); }
#achPop.show { transform: translate(-50%, 0); }
#achPop .achIcon { font-size: 28px; }
#achPop small { display: block; font-size: 11px; color: var(--accent); font-weight: 600; letter-spacing: .05em; }
#achPop b { display: block; font-size: 15px; }
#achPop em { display: block; font-style: normal; font-size: 12px; color: var(--muted); }
@media (prefers-reduced-motion: reduce) { #achPop { transition: none; } }

/* 圖鑑 */
#codexModal { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; padding: 16px; }
#codexModal[hidden] { display: none; }
.cxBox { width: min(860px, 100%); }
.cxTabs { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); }
.cxGrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.cxCard { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 8px 6px; border-radius: 10px; }
.cxCard canvas { width: 96px; height: 66px; background: #dfe9e4; border-radius: 6px; image-rendering: pixelated; }
.cxCard b { font-size: 14px; }
.cxCard small { font-size: 12px; color: var(--muted); }
.cxCard.unseen canvas { filter: grayscale(1) brightness(.55); }
.cxCard.unseen b { color: var(--muted); }
.cxCard.full { outline: 2px solid var(--accent); }
.cxDetail h3 { margin: 0 0 6px; font-size: 20px; }
.cxDetail p { line-height: 1.55; margin: 4px 0; }
.cxDetail ul { margin: 4px 0 8px; padding-left: 20px; line-height: 1.5; font-size: 13px; }
.cxTop { display: flex; gap: 14px; align-items: flex-start; margin: 10px 0; }
.cxTop canvas { width: 192px; height: 132px; flex: none; background: #dfe9e4; border-radius: 8px; image-rendering: pixelated; }
.cxInfo { display: grid; gap: 2px; max-width: 420px; margin-bottom: 6px; }
ul.cxBeh { list-style: none; padding-left: 0; font-size: 14px; }
ul.cxBeh li { padding: 2px 0; color: var(--muted); }
ul.cxBeh li.done { color: inherit; }
@media (max-width: 600px) { .cxTop { flex-direction: column; } }

/* 小目標 */
.goal { display: flex; align-items: baseline; gap: 6px; padding: 2px 0; font-size: 13px; }
.goal em { font-style: normal; flex: 1; line-height: 1.4; }
.goal i { font-style: normal; color: var(--accent); font-weight: 700; font-size: 12px; white-space: nowrap; }
.goal.done em { color: var(--muted); text-decoration: line-through; }
.pmGoal { color: var(--accent) !important; font-weight: 600; }

/* 錦鯉品評會 */
#koiShowModal { position: fixed; inset: 0; z-index: 55; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; padding: 16px; }
#koiShowModal[hidden] { display: none; }
.ksBox { width: min(520px, 100%); }
.ksTable { width: 100%; border-collapse: collapse; margin: 8px 0; }
.ksTable td { padding: 6px 8px; border-bottom: 1px solid var(--line); }
.ksTable tr.mine td { color: var(--accent); font-weight: 700; }
.kcand { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; align-items: center; padding: 5px 0; border-bottom: 1px solid var(--line); }
.kcand b { font-size: 13px; }
.kcand small { grid-column: 1; color: var(--muted); font-size: 12px; line-height: 1.4; }
.kcand button { grid-column: 2; grid-row: 1 / span 2; }
