/* =====================================================================
 * fantamobile — 幻想國度 手機版介面樣式
 * 單欄、拇指操作優先：上＝房名/血條，中＝文字流，下＝在場人物＋羅盤＋快捷指令＋分頁列。
 * ===================================================================== */
:root {
  --bg: #14110e;
  --bg-2: #1d1914;
  --bg-3: #2a241c;
  --line: #3a3228;
  --ink: #e9e0cf;
  --ink-soft: #b8ab94;
  --ink-faint: #7d7263;
  --seal: #c0392b;
  --seal-soft: #e0685a;
  --gold: #d9b45a;
  --jade: #6fbf8e;
  --river: #6aa6d8;
  --fali: #a98be0;
  --hp: #d0503f;
  --neili: #4f8fd6;
  --focus: #d9b45a;
  --stamina: #5fb77a;
  --link: #7fd4e0;
  --radius: 10px;
  --tap: 44px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--ink); overflow: hidden; }
body {
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC", system-ui, sans-serif;
  font-size: 15px; line-height: 1.5;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; touch-action: manipulation; }
[data-cmd], [data-act], a#mxplink, a[id="mxplink"] { touch-action: manipulation; cursor: pointer; }

#fm-root { height: 100%; }
.fm-app {
  position: fixed; left: 0; right: 0; top: 0;
  height: 100vh; height: 100dvh;           /* JS 會再以 visualViewport 覆寫（鍵盤彈出時） */
  display: flex; flex-direction: column;
  padding-top: var(--safe-t);
}

/* ---------- 頂列：房名＋連線＋繁簡 ---------- */
.fm-top { flex: none; background: var(--bg-2); border-bottom: 1px solid var(--line); padding: 0 10px; }

