/*
 * 版面對照原 MainForm.Designer.cs。
 * 桌面版是絕對座標 + AutoScaleMode.Font；網頁版改用 flex，行為等價但會自己換行，
 * 手機開起來也不會爆版。
 */

:root {
    --team-mystic: #0066be;
    --team-valor: #c82828;
    --team-instinct: #aa7d00;
    --row-target: #e9f8e9;      /* 佔領人數 < 6 的淡綠底 */
    --row-selected: #cfe6ff;
    --border: #c8c8c8;
    --panel-bg: #f4f4f4;
    --highlight: #ff2d95;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
    font-family: "Microsoft JhengHei UI", "Microsoft JhengHei", "Segoe UI", system-ui, sans-serif;
    font-size: 13px;
    color: #1a1a1a;
    background: #fff;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

/* ---------------- panelTop ---------------- */

.panel-top {
    flex: 0 0 auto;
    background: var(--panel-bg);
    border-bottom: 1px solid var(--border);
    padding: 8px 12px;
}

.panel-top .row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 10px;
    margin-bottom: 6px;
}

.panel-top .row:last-child { margin-bottom: 0; }

.panel-top label { white-space: nowrap; }

.panel-top .chk {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    cursor: pointer;
    user-select: none;
}

.panel-top .source {
    color: #6b6b6b;
    font-size: 12px;
}

