/* ============================================================
   海上貿易 · 正式版 — Mobile-first 樣式
   地圖為全屏背景，操作面板為底部浮動視窗（bottom sheet）
   ============================================================ */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; padding: 0; }
body {
    font-family: -apple-system, 'Helvetica Neue', 'PingFang TC', 'Microsoft JhengHei', Arial, sans-serif;
    background: #081527;
    color: #dbe8f5;
    overflow: hidden; /* 地圖全屏，禁止頁面捲動 */
    -webkit-user-select: none; user-select: none;
}
input, textarea, button { font-family: inherit; }

/* ---- 全屏地圖 ---- */
#mapWrap { position: fixed; inset: 0; z-index: 0; }
#gameMap { position: absolute; inset: 0; width: 100%; height: 100%; }
#gameMap .maplibregl-canvas-container, #gameMap .maplibregl-canvas { background: transparent !important; }
#gameMap .maplibregl-marker { will-change: transform; }

/* ---- 頂部 HUD（浮在地圖上） ---- */
#hud {
    position: fixed; top: 0; left: 0; right: 0; z-index: 20;
    padding: max(8px, env(safe-area-inset-top)) 10px 4px;
    display: flex; flex-direction: column; gap: 6px;
    pointer-events: none;
}
#hud .hud-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
#hud .hud-spacer { flex: 1; }
#hud .hud-chip {
    background: rgba(8,21,39,0.85); border: 1px solid #2a6a9a; border-radius: 20px;
    padding: 4px 10px; font-size: 12px; color: #ffd700; white-space: nowrap;
}
#hud .hud-gold { color: #ffd700; font-weight: 700; }
/* 貨物獲利模式 */
.hud-btn.cargo-on { background: #28a745; border-color: #4ade80; color: #fff; }
.cargo-banner {
    background: rgba(40,167,69,0.92); border: 1px solid #4ade80; color: #fff; pointer-events: none;
    border-radius: 12px; padding: 8px 12px; font-size: 13px; display: flex; align-items: center; gap: 8px;
}
.cargo-banner button {
    margin-left: auto; background: rgba(0,0,0,0.25); border: none; color: #fff; border-radius: 8px;
    padding: 4px 10px; font-size: 12px; cursor: pointer; pointer-events: auto;
}
.cargo-warn-banner {
    background: rgba(208,52,44,0.92); border: 1px solid #ffb3b3; color: #fff;
    border-radius: 10px; padding: 7px 10px; font-size: 12px; line-height: 1.5; margin-top: 6px;
}
.profit-summary { background: rgba(13,31,51,0.7); border: 1px solid #1e3a58; border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; }
.ps-row { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: #eaf3fb; padding: 3px 0; }
.ps-row b { color: #ffd700; }
.ps-row b.up { color: #4ade80; }
.ps-row b.down { color: #f87171; }
#hud .hud-btn { cursor: pointer; pointer-events: auto; }
#hud button {
    background: rgba(8,21,39,0.85); border: 1px solid #2a6a9a; border-radius: 20px;
    color: #dbe8f5; font-size: 13px; padding: 4px 10px; cursor: pointer;
    pointer-events: auto; min-height: 34px;
}
#hud button:active { filter: brightness(1.3); }

/* ---- 底部導航（浮動藥丸） ---- */
#tabBar {
    position: fixed; bottom: max(10px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
    z-index: 30; display: flex; gap: 2px; max-width: calc(100% - 16px);
    padding: 6px; background: rgba(8,21,39,0.88); backdrop-filter: blur(8px);
    border: 1px solid #2a5a88; border-radius: 20px; box-shadow: 0 6px 20px rgba(0,0,0,0.45);
}
#tabBar button {
    border: none; background: transparent; color: #bcd6ee; font-size: 13px;
    padding: 9px 12px; border-radius: 14px; cursor: pointer; min-width: 52px;
    transition: background .15s, color .15s; white-space: nowrap;
}
#tabBar button.active { background: #1f6feb; color: #fff; }

/* ---- Bottom Sheet（面板） ---- */
.sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    background: linear-gradient(180deg, #122b47, #0d2036);
    border: 1px solid #2a5a88; border-bottom: none; border-radius: 18px 18px 0 0;
    box-shadow: 0 -6px 24px rgba(0,0,0,0.5);
    transform: translateY(110%); transition: transform .28s ease;
    max-height: 72vh; display: flex; flex-direction: column;
    padding-bottom: env(safe-area-inset-bottom);
}
.sheet.open { transform: translateY(0); }
.sheet-handle {
    width: 40px; height: 4px; border-radius: 2px; background: #3a5a7a;
    margin: 8px auto 2px; flex: none;
}
.panel-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 4px 16px 10px; flex: none;
}
.panel-head h3 { margin: 0; color: #ffd700; font-size: 16px; }
.panel-head .close {
    background: #24446a; border: none; color: #dbe8f5; border-radius: 10px;
    width: 32px; height: 32px; font-size: 14px; cursor: pointer;
}
.panel-body { overflow-y: auto; padding: 0 16px 84px; flex: 1; }

/* ---- 登入 ---- */
#loginSheet {
    max-height: 92vh; align-items: center; text-align: center;
    padding: 18px 20px 26px;
}
.login-hero { font-size: 46px; margin-top: 4px; }
#loginSheet h2 { margin: 4px 0 0; color: #ffd700; font-size: 24px; }
#loginSheet .sub { margin: 4px 0 18px; color: #8fb4d6; font-size: 13px; }
#loginSheet input {
    width: 100%; padding: 14px 14px; margin: 6px 0; border-radius: 12px;
    border: 1px solid #2a5a88; background: #0d1f33; color: #eaf3fb; font-size: 16px;
}
#loginSheet .row { display: flex; gap: 10px; margin: 12px 0; }
#loginSheet .row button {
    flex: 1; padding: 13px 0; border: none; border-radius: 12px;
    background: #1f6feb; color: #fff; font-size: 16px; cursor: pointer;
}
#loginSheet .row button:last-child { background: #28a745; }
#loginSheet .row button:active { filter: brightness(1.15); }
#loginSheet .msg { min-height: 18px; color: #ff9b9b; font-size: 13px; margin-top: 2px; }
#loginSheet .hint { color: #7f9fc0; font-size: 12px; line-height: 1.7; margin-top: 10px; }

/* ---- 通用按鈕 / 輸入 ---- */
button { -webkit-user-select: none; user-select: none; }
button:disabled { opacity: .45; cursor: not-allowed; }
.big { display: block; width: 100%; margin: 14px 0 4px; padding: 15px 0; border: none; border-radius: 14px; font-size: 16px; font-weight: 600; cursor: pointer; }
.big.primary { background: #1f6feb; color: #fff; }
.big.primary:active { filter: brightness(1.15); }
.big.primary:disabled { opacity: .6; }
.big.primary.insufficient { background: #6c757d; }
.sail-hint { color: #ffc107; font-size: 12px; text-align: center; margin-top: 10px; line-height: 1.6; }

/* ---- 區塊標題 ---- */
.sec-title {
    color: #ffd700; font-size: 14px; font-weight: 700; margin: 16px 0 8px;
    padding-left: 8px; border-left: 3px solid #ffd700;
}
.empty { color: #8fb4d6; font-size: 13px; padding: 12px 4px; text-align: center; }

/* ---- 數量選擇器 ---- */
.qtybar { display: flex; align-items: center; gap: 6px; margin: 4px 0 10px; flex-wrap: wrap; }
.qtybar > span { font-size: 13px; color: #bcd6ee; }
.qtybar .chip {
    border: 1px solid #2a6a9a; background: #122b47; color: #dbe8f5;
    border-radius: 16px; padding: 6px 14px; font-size: 13px; cursor: pointer;
}
.qtybar .chip.on { background: #ffd700; color: #1a2a3a; border-color: #ffd700; font-weight: 700; }

/* ---- 船艙 / 背包狀態 ---- */
.cargo-box {
    background: rgba(30,58,95,0.6); border: 1px solid #2a5a88; border-radius: 12px;
    padding: 10px 12px; margin-bottom: 10px;
}
.cargo-top { font-size: 13px; color: #eaf3fb; }
.cargo-top b { color: #ffd700; }
.cargo-top b.pos { color: #4ade80; }
.cargo-inv { font-size: 11px; color: #8fb4d6; margin-top: 4px; line-height: 1.9; }
.cargo-item {
    display: inline-block; margin: 2px 6px 2px 0; background: rgba(13,31,51,0.6);
    border: 1px solid #1e3a58; border-radius: 8px; padding: 3px 8px; font-size: 11px; color: #eaf3fb;
}
.cargo-empty { color: #7f9fc0; }
.cargo-warn { font-size: 11px; color: #ffb3b3; margin-top: 6px; line-height: 1.6; }
.profit { font-weight: 700; margin-left: 6px; font-size: 11px; }
.cargo-sell {
    border: none; border-radius: 8px; background: #28a745; color: #fff;
    padding: 3px 8px; margin-left: 6px; font-size: 11px; cursor: pointer; font-weight: 600;
}
.cargo-sell:active { filter: brightness(1.2); }
.mhold { font-size: 10px; color: #7f9fc0; margin-top: 2px; }
.mhold b { color: #ffd700; }
.mhold b.pos { color: #4ade80; }

/* ---- 市場列 ---- */
.mlist { display: flex; flex-direction: column; gap: 8px; }
.mrow {
    display: flex; align-items: center; gap: 10px;
    background: rgba(13,31,51,0.7); border: 1px solid #1e3a58; border-radius: 12px;
    padding: 10px 12px;
}
.mname { flex: 1; min-width: 0; }
.mcn { font-size: 14px; font-weight: 600; color: #eaf3fb; }
.mcn small.foreign { color: #4ade80; font-size: 10px; font-weight: 400; }
.men { font-size: 10px; color: #7f9fc0; }
.mprice { text-align: right; flex: none; }
.p-buy { position: relative; color: #ffb3b3; font-size: 12px; }
.p-buy.p-expensive { background: rgba(208,52,44,0.22); border: 1px solid #d0342c; border-radius: 8px; padding: 3px 7px; }
.p-buy.p-cheap { background: rgba(255,215,0,0.22); border: 1px solid #ffd700; border-radius: 8px; padding: 3px 7px; color: #ffe08a; }
.tag { display: block; font-size: 9px; font-weight: 700; margin-top: 2px; white-space: nowrap; }
.tag-red { color: #ff6b6b; }
.tag-gold { color: #ffd700; }
.tag-ok { color: #7fdc7f; }
.p-sell { color: #4ade80; font-size: 12px; }
.maction { display: flex; gap: 6px; flex: none; }
.maction button {
    border: none; border-radius: 10px; padding: 9px 12px; font-size: 13px; cursor: pointer; font-weight: 600;
}
.btn-buy { background: #1f6feb; color: #fff; }
.btn-sell { background: #28a745; color: #fff; }
.maction button:active { filter: brightness(1.2); }

/* ---- 港口卡 ---- */
#portCard { z-index: 26; }
.portinfo { text-align: center; margin-bottom: 10px; }
.portinfo .pname { font-size: 18px; font-weight: 700; color: #ffd700; }
.portinfo .pmeta { font-size: 12px; color: #8fb4d6; margin-top: 2px; }

/* ---- 最佳路線 ---- */
.arb { display: flex; flex-direction: column; gap: 8px; }
.arb-item {
    text-align: left; background: rgba(13,31,51,0.7); border: 1px solid #1e3a58; border-radius: 12px;
    padding: 10px 12px; color: #eaf3fb; cursor: pointer;
}
.arb-item:active { filter: brightness(1.2); }
.arb-main { font-size: 13px; font-weight: 600; }
.arb-sub { font-size: 11px; color: #8fb4d6; margin-top: 3px; }
.arb-sub .pos { color: #4ade80; }

/* ---- 狀態格 ---- */
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat {
    background: rgba(13,31,51,0.7); border: 1px solid #1e3a58; border-radius: 12px;
    padding: 10px 6px; text-align: center;
}
.stat .v { font-size: 16px; font-weight: 700; color: #ffd700; }
.stat .k { font-size: 10px; color: #8fb4d6; margin-top: 2px; }

/* ---- 船廠 ---- */
.ship-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.ship-card {
    background: rgba(13,31,51,0.7); border: 1px solid #1e3a58; border-radius: 12px; padding: 10px;
    display: flex; flex-direction: column; gap: 4px;
}
.ship-card.off { opacity: .6; }
.sc-name { font-size: 13px; font-weight: 600; color: #eaf3fb; }
.sc-name small { color: #ffd700; font-size: 10px; }
.sc-sub { font-size: 10px; color: #8fb4d6; }
.sc-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; }
.sc-foot .cost { color: #ffd700; font-size: 13px; font-weight: 700; }
.sc-foot button {
    border: none; border-radius: 9px; background: #1f6feb; color: #fff; padding: 8px 12px; cursor: pointer; font-size: 12px;
}
.fleet-row { background: rgba(30,58,95,0.6); }

/* ---- 船員 ---- */
.crew-grid { display: flex; flex-direction: column; gap: 8px; }
.crew-row {
    display: flex; align-items: center; justify-content: space-between;
    background: rgba(13,31,51,0.7); border: 1px solid #1e3a58; border-radius: 12px; padding: 10px 12px;
}
.cr-name { font-size: 13px; color: #eaf3fb; }
.cr-name small { color: #ffd700; }
.crew-row button { border: none; border-radius: 10px; background: #28a745; color: #fff; padding: 8px 12px; cursor: pointer; font-size: 12px; }
.repair-row { display: flex; align-items: center; justify-content: space-between; background: rgba(13,31,51,0.7); border: 1px solid #1e3a58; border-radius: 12px; padding: 10px 12px; font-size: 13px; }
.repair-row button { border: none; border-radius: 10px; background: #fd7e14; color: #fff; padding: 9px 16px; cursor: pointer; font-size: 13px; }

/* ---- 每日任務 ---- */
.quest { background: rgba(13,31,51,0.7); border: 1px solid #1e3a58; border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
.q-name { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; color: #eaf3fb; }
.q-name .q-target { color: #ffd700; font-size: 12px; }
.q-claim { margin-left: auto; border: none; border-radius: 9px; background: #28a745; color: #fff; padding: 7px 12px; cursor: pointer; font-size: 12px; }
.q-claim:disabled { background: #24446a; color: #8fb4d6; cursor: default; }
.q-desc { font-size: 11px; color: #8fb4d6; margin: 4px 0; }
.bar { height: 8px; background: #0a1524; border-radius: 5px; overflow: hidden; }
.bar-fill { height: 100%; background: linear-gradient(90deg, #ffd700, #ffb347); }

/* ---- 成就 ---- */
.ach-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.ach { background: rgba(13,31,51,0.6); border: 1px solid #1e3a58; border-radius: 12px; padding: 10px; text-align: center; opacity: .65; }
.ach.on { opacity: 1; border-color: #ffd700; background: rgba(60,48,20,0.5); }
.ach-icon { font-size: 24px; }
.ach-name { font-size: 12px; font-weight: 600; color: #eaf3fb; margin-top: 4px; }
.ach-desc { font-size: 10px; color: #8fb4d6; margin-top: 2px; }

/* ---- 排行 ---- */
.seg { display: flex; gap: 8px; margin-bottom: 10px; }
.seg button { flex: 1; border: 1px solid #2a6a9a; background: #122b47; color: #dbe8f5; border-radius: 12px; padding: 10px 0; cursor: pointer; font-size: 13px; }
.seg button.on { background: #1f6feb; color: #fff; border-color: #4d9fff; }
.lb-list, .mini-list { display: flex; flex-direction: column; gap: 6px; }
.lb-row, .mini-row {
    display: flex; align-items: center; gap: 10px; background: rgba(13,31,51,0.7);
    border: 1px solid #1e3a58; border-radius: 12px; padding: 9px 12px; font-size: 13px;
}
.lb-row.me { border-color: #ffd700; background: rgba(60,48,20,0.5); }
.rank { color: #ffd700; font-weight: 700; width: 22px; text-align: center; flex: none; }
.lb-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.right { margin-left: auto; color: #ffd700; flex: none; }
.hint { color: #7f9fc0; font-size: 11px; text-align: center; margin-top: 10px; line-height: 1.7; }

/* ---- Toast ---- */
#toasts { position: fixed; top: max(12px, env(safe-area-inset-top)); right: 10px; left: 10px; z-index: 999; pointer-events: none; display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
.toast {
    background: #122b47; border: 1px solid #ffd700; border-radius: 12px; color: #fff;
    padding: 10px 16px; font-size: 14px; box-shadow: 0 4px 16px rgba(0,0,0,0.5);
    max-width: 92%; opacity: 0; transform: translateY(-8px); transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: translateY(0); }

/* ---- 桌機優化 ---- */
@media (min-width: 900px) {
    .sheet { left: 50%; right: auto; width: 440px; transform: translate(-50%, 110%); border-radius: 18px; border-bottom: 1px solid #2a5a88; }
    .sheet.open { transform: translate(-50%, 0); }
    #loginSheet { left: 50%; right: auto; width: 400px; }
    .ship-grid, .ach-grid { grid-template-columns: 1fr 1fr; }
}

/* 安全區（劉海） */
#hud { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
.sheet { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