/* 房間橫幅：沿用 /g/ 的水墨房景圖（1898×660），手機上只露中段一條 */
.fm-banner {
  position: relative; margin: 0 -10px; padding: 6px 10px 5px; min-height: 40px;
  display: flex; flex-direction: column; justify-content: flex-end;
  background: linear-gradient(180deg, #2a241c 0%, var(--bg-2) 100%);
  transition: min-height .2s;
}
.fm-banner.has-img { min-height: 104px; }
.fm-banner-img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; object-position: center 55%;
  pointer-events: none;
}
.fm-banner::after {   /* 底部壓暗，房名才讀得清楚 */
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(180deg, #0000 35%, #14110ecc 80%, var(--bg-2) 100%);
}
.fm-banner > .fm-top-row { position: relative; z-index: 1; }
.fm-banner.has-img .fm-room b { text-shadow: 0 1px 3px #000, 0 0 8px #000; font-size: 18px; }
.fm-banner.has-img .fm-room small { color: var(--ink-soft); text-shadow: 0 1px 2px #000; }
.fm-banner.has-img .fm-iconbtn { background: #14110ebb; }
/* 打字或抽屜開著時橫幅收成一條，讓位給文字區 */
.fm-app.is-typing .fm-banner.has-img, .fm-app.is-sheet .fm-banner.has-img { min-height: 40px; }
.fm-app.is-typing .fm-banner-img, .fm-app.is-sheet .fm-banner-img { opacity: .35; }
.fm-top-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.fm-conn { width: 9px; height: 9px; border-radius: 50%; background: var(--ink-faint); flex: none; }
.fm-conn.is-open { background: var(--jade); }
.fm-conn.is-closed { background: var(--seal); }
.fm-room { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 6px; overflow: hidden; }
.fm-room b { font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--ink); }
.fm-room small { color: var(--ink-faint); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fm-iconbtn {
  flex: none; min-width: 34px; height: 30px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg-3); color: var(--ink-soft); font-size: 13px; padding: 0 8px;
}
.fm-seal { background: var(--seal); color: #fff; border-color: var(--seal); font-weight: 700; }

/* 狀態列：文字區正下方（戰鬥列＋血條＋時辰） */
.fm-status { flex: none; background: var(--bg-2); border-top: 1px solid var(--line); }
.fm-status .fm-vitals:empty { display: none; }
/* 血條（五條並排＋時辰，點血條開人物頁、點時辰送 checktime） */
.fm-vitals { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)) auto; gap: 6px; padding: 5px 10px 6px; align-items: end; }
.fm-time {
  align-self: center; font-size: 11px; color: var(--gold); border: 1px solid #5a4a2a; border-radius: 6px;
  padding: 2px 6px; white-space: nowrap; background: var(--bg-3);
}
.fm-vital { min-width: 0; }
.fm-vital-lab { display: flex; justify-content: space-between; font-size: 10.5px; color: var(--ink-soft); line-height: 1.2; white-space: nowrap; }
.fm-vital-lab span:last-child { color: var(--ink-faint); overflow: hidden; text-overflow: ellipsis; }
.fm-bar { height: 5px; background: #0006; border-radius: 3px; overflow: hidden; margin-top: 2px; }
.fm-bar i { display: block; height: 100%; width: 0; border-radius: 3px; transition: width .25s; }
.fm-bar.hp i { background: var(--hp); }
.fm-bar.neili i { background: var(--neili); }
.fm-bar.focus i { background: var(--focus); }
.fm-bar.fali i { background: var(--fali); }
.fm-bar.stamina i { background: var(--stamina); }
.fm-bar.low i { animation: fm-pulse 1s infinite alternate; }
@keyframes fm-pulse { from { opacity: 1; } to { opacity: .45; } }

/* 戰鬥列 */
.fm-combat { display: none; background: #3a1512; border-bottom: 1px solid #6b2a22; padding: 6px 10px; }
.fm-combat.is-on { display: block; }
.fm-combat-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.fm-combat-row b { color: #ffb4a8; }
.fm-combat .fm-bar { flex: 1; height: 7px; }
.fm-combat .fm-bar i { background: var(--seal-soft); }

/* ---------- 文字流 ---------- */
.fm-log {
  flex: 1 1 auto; min-height: 90px; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 8px 10px 12px; font-size: 15px; line-height: 1.6; word-break: break-word; overflow-wrap: anywhere; overflow-x: hidden;
  background: var(--bg);
}
.fm-log { touch-action: pan-y; overscroll-behavior-y: contain; cursor: grab; }
.fm-log.is-dragging { cursor: grabbing; user-select: none; -webkit-user-select: none; }
.fm-log > div { white-space: pre-wrap; }
.fm-log a[id="mxplink"] {
  color: var(--link); text-decoration: none; border-bottom: 1px dotted currentColor;
  padding: 2px 1px; margin: -2px 0;
}
.fm-log .fm-echo { color: var(--ink-faint); font-size: 13px; }
.fm-log .fm-sys { color: var(--gold); font-size: 13px; }
.fm-newmsg {
  position: absolute; right: 12px; bottom: 8px; z-index: 5;
  background: var(--seal); color: #fff; border: 0; border-radius: 16px; padding: 6px 12px; font-size: 13px;
  display: none; box-shadow: 0 2px 8px #0008;
}
.fm-newmsg.is-on { display: block; }
.fm-logwrap { position: relative; flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }

/* ---------- 在場人物/物品 chips（可左右拖） ---------- */
.fm-peoplewrap { flex: none; position: relative; background: var(--bg-2); border-top: 1px solid var(--line); }
.fm-people {
  display: flex; align-items: center; gap: 6px; overflow-x: auto; padding: 6px 10px; scrollbar-width: none;
  touch-action: pan-x; overscroll-behavior-x: contain; cursor: grab;
}
.fm-people.is-dragging { cursor: grabbing; user-select: none; -webkit-user-select: none; }
.fm-more {
  display: none; position: absolute; right: 0; top: 0; bottom: 0; width: 30px; pointer-events: none;
  align-items: center; justify-content: flex-end; padding-right: 6px; font-style: normal; font-size: 20px; color: var(--gold);
  background: linear-gradient(90deg, #1d191400, var(--bg-2) 70%);
}
.fm-more.is-on { display: flex; }
.fm-chipsep { flex: none; width: 1px; height: 22px; background: var(--line); margin: 0 2px; }
.fm-chiptag { font-size: 10px; color: var(--ink-faint); border: 1px solid #4a4a4a; border-radius: 4px; padding: 0 3px; margin-right: 2px; }
.fm-people::-webkit-scrollbar { display: none; }
.fm-people:empty { display: none; }
.fm-chip {
  flex: none; height: 34px; padding: 0 12px; border-radius: 17px; border: 1px solid var(--line);
  background: var(--bg-3); color: var(--ink); font-size: 14px; white-space: nowrap; display: inline-flex; align-items: center; gap: 4px;
}
.fm-chip.npc { border-color: #7a5a2a; }
.fm-chip.player { border-color: #2f6a8a; }
.fm-chip.item { border-color: #4a4a4a; color: var(--ink-soft); }
.fm-chip small { font-size: 10px; color: var(--ink-faint); }
.fm-chip.is-target { background: #5a1d17; border-color: var(--seal-soft); }

/* ---------- 下方操作區（羅盤/面板） ---------- */
.fm-deck { flex: none; background: var(--bg-2); border-top: 1px solid var(--line); }
.fm-deck.is-collapsed .fm-pane { display: none; }
.fm-pane { display: none; }
.fm-pane.is-active { display: block; }

/* 行：羅盤 + 其他出口 + 快捷列 */
.fm-move { display: flex; gap: 8px; padding: 8px 10px 4px; }
.fm-dpad { flex: none; display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; width: min(62vw, 250px); }
.fm-cell {
  height: 50px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg-3);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 2px 3px;
  min-width: 0; overflow: hidden; text-align: center;
}
.fm-cell .dir { font-size: 11px; color: var(--gold); line-height: 1.1; }
.fm-cell .nm { font-size: 11.5px; line-height: 1.2; color: var(--ink); max-width: 100%; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.fm-cell.is-empty { background: transparent; border-style: dashed; border-color: #2a241c; }
.fm-cell.is-center { background: #3a2a1a; border-color: var(--gold); }
.fm-cell.is-center .nm { color: var(--gold); }
.fm-cell:not(.is-empty):active, .fm-btn:active, .fm-chip:active, .fm-xit:active { filter: brightness(1.5); transform: scale(.97); }
.fm-xits { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; max-height: 162px; overflow-y: auto; }
.fm-xit {
  min-height: 38px; border-radius: 8px; border: 1px solid #5a4a2a; background: var(--bg-3); text-align: left;
  padding: 3px 8px; font-size: 13px; line-height: 1.25; overflow: hidden;
}
.fm-xit .dir { color: var(--gold); margin-right: 4px; }
.fm-xits .fm-none { color: var(--ink-faint); font-size: 12px; padding: 6px 2px; }

.fm-quickwrap { position: relative; }
.fm-quick {
  display: flex; gap: 6px; overflow-x: auto; padding: 4px 10px 8px; scrollbar-width: none;
  touch-action: pan-x; overscroll-behavior-x: contain; cursor: grab;
}
.fm-app.pad-right .fm-move { flex-direction: row-reverse; }
.fm-quick .fm-btn.is-macro { border-color: #4a6a5a; }
.fm-quick .fm-quick-edit { color: var(--ink-faint); border-style: dashed; min-width: 0; }
.fm-qe-row { flex-wrap: nowrap; }
.fm-qe-row .grow { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fm-qe-btn { flex: none; width: 36px; height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg-2); color: var(--ink-soft); }
.fm-qe-btn:disabled { opacity: .3; }
.fm-qe-btn.danger { color: #ffb4a8; border-color: #7a2d25; }
.fm-qe-form { display: flex; flex-direction: column; gap: 6px; }
.fm-qe-form input { height: 42px; border-radius: 8px; border: 1px solid var(--line); background: #0c0a08; color: var(--ink); font-size: 16px; padding: 0 10px; }
.fm-quick.is-dragging { cursor: grabbing; user-select: none; -webkit-user-select: none; }
.fm-quick::-webkit-scrollbar { display: none; }
.fm-btn {
  flex: none; min-width: 52px; height: 40px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg-3); font-size: 14px; white-space: nowrap;
}
.fm-btn.danger { border-color: #7a2d25; color: #ffb4a8; }
.fm-btn.primary { border-color: var(--gold); color: var(--gold); }

/* 面板（包/技/人/任/聊/指令） */
.fm-panel { height: min(46dvh, 380px); overflow-y: auto; padding: 8px 12px 12px; -webkit-overflow-scrolling: touch; }
.fm-panel h4 { margin: 10px 0 6px; font-size: 13px; color: var(--gold); font-weight: 600; border-bottom: 1px solid var(--line); padding-bottom: 3px; }
.fm-panel h4:first-child { margin-top: 2px; }
.fm-pending { color: var(--ink-faint); font-size: 13px; padding: 4px 0; }
.fm-row {
  display: flex; align-items: center; gap: 6px; min-height: var(--tap); padding: 6px 8px; margin-bottom: 4px;
  border-radius: 8px; background: var(--bg-3); border: 1px solid var(--line); font-size: 14px; flex-wrap: wrap;
}
.fm-row.is-on { border-color: var(--jade); }
.fm-row .grow { flex: 1; min-width: 0; }
.fm-tag { font-size: 11px; border: 1px solid currentColor; border-radius: 4px; padding: 0 4px; color: var(--ink-soft); white-space: nowrap; }
.fm-tag.on { color: var(--jade); }
.fm-lv { font-size: 12px; color: var(--ink-soft); white-space: nowrap; }
.fm-lvbar { width: 100%; height: 3px; background: #0006; border-radius: 2px; overflow: hidden; }
.fm-lvbar i { display: block; height: 100%; background: var(--gold); }
.fm-grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; font-size: 13.5px; }
.fm-grid2 b { color: var(--ink); }
.fm-kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; font-size: 13.5px; }
.fm-kv .k { color: var(--ink-faint); }
.gold { color: var(--gold); }
.fm-text { white-space: pre-wrap; font-size: 14px; }
.fm-cmdgrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.fm-cmdgrid .fm-btn { min-width: 0; padding: 0 4px; width: 100%; }
.fm-cmdgrid .fm-btn small { display: block; font-size: 10px; color: var(--ink-faint); line-height: 1; }
.fm-cmdgrid .fm-btn.has-arg::after { content: "…"; color: var(--ink-faint); }

/* 聊天 */
.fm-chatlog { font-size: 14px; }
.fm-chatrow { padding: 3px 0; border-bottom: 1px dashed #2a241c; word-break: break-word; }
.fm-chatrow .sp { color: var(--river); }
.fm-chatrow.emote { color: var(--ink-soft); font-style: italic; }
.fm-emotes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 8px; }
.fm-emotes .fm-btn { min-width: 0; padding: 0 4px; font-size: 13px; }
.fm-seg { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.fm-seg .fm-btn { height: 34px; }
.fm-seg .fm-btn.is-on { border-color: var(--gold); color: var(--gold); }

/* ---------- 輸入列（預設隱藏） ---------- */
.fm-input { display: none; flex: none; background: var(--bg-2); border-top: 1px solid var(--line); padding: 6px 8px; }
.fm-input.is-on { display: block; }
.fm-sugg { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.fm-sugg::-webkit-scrollbar { display: none; }
.fm-sugg:empty { display: none; }
.fm-sugg .fm-chip { height: 30px; font-size: 13px; }
.fm-inrow { display: flex; gap: 6px; }
.fm-inrow input {
  flex: 1; min-width: 0; height: 42px; border-radius: 8px; border: 1px solid var(--line); background: #0c0a08;
  color: var(--ink); font-size: 16px; /* 16px：避免 iOS 聚焦時自動放大 */ padding: 0 10px;
}
.fm-inrow button { height: 42px; border-radius: 8px; border: 1px solid var(--gold); background: var(--bg-3); color: var(--gold); padding: 0 12px; }

/* ---------- 分頁列 ---------- */
.fm-nav {
  flex: none; display: grid; grid-template-columns: repeat(7, 1fr) 1.15fr; background: #0f0d0a; border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.fm-nav button {
  height: 50px; border: 0; background: transparent; color: var(--ink-faint); font-size: 17px; position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1;
}
.fm-nav button small { font-size: 9.5px; margin-top: 2px; }
.fm-nav button.is-active { color: var(--gold); }
.fm-nav button.is-active::before { content: ""; position: absolute; top: 0; left: 25%; right: 25%; height: 2px; background: var(--gold); }
/* 打字：主要按鈕，金色實心 */
.fm-nav button.fm-nav-kbd {
  margin: 5px 4px 5px 2px; height: 40px; border-radius: 10px; background: var(--gold); color: #1d1914;
  font-size: 17px; box-shadow: 0 0 0 1px #f0d890 inset, 0 2px 6px #0008;
}
.fm-nav button.fm-nav-kbd small { color: #1d1914; font-weight: 600; }
.fm-nav button.fm-nav-kbd:active { filter: brightness(1.15); transform: scale(.96); }
.fm-badge { position: absolute; top: 5px; right: 18%; min-width: 16px; height: 16px; border-radius: 8px; background: var(--seal); color: #fff; font-size: 10px; line-height: 16px; padding: 0 4px; display: none; }
.fm-badge.is-on { display: block; }

/* ---------- 動作選單（底部抽屜） ---------- */
/* 抽屜開著：整個畫面縮到抽屜上方（JS 設高度），下方羅盤/分頁列先收起 */
.fm-app.is-sheet .fm-deck, .fm-app.is-sheet .fm-nav, .fm-app.is-sheet .fm-input { display: none !important; }
.fm-sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; background: var(--bg-2); border-top: 1px solid var(--gold);
  border-radius: 14px 14px 0 0; max-height: 55dvh; box-shadow: 0 -6px 18px #0009; overflow-y: auto; padding: 8px 12px calc(12px + var(--safe-b));
  transform: translateY(105%); transition: transform .18s ease-out;
}
.fm-sheet.is-on { transform: translateY(0); }
.fm-sheet-grab { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 0 auto 8px; }
.fm-sheet-title { font-size: 16px; font-weight: 600; margin-bottom: 8px; color: var(--gold); }
.fm-sheet-sub { font-size: 12px; color: var(--ink-faint); margin: 10px 0 4px; }
.fm-sheet-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.fm-sheet-grid .fm-btn { width: 100%; min-width: 0; height: 46px; white-space: normal; line-height: 1.15; padding: 0 4px; }
.fm-sheet-grid .fm-btn.is-sent { border-color: var(--jade); color: var(--jade); }
.fm-sheet-grid .fm-btn.is-sent::after { content: " ✓"; }
.fm-sheet-cancel { width: 100%; margin-top: 12px; height: 44px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg-3); color: var(--ink-soft); }

/* ---------- 登入遮罩 ---------- */
.fm-login { position: fixed; inset: 0; z-index: 60; background: #000c; display: none; align-items: center; justify-content: center; padding: 16px; }
.fm-login.is-on { display: flex; }
.fm-login-card { width: 100%; max-width: 360px; background: var(--bg-2); border: 1px solid var(--gold); border-radius: 14px; padding: 18px 16px; }
.fm-login-card h2 { margin: 0 0 4px; color: var(--gold); font-size: 20px; }
.fm-login-card p { margin: 0 0 12px; font-size: 13px; color: var(--ink-soft); }
.fm-login-card input { display: block; width: 100%; height: 44px; margin-bottom: 8px; border-radius: 8px; border: 1px solid var(--line); background: #0c0a08; color: var(--ink); font-size: 16px; padding: 0 10px; }
.fm-login-card .fm-btn { width: 100%; margin-top: 4px; height: 46px; }
.fm-login-card a { color: var(--link); }
.fm-login-links { display: flex; justify-content: space-between; margin-top: 12px; font-size: 13px; }

/* 桌機上看也不會太寬 */
@media (min-width: 700px) {
  .fm-app { max-width: 520px; margin: 0 auto; left: 0; right: 0; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
}
@media (max-height: 560px) {
  .fm-cell { height: 40px; }
  .fm-panel { height: 40dvh; }
}