.panel-top .source a { color: #6b6b6b; }

/* 登出按鈕靠右，長得像連結而不是一顆會被誤按的按鈕 */
.logout-form {
    margin-left: auto;
    display: inline;
}

button.link-btn {
    background: none;
    border: none;
    padding: 0 2px;
    color: #6b6b6b;
    text-decoration: underline;
    font-size: 12px;
}

@media (hover: hover) {
    button.link-btn:hover:not(:disabled) {
        background: none;
        border: none;
        color: #1a1a1a;
    }
}

.team-mystic { color: var(--team-mystic); }
.team-valor { color: var(--team-valor); }
.team-instinct { color: var(--team-instinct); }

input[type="text"], input[type="number"], select, button {
    font: inherit;
    padding: 3px 6px;
    border: 1px solid #adadad;
    border-radius: 3px;
    background: #fff;
}

input.coord { width: 100px; }
input.num { width: 72px; }
input.num.narrow { width: 56px; }

button {
    background: linear-gradient(#fdfdfd, #f0f0f0);
    cursor: pointer;
    padding: 4px 12px;
}

/* hover 效果只給真的有指標的裝置。觸控螢幕上 :hover 會在點完之後「黏住」，
   看起來像那個按鈕還被選著。 */
@media (hover: hover) {
    button:hover:not(:disabled) { background: linear-gradient(#fff, #e8f2fb); border-color: #4a90d9; }
    table.grid tbody tr:hover td { filter: brightness(0.97); }
}

button:disabled { color: #8a8a8a; cursor: default; background: #f0f0f0; }
button.primary { font-weight: 600; }

/* 觸控裝置的目標尺寸。26px 高的按鈕在手機上按不準。
 *
 * 兩個條件是「或」：不要只靠 pointer: coarse。有些 Android WebView 與接了觸控筆的
 * 裝置會回報 fine，而窄螢幕幾乎一定是手機——寬度是比指標類型更可靠的判準。 */
@media (pointer: coarse), (max-width: 720px), (max-height: 500px) {
    button,
    input[type="text"],
    input[type="number"],
    select {
        min-height: 40px;
        padding-top: 6px;
        padding-bottom: 6px;
    }

    /* iOS Safari 在 font-size < 16px 的輸入框聚焦時會自動放大整頁 */
    input[type="text"], input[type="number"], select { font-size: 16px; }

    .panel-top .chk { min-height: 40px; gap: 8px; }
    .panel-top .chk input[type="checkbox"] {
        width: 20px;
        height: 20px;
        flex: 0 0 auto;
    }

    .map-header button { min-height: 36px; }
}

/* 只在窄螢幕出現的收合列 */
.panel-bar { display: none; }

/* ---------------- splitMain ---------------- */

.split {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.split-top {
    /* JS 會依「約 8 列」的高度覆寫這個值，對應 MainForm.ApplyDefaultSplit() */
    flex: 0 0 220px;
    min-height: 80px;
    overflow: hidden;
}

.splitter {
    flex: 0 0 6px;
    background: var(--panel-bg);
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    cursor: row-resize;
}

.splitter:hover { background: #dcdcdc; }

.split-bottom {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 80px;
}

/* ---------------- dgv ---------------- */

.grid-wrap {
    height: 100%;
    overflow: auto;
    outline: none;
    position: relative;
}

.grid-wrap:focus-visible { box-shadow: inset 0 0 0 2px #4a90d9; }

table.grid {
    width: 100%;
    border-collapse: collapse;
    white-space: nowrap;
}

table.grid th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-top: none;
    padding: 5px 8px;
    text-align: left;
    font-weight: 600;
}

table.grid td {
    border: 1px solid #e4e4e4;
    padding: 4px 8px;
}

table.grid tbody tr { cursor: pointer; }

/* 對應 dgv_CellFormatting：佔領人數未滿 6 的整列淡綠底 */
table.grid tbody tr.target td { background: var(--row-target); }

/* FullRowSelect */
table.grid tbody tr.selected td {
    background: var(--row-selected) !important;
    font-weight: 600;
}

/* 對應 dgv_RowPostPaint 手繪的列號 */
td.col-no, th.col-no {
    width: 60px;
    text-align: right;
    color: #666;
    background: var(--panel-bg);
}

.col-name { width: 300px; }
.col-team { width: 70px; }
.col-occ, .col-dist { width: 90px; text-align: right; }
.col-raid { width: 110px; }
.col-latlon { width: 100px; text-align: right; }

td.col-occ, td.col-dist, td.col-latlon { text-align: right; font-variant-numeric: tabular-nums; }

.grid-empty {
    padding: 24px 12px;
    color: #8a8a8a;
}

/* ---------------- panelMapHeader ---------------- */

.map-header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 5px 12px;
    background: var(--panel-bg);
    border-bottom: 1px solid var(--border);
}

#lblSelected {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.map {
    flex: 1 1 auto;
    min-height: 120px;
    background: #eee;
}

/* Leaflet 的道館標記 */
.gym-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid #fff;
    box-shadow: 0 0 3px rgba(0,0,0,.5);
}

.gym-dot.Mystic { background: var(--team-mystic); }
.gym-dot.Valor { background: var(--team-valor); }
.gym-dot.Instinct { background: var(--team-instinct); }
.gym-dot.Uncontested { background: #9a9a9a; }

/* ---------------- statusStrip ---------------- */

.status-strip {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 3px 12px;
    background: var(--panel-bg);
    border-top: 1px solid var(--border);
    font-size: 12px;
    min-height: 22px;
}

#lblStatus {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#lblCount { white-space: nowrap; color: #444; }

/* ---------------- toast（取代 MessageBox） ---------------- */

.toast {
    position: fixed;
    left: 50%;
    bottom: 48px;
    transform: translateX(-50%);
    max-width: min(560px, calc(100vw - 32px));
    padding: 10px 16px;
    border-radius: 6px;
    background: #b3261e;
    color: #fff;
    box-shadow: 0 4px 16px rgba(0,0,0,.28);
    z-index: 1000;
    white-space: pre-wrap;
}

.toast.info { background: #1f4e79; }

/* ---------------- Error.cshtml ---------------- */

.error-page {
    max-width: 640px;
    margin: 64px auto;
    padding: 0 16px;
}

/* ---------------- 窄螢幕 ----------------
 *
 * 基準機是 POCO F8 Ultra：3200×1440 實體，Chrome 的 CSS viewport 約 412×915。
 * 扣掉網址列與系統列，實際可用高度約 780～830px。
 *
 * 最大的問題是 .panel-top：那兩排控件在 412px 寬會 wrap 成七八行、吃掉約 340px，
 * 地圖只剩不到一條縫。所以窄螢幕預設把它整個收起來，只留一條摘要列。
 *
 * max-height 那個條件是給橫向手機的：915×412 的寬度超過 720，會被當成桌面，
 * 展開的控制列 127px 加上表格就把 412px 吃光，地圖只剩 120px。矮螢幕一樣要收合。
 * 斷點若有變動，js/app.js 的 NARROW_QUERY 要跟著改。
 */

@media (max-width: 720px), (max-height: 500px) {

    /* ---- 收合列 ---- */

    .panel-bar {
        display: flex;
        align-items: stretch;
        gap: 8px;
        padding: 6px 10px;
        padding-left: max(10px, env(safe-area-inset-left));
        padding-right: max(10px, env(safe-area-inset-right));
    }

    .panel-toggle {
        flex: 1 1 auto;
        display: flex;
        align-items: center;
        gap: 8px;
        min-width: 0;
        text-align: left;
        padding: 4px 10px;
    }

    .panel-toggle .caret {
        flex: 0 0 auto;
        font-size: 11px;
        color: #666;
        transition: transform .15s ease;
    }

    .panel-toggle[aria-expanded="true"] .caret { transform: rotate(90deg); }

    .panel-toggle .title { flex: 0 0 auto; font-weight: 600; }

    .panel-toggle .summary {
        flex: 1 1 auto;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: #555;
        font-size: 12px;
    }

    .panel-bar #btnFetchTop { flex: 0 0 auto; }

    /* ---- 可收合的本體 ---- */

    .panel-top { padding: 0 0 0 0; }
    .panel-body {
        padding: 2px 10px 8px;
        padding-left: max(10px, env(safe-area-inset-left));
        padding-right: max(10px, env(safe-area-inset-right));
        border-top: 1px solid var(--border);
    }
    .panel-body[hidden] { display: none; }

    /* 展開時最多佔 60% 螢幕高，超過就自己捲，不要把地圖擠沒 */
    .panel-body {
        max-height: 60vh;
        max-height: 60dvh;
        overflow-y: auto;
    }

    /* 收合列上已經有一顆抓取鈕了 */
    .panel-body #btnFetch { display: none; }

    .panel-top .row { gap: 8px 10px; }

    /* 中心點兩個座標框各佔一半，不要被擠成兩行 */
    input.coord { flex: 1 1 120px; width: auto; min-width: 0; }

    /* ---- 表格 ---- */

    .col-latlon, th.col-latlon { display: none; }
    .col-raid, th.col-raid { display: none; }

    table.grid { white-space: normal; }

    .col-name { width: auto; }
    td.col-name { white-space: normal; word-break: break-all; }

    td.col-no, th.col-no { width: 34px; padding-left: 4px; padding-right: 4px; }
    .col-team, th.col-team { width: 56px; }
    .col-occ, th.col-occ { width: 46px; }
    .col-dist, th.col-dist { width: 62px; }

    table.grid th { padding: 6px 6px; }
    table.grid td { padding: 8px 6px; }   /* 列高一點，手指好按 */

    /* ---- splitter ---- */

    .splitter {
        flex: 0 0 18px;
        position: relative;
        touch-action: none;               /* 拖曳時不要變成頁面捲動 */
    }

    .splitter::after {
        content: "";
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 48px;
        height: 4px;
        border-radius: 2px;
        background: #b0b0b0;
    }

    /* ---- 地圖與狀態列 ---- */

    .map-header {
        padding: 5px 10px;
        gap: 8px;
    }

    #lblSelected { font-size: 12px; }
    #btnOpenExternal { flex: 0 0 auto; }

    .status-strip {
        padding: 3px 10px;
        padding-bottom: max(3px, env(safe-area-inset-bottom));
        font-size: 11px;
    }

    .toast { bottom: calc(56px + env(safe-area-inset-bottom)); }
}

/* 很窄的螢幕（412px 以下也涵蓋）再收一點 */
@media (max-width: 430px) {
    .map-header #btnOpenExternal {
        /* 「用 Google 地圖開啟導航」在 412px 會把選取資訊擠掉 */
        font-size: 12px;
        padding-left: 8px;
        padding-right: 8px;
    }
}

/* ---------------- 登入頁 ---------------- */

.login-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 16px;
    background: var(--panel-bg);
}

.login-box {
    width: 100%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 20px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 6px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.login-box h1 {
    margin: 0;
    font-size: 16px;
}

.login-box .hint {
    margin: 0 0 4px;
    color: #6b6b6b;
    font-size: 12px;
}

.login-box label { font-size: 12px; }

.login-box input[type="password"] {
    font: inherit;
    /* iOS Safari 在 font-size < 16px 的輸入框聚焦時會自動放大整頁 */
    font-size: 16px;
    min-height: 40px;
    padding: 6px;
    border: 1px solid #adadad;
    border-radius: 3px;
    background: #fff;
}

.login-box button { min-height: 40px; margin-top: 4px; }

.login-error {
    margin: 0;
    color: var(--team-valor);
    font-size: 12px;
}
