:root {
    --bar: 48px;
    --bg: #0b0d10;
    --panel: #15191e;
    --panel-2: #1d232b;
    --line: #2a323c;
    --text: #e8edf2;
    --muted: #93a1b0;
    --accent: #3b82f6;
    --live: #ef4444;
    --gap: 3px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    overscroll-behavior: none;   /* no pull-to-refresh / rubber-band bounce — feels app-like */
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
    overflow: hidden;
}

/* ---------- top bar ---------- */
#bar {
    height: var(--bar);
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 0 10px;
    background: linear-gradient(#171c22, #11151a);
    border-bottom: 1px solid var(--line);
    overflow-x: auto;
    white-space: nowrap;
}
.bar-group { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }
.brand { font-weight: 700; gap: 7px; }
.brand .logo { font-size: 18px; }
.brand .title { letter-spacing: 1px; }
.brand .ver { font-size: 11px; color: var(--muted); text-decoration: none; font-weight: 400; }
.brand .ver:hover { color: var(--text); }
.brand .home-link {
    display: inline-flex; align-items: center; gap: 4px;
    height: 30px; padding: 0 10px;
    font-size: 13px; font-weight: 600; text-decoration: none; color: var(--text);
    border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px;
    white-space: nowrap;
}
.brand .home-link:hover { background: #2a323c; border-color: var(--accent); }
.region { gap: 6px; }
.rsel { height: 32px; border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
        border-radius: 8px; font-size: 13px; padding: 0 6px; max-width: 44vw; cursor: pointer; }

/* ---------- 母站 portal ---------- */
body.portal { overflow: auto; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.portal-wrap { text-align: center; padding: 24px; max-width: 760px; width: 100%; }
.portal-wrap h1 { font-size: 28px; margin: 0 0 6px; }
.portal-sub { color: var(--muted); margin: 0 0 22px; font-size: 14px; }
.portal-cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.pcard { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 22px 14px;
         background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
         text-decoration: none; color: var(--text); transition: background .12s, border-color .12s, transform .08s; }
.pcard:hover { background: var(--panel-2); border-color: var(--accent); transform: translateY(-2px); }
.pcard .pic { font-size: 34px; }
.pcard b { font-size: 17px; }
.pcard small { color: var(--muted); font-size: 12px; }
.pcard.wide { grid-column: 1 / -1; flex-direction: row; justify-content: center; gap: 12px; }
.pcard.wide .pic { font-size: 24px; }
.portal-search { display: flex; gap: 8px; margin-top: 14px; }
.portal-search input { flex: 1; font-size: 16px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); color: var(--text); }
.portal-search input:focus { outline: none; border-color: var(--accent); }
.portal-search button { padding: 0 18px; border: 0; border-radius: 12px; background: var(--accent); color: #fff; font-size: 15px; font-weight: 600; cursor: pointer; }
.portal-foot { margin-top: 20px; color: var(--muted); font-size: 12px; }
.portal-foot a { color: var(--muted); }
@media (max-width: 480px) { .portal-cards { grid-template-columns: 1fr; } }
.actions { margin-left: auto; }

.lbtn, .abtn, .fbtn {
    height: 32px;
    min-width: 32px;
    padding: 0 10px;
    border: 1px solid var(--line);
    background: var(--panel-2);
    color: var(--text);
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    transition: background .12s, border-color .12s;
}
.lbtn:hover, .abtn:hover, .fbtn:hover { background: #2a323c; }
.lbtn.active, .fbtn.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
.lbtn[hidden] { display: none; }
.fbtn[hidden] { display: none; }
.filters { gap: 4px; }
.avail-note { align-self: center; font-size: 12px; color: var(--muted); white-space: nowrap; padding: 0 2px; }
.lbtn.pip { min-width: auto; }
.abtn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.abtn.primary:hover { filter: brightness(1.1); }
#btn-mute.solo-active { background: var(--live); border-color: var(--live); color: #fff; }

/* ---------- grid ---------- */
.grid {
    display: grid;
    gap: var(--gap);
    width: 100vw;
    height: calc(100vh - var(--bar));
    padding: var(--gap);
    background: #000;
}
.empty-msg {
    position: fixed;
    top: var(--bar); left: 0; right: 0; bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: 15px;
    pointer-events: none;
}
.empty-msg[hidden] { display: none; }

.grid[hidden] { display: none; }              /* let JS hide the wall (overrides display:grid) */
.grid[data-layout="1"]  { grid-template-columns: 1fr; grid-template-rows: 1fr; }
.grid[data-layout="2"]  { grid-template-columns: repeat(2, 1fr); grid-template-rows: 1fr; }
.grid[data-layout="3"]  { grid-template-columns: repeat(3, 1fr); grid-template-rows: 1fr; }
.grid[data-layout="4"]  { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(2, 1fr); }
.grid[data-layout="9"]  { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); }
.grid[data-layout="12"] { grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(3, 1fr); }
.grid[data-layout="16"] { grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr); }

/* picture-in-picture: one big tile + a strip of small ones */
.grid[data-layout="pip"] { grid-template-columns: 1fr 280px; grid-template-rows: repeat(5, 1fr); }
.grid[data-layout="pip"] .tile:nth-child(1) { grid-column: 1; grid-row: 1 / span 5; }
.grid[data-layout="pip"] .tile:nth-child(n+2) { grid-column: 2; }

.tile {
    position: relative;
    background: #000;
    overflow: hidden;
    border: 2px solid transparent;
    border-radius: 4px;
    min-height: 0;
    min-width: 0;
}
.tile.hidden { display: none; }
.tile.focused { border-color: var(--accent); }

/* CSS-only fullscreen fallback (iOS Safari has no Fullscreen API for div tiles). */
.tile.pseudo-fs {
    position: fixed;
    inset: 0;
    width: 100vw; height: 100vh;
    z-index: 60;
    border: 0;
    border-radius: 0;
}
body.has-pseudo-fs { overflow: hidden; }

.tile .mount, .tile iframe {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
}
.tile .cctv-media {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #000;
    border: 0;
    display: block;
}

/* placeholder when no live video is resolved yet */
.tile .placeholder {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    color: var(--muted);
    font-size: 13px;
    text-align: center;
    padding: 8px;
}
.tile .placeholder .spin {
    width: 22px; height: 22px;
    border: 2px solid var(--line);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* per-tile overlay */
.tile .ov {
    position: absolute;
    left: 0; right: 0; top: 0;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 6px;
    background: linear-gradient(rgba(0,0,0,.72), rgba(0,0,0,0));
    opacity: 0;
    transition: opacity .15s;
    pointer-events: none;
    z-index: 3;
}
.tile:hover .ov, .tile.focused .ov { opacity: 1; pointer-events: auto; }
.tile .ov .name { font-size: 12px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .ov .spacer { flex: 1; }
.tile .ov button, .tile .ov a {
    border: 0;
    background: rgba(255,255,255,.12);
    color: #fff;
    border-radius: 6px;
    height: 24px;
    min-width: 24px;
    padding: 0 6px;
    font-size: 13px;
    cursor: pointer;
    line-height: 1;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.tile .ov button:hover, .tile .ov a:hover { background: rgba(255,255,255,.28); }
.tile .ov button.on { background: var(--live); }

/* click-catcher: sits above the iframe so single/double clicks drive the tile
   (YouTube's own controls are disabled). Below the overlay buttons (z-index 3). */
.tile .catch { position: absolute; inset: 0; z-index: 2; cursor: pointer; }

/* 360° tiles: the catch layer captures drag and pans the view via the IFrame spherical
   API (controls:0 embeds don't pan natively). Keep the control row + hint always shown. */
/* non-embeddable channel (e.g. 台視, error 150): a clickable poster that opens YouTube */
.tile.noembed .catch { pointer-events: none; }
.tile .mount a.noembed {
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center;
    background: #000 center / cover no-repeat;
    text-decoration: none;
}
.tile .mount a.noembed::before { content: ''; position: absolute; inset: 0; background: rgba(0, 0, 0, .5); }
.noembed-box {
    position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px;
    color: #fff; font-size: 14px; font-weight: 700;
    background: rgba(0, 0, 0, .6); padding: 10px 16px; border-radius: 10px;
}
.noembed-box small { font-weight: 400; font-size: 11px; color: #cbd5e1; }

.tile.pano .catch { cursor: grab; }
.tile.pano .catch:active { cursor: grabbing; }
.tile.pano .ov { opacity: 1; pointer-events: auto; }
/* single-tile / fullscreen → YouTube native controls are shown; let clicks reach them
   (and on a 360 tile, native drag-to-pan takes over). Hide the 360 drag hint here. */
.tile.controls .catch { pointer-events: none; }
.tile.controls .pano-badge { display: none; }
.pano-badge {
    display: none;
    position: absolute; left: 6px; bottom: 6px; z-index: 3;
    align-items: center; gap: 4px;
    background: rgba(0, 0, 0, .58); color: #fff;
    font-size: 11px; padding: 2px 8px; border-radius: 6px;
    pointer-events: none; white-space: nowrap;
}
.tile.pano .pano-badge { display: flex; }

/* 市區快照標示 + 點擊看即時 */
.tile .cctv-badge {
    position: absolute; left: 6px; bottom: 6px; z-index: 3;
    background: rgba(0, 0, 0, .58); color: #fff;
    font-size: 11px; padding: 2px 8px; border-radius: 6px;
    text-decoration: none; white-space: nowrap;
}
.tile .cctv-badge[hidden] { display: none; }
.tile .cctv-badge:hover { background: var(--accent); }
.tile .cctv-badge.live { background: rgba(22, 163, 74, .82); }   /* 即時影片 (台北 HLS) */
.tile .cctv-badge.live:hover { background: #16a34a; }

/* error / cannot-embed overlay */
.tile .errbox {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    background: rgba(10, 12, 15, .92);
    color: var(--text);
    text-align: center;
    padding: 12px;
}
.tile .errbox[hidden] { display: none; }
.tile .errbox .err-msg { font-size: 13px; }
.tile .errbox .err-actions { display: flex; gap: 8px; }
.tile .errbox a, .tile .errbox button {
    border: 1px solid var(--line);
    background: var(--panel-2);
    color: var(--text);
    border-radius: 8px;
    padding: 7px 12px;
    font-size: 13px;
    cursor: pointer;
    text-decoration: none;
}
.tile .errbox a { background: var(--accent); border-color: var(--accent); color: #fff; }

.badge-live {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 10px;
    font-weight: 700;
    color: #fff;
    background: var(--live);
    padding: 1px 6px;
    border-radius: 4px;
}
.badge-live.off { background: #4b5563; }
.badge-live .dot { width: 6px; height: 6px; border-radius: 50%; background: #fff; }

/* left/right channel-flip arrows */
.tile .flip {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 30px; height: 52px;
    background: rgba(0,0,0,.45);
    color: #fff;
    border: 0;
    font-size: 20px;
    cursor: pointer;
    opacity: 0;
    transition: opacity .15s;
    z-index: 3;
    pointer-events: none;            /* don't steal clicks from the catcher when hidden */
}
.tile:hover .flip { opacity: 1; pointer-events: auto; }
.tile .flip.prev { left: 0; border-radius: 0 8px 8px 0; }
.tile .flip.next { right: 0; border-radius: 8px 0 0 8px; }

/* ---- single-cam viewer (channel-surf the list; mobile = immersive TV) ---- */
body.viewing-single .tile .flip { display: flex !important; align-items: center; justify-content: center; }   /* re-enable prev/next in single browse view (beats browse-mode hide) */
@media (hover: hover) { body.viewing-single .tile:hover .flip { opacity: 1; pointer-events: auto; } }
/* touch device OR narrow screen: single-cam view = immersive full-bleed TV with a tap panel */
@media (pointer: coarse), (max-width: 820px) {
    body.viewing-single #grid { position: fixed; inset: 0; height: 100dvh; z-index: 40; padding: 0; gap: 0; }   /* video fills the screen */
    body.viewing-single .tile { border: 0; border-radius: 0; }
    body.viewing-single #bar { display: none; }                                  /* replaced by the tap panel */
    /* single view turns on native YT controls (.controls → catch off); re-enable the catch so
       taps/swipes reach the viewer instead of being swallowed by the iframe (was: locked). */
    body.viewing-single .tile.controls .catch { pointer-events: auto; }
    body.viewing-single .tile .ov, body.viewing-single .tile .flip, body.viewing-single .tile .cctv-badge { opacity: 0 !important; pointer-events: none !important; }
    body.viewing-single #viewer-panel { display: block; }                        /* exists here; fades in via .viewer-ui */
    body.viewing-single::after { content: '點一下顯示控制 · 上下滑動切換'; position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 60; background: rgba(0, 0, 0, .62); color: #fff; font-size: 12px; padding: 5px 13px; border-radius: 14px; pointer-events: none; animation: viewerHint 3.2s forwards; }
    body.viewing-single.viewer-ui::after { display: none; }
}
@keyframes viewerHint { 0%, 68% { opacity: .92; } 100% { opacity: 0; } }

/* translucent on-tap control panel (mobile single-cam viewer) */
#viewer-panel { display: none; position: fixed; inset: 0; height: 100dvh; z-index: 55; opacity: 0; pointer-events: none; transition: opacity .18s; color: #fff; }
body.viewing-single.viewer-ui #viewer-panel { opacity: 1; pointer-events: auto; }
#viewer-panel button, #viewer-panel .vp-live { background: rgba(0, 0, 0, .5); border: 0; color: #fff; border-radius: 9px; font-size: 15px; padding: 9px 13px; cursor: pointer; text-decoration: none; -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); }
#viewer-panel .vp-top { position: absolute; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 10px; padding: 14px 14px 30px; background: linear-gradient(rgba(0, 0, 0, .6), transparent); }
#viewer-panel .vp-name { flex: 1; font-size: 15px; font-weight: 700; text-shadow: 0 1px 4px #000; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#viewer-panel .vp-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 50px; height: 78px; font-size: 30px; display: flex; align-items: center; justify-content: center; }
#viewer-panel .vp-prev { left: 10px; }
#viewer-panel .vp-next { right: 10px; }
#viewer-panel .vp-bottom { position: absolute; left: 0; right: 0; bottom: 0; display: flex; justify-content: center; gap: 12px; padding: 18px 14px 26px; background: linear-gradient(transparent, rgba(0, 0, 0, .6)); }
#viewer-panel .vp-guide { position: absolute; left: 0; right: 0; bottom: 0; max-height: 60%; overflow-y: auto; background: rgba(10, 12, 16, .94); border-top: 1px solid rgba(255, 255, 255, .12); }
#viewer-panel .vp-guide[hidden] { display: none; }
#viewer-panel .vg-item { padding: 13px 16px; border-bottom: 1px solid rgba(255, 255, 255, .07); font-size: 15px; }
#viewer-panel .vg-item.cur { color: var(--accent); font-weight: 700; }

/* global search overlay (all cams, every category) */
.search-overlay { position: fixed; inset: 0; z-index: 80; background: rgba(10, 12, 16, .98); display: flex; flex-direction: column; }
.search-overlay[hidden] { display: none; }
.so-bar { display: flex; gap: 10px; padding: 14px; border-bottom: 1px solid var(--line); }
#so-input { flex: 1; font-size: 17px; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--text); }
.so-results { flex: 1; overflow-y: auto; padding: 6px 0 40px; }
.so-hint { padding: 30px 20px; text-align: center; color: var(--muted); }
.so-item { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; background: none; border: 0; border-bottom: 1px solid rgba(255, 255, 255, .06); color: var(--text); padding: 13px 16px; font-size: 15px; cursor: pointer; }
.so-item:hover { background: var(--panel-2); }
.so-tag { flex: none; font-size: 12px; padding: 3px 8px; border-radius: 5px; color: #fff; min-width: 40px; text-align: center; }
.so-news, .so-finance { background: #b91c1c; }
.so-scenic { background: #16a34a; }
.so-temple { background: #b45309; }
.so-cctv { background: #2563eb; }
.so-name { flex: 1; }

/* notch / home-indicator safe areas (standalone PWA + notched phones; 0 on normal browsers) */
@supports (top: env(safe-area-inset-top)) {
    #bar { padding-top: env(safe-area-inset-top); height: calc(var(--bar) + env(safe-area-inset-top)); }
    .so-bar { padding-top: calc(14px + env(safe-area-inset-top)); }
    #browse-bar { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
    #viewer-panel .vp-top { padding-top: calc(14px + env(safe-area-inset-top)); }
    #viewer-panel .vp-bottom { padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
    #viewer-panel .vp-prev { left: calc(10px + env(safe-area-inset-left)); }
    #viewer-panel .vp-next { right: calc(10px + env(safe-area-inset-right)); }
}
/* portrait single-cam: nudge to rotate for a bigger picture (hint fades, hidden once controls are up) */
@media (pointer: coarse) and (orientation: portrait) {
    body.viewing-single::before {
        content: '↻ 手機橫放，畫面更大';
        position: fixed; top: calc(12px + env(safe-area-inset-top, 0px)); left: 50%; transform: translateX(-50%);
        z-index: 60; background: rgba(0, 0, 0, .6); color: #fff; font-size: 13px; padding: 6px 14px; border-radius: 14px;
        pointer-events: none; animation: rotHint 4.5s forwards;
    }
    body.viewing-single.viewer-ui::before { display: none; }
}
@keyframes rotHint { 0%, 72% { opacity: .95; } 100% { opacity: 0; } }

/* ---------- drawer ---------- */
.scrim {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 40;
}
.drawer {
    position: fixed;
    top: 0; right: 0;
    width: 330px;
    max-width: 86vw;
    height: 100%;
    background: var(--panel);
    border-left: 1px solid var(--line);
    transform: translateX(100%);
    transition: transform .22s ease;
    z-index: 50;
    display: flex;
    flex-direction: column;
    padding: 12px;
    gap: 10px;
}
.drawer.open { transform: translateX(0); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; }
.drawer-hint { margin: 0; font-size: 12px; color: var(--muted); }
.search {
    width: 100%;
    height: 36px;
    padding: 0 10px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: var(--panel-2);
    color: var(--text);
    font-size: 14px;
}
.show-all { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); cursor: pointer; user-select: none; }
.show-all input { accent-color: var(--accent); }
.channel-list { overflow-y: auto; flex: 1; margin: 0 -4px; }
.ch-item.dim { opacity: .45; }
.cat-head {
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    padding: 12px 8px 4px;
    position: sticky;
    top: 0;
    background: var(--panel);
}
.region-head {
    font-size: 12px;
    color: var(--text);
    font-weight: 600;
    padding: 8px 8px 6px 12px;
    opacity: .85;
    cursor: pointer;
    user-select: none;
}
.region-head:hover { color: var(--accent); }
.ch-item {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 9px 8px;
    border-radius: 8px;
    cursor: pointer;
}
.ch-item:hover { background: var(--panel-2); }
.ch-item.active { outline: 1px solid var(--accent); }
.ch-item .ch-dot { width: 9px; height: 9px; border-radius: 50%; background: #4b5563; flex: 0 0 auto; }
.ch-item .ch-dot.live { background: var(--live); box-shadow: 0 0 0 3px rgba(239,68,68,.22); }
.ch-item .ch-name { font-size: 14px; }
.ch-item .ch-cat { margin-left: auto; font-size: 11px; color: var(--muted); }

/* ---------- browse panel (路況/風景/廟宇 drill-down) ---------- */
.is-hidden { display: none !important; }
/* in a browse category the per-tile channel-flip + drawer buttons don't apply */
body.browse-mode .tile .flip,
body.browse-mode .tile .b-switch { display: none !important; }

.browse {
    position: absolute;
    top: var(--bar); left: 0; right: 0; bottom: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
}
.browse-crumb {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 16px;
    border-bottom: 1px solid var(--line);
    flex: 0 0 auto;
    flex-wrap: wrap;
}
.browse-crumb .crumb-back {
    height: 32px; padding: 0 12px;
    background: var(--panel-2); color: var(--text);
    border: 1px solid var(--line); border-radius: 8px;
    font-size: 14px; cursor: pointer;
}
.browse-crumb .crumb-back:hover { border-color: var(--accent); }
.browse-crumb .crumb-cur { font-size: 15px; font-weight: 700; }
.browse-crumb .crumb-sub { font-size: 12px; color: var(--muted); }
.browse-crumb .road-filter {
    display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
.browse-crumb .road-filter button {
    border: 0; border-left: 1px solid var(--line); background: var(--panel-2); color: var(--text);
    font-size: 13px; padding: 6px 11px; cursor: pointer; white-space: nowrap;
}
.browse-crumb .road-filter button:first-child { border-left: 0; }
.browse-crumb .road-filter button.on { background: var(--accent); color: #fff; font-weight: 700; }
.browse-crumb .browse-search {
    margin-left: auto;
    height: 32px; min-width: 160px; flex: 0 1 280px;
    padding: 0 10px;
    background: var(--panel-2); color: var(--text);
    border: 1px solid var(--line); border-radius: 8px; font-size: 14px;
}
.browse-list {
    flex: 1; overflow-y: auto;
    padding: 12px 14px 88px;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 8px;
    align-content: start;
}
.menu-item {
    display: flex; align-items: center; gap: 10px;
    height: 54px; padding: 0 14px;
    background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
    color: var(--text); font-size: 15px; text-align: left; cursor: pointer;
}
.menu-item:hover { background: var(--panel-2); border-color: var(--accent); }
.menu-item .mi-name { flex: 1; }
.menu-item .mi-count { font-size: 12px; color: var(--muted); }
.menu-item .mi-arrow { color: var(--muted); font-size: 18px; }
/* mode chooser cards (地圖模式 / 分類瀏覽) */
.menu-item.mode-card { height: 92px; padding: 0 18px; }
.menu-item.mode-card .mi-pic { font-size: 30px; margin-right: 8px; }
.menu-item.mode-card .mi-name { display: flex; flex-direction: column; gap: 5px; font-size: 17px; font-weight: 700; }
.menu-item.mode-card .mi-name small { font-weight: 400; font-size: 12px; color: var(--muted); }
.cam-item {
    display: flex; align-items: center; gap: 8px;
    height: 50px; padding: 0 10px 0 4px;
    background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
}
.cam-item.picked { border-color: var(--accent); background: rgba(59, 130, 246, .14); }
.cam-item .cam-check { display: flex; align-items: center; padding: 0 8px; cursor: pointer; }
.cam-item .cam-check input { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; }
.cam-item .cam-name { flex: 1; font-size: 14px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cam-item .cam-dist { color: var(--accent); font-size: 12px; margin-left: 6px; }
.cam-item .cam-go {
    cursor: pointer; color: #fff; background: var(--accent);
    border-radius: 6px; height: 30px; min-width: 34px;
    display: inline-flex; align-items: center; justify-content: center; font-size: 13px;
}
.cam-item .cam-go:hover { filter: brightness(1.12); }

/* tw.live-style thumbnail cards (browse cam list). Card height is the single knob the
   responsive blocks below scale; the thumb is flex:1 so it grows/shrinks with it. */
.cam-item.cam-card { display: flex; flex-direction: column; height: 185px; padding: 0; gap: 0; overflow: hidden; cursor: pointer; }
.cam-card .cam-thumb { position: relative; flex: 1 1 auto; min-height: 0; background: #161a22; display: flex; align-items: center; justify-content: center; }
.cam-card .cam-thumb.empty::after { content: '📷'; font-size: 26px; opacity: .3; }
.cam-card .cam-thumb-img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam-card .cam-name { flex: 0 0 auto; padding: 7px 9px; font-size: 13px; line-height: 1.35; white-space: normal; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; cursor: pointer; }
.cam-card .cam-check { position: absolute; top: 4px; right: 4px; padding: 3px; background: rgba(0, 0, 0, .45); border-radius: 6px; z-index: 2; }
.cam-card .cam-go { position: absolute; left: 6px; bottom: 6px; width: 30px; min-width: 0; height: 30px; border-radius: 50%; background: rgba(0, 0, 0, .55); opacity: 0; transition: opacity .15s; z-index: 2; }
.cam-card:hover .cam-go { opacity: 1; }
.cam-card:hover .cam-thumb-img { filter: brightness(1.06); }
.cam-card.picked { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }
/* 即時 / 快照 corner tag */
.cam-tag { position: absolute; top: 5px; left: 5px; z-index: 2; font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 5px; color: #fff; line-height: 1.5; }
.cam-tag-live { background: rgba(22, 163, 74, .92); }
.cam-tag-snap { background: rgba(202, 138, 4, .92); }

/* Scale cards up with the viewport so big/hi-res screens aren't a sea of tiny tiles. */
@media (min-width: 1500px) { .browse-list { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); } .cam-item.cam-card { height: 225px; } .cam-card .cam-name { font-size: 14px; } }
@media (min-width: 2200px) { .browse-list { grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); } .cam-item.cam-card { height: 280px; } .cam-card .cam-name { font-size: 15px; } }
@media (min-width: 3400px) { .browse-list { grid-template-columns: repeat(auto-fill, minmax(480px, 1fr)); } .cam-item.cam-card { height: 345px; } .cam-card .cam-name { font-size: 17px; } }
/* Phones: 2-up in portrait, denser auto-fill in landscape. */
@media (max-width: 680px) and (orientation: portrait) { .browse-list { grid-template-columns: repeat(2, 1fr); } .cam-item.cam-card { height: 150px; } }
@media (max-width: 960px) and (orientation: landscape) { .browse-list { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); } .cam-item.cam-card { height: 150px; } }

.browse-loading, .browse-empty { grid-column: 1 / -1; padding: 24px; text-align: center; color: var(--muted); }
.browse-bar {
    position: fixed; left: 0; right: 0; bottom: 0;
    display: flex; align-items: center; gap: 10px;
    padding: 10px 16px;
    background: rgba(18, 22, 28, .97);
    border-top: 1px solid var(--line);
    z-index: 45;
}
.browse-bar[hidden] { display: none; }
.browse-bar .bb-spacer { flex: 1; }
.browse-count { font-size: 14px; color: var(--text); font-weight: 600; }

@media (max-width: 680px) {
    /* browse-list columns handled by the orientation-aware rules above */
    .grid[data-layout="2"], .grid[data-layout="3"] {
        grid-template-columns: 1fr; grid-template-rows: none; grid-auto-rows: 40vh;
    }
}

/* ---------- map (全台 CCTV 地圖) ---------- */
.map-view {
    position: absolute;
    top: var(--bar); left: 0; right: 0; bottom: 0;
    z-index: 1;                 /* own stacking context so Leaflet's panes stay under the toolbar/bar */
    background: var(--bg);
}
.map-view .leaflet-container { width: 100%; height: 100%; background: #0b0d10; font: inherit; }
.map-loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.map-pop { font-size: 13px; line-height: 1.5; }
.map-pop b { display: block; margin-bottom: 6px; max-width: 220px; }
.map-pop button {
    border: 0; border-radius: 6px; padding: 6px 10px; margin: 2px 4px 0 0;
    font-size: 13px; cursor: pointer; color: #fff; background: var(--accent);
}
.map-pop button[data-mapact="pick"] { background: var(--panel-2); border: 1px solid var(--line); color: var(--text); }
/* the floating 同時觀看 bar must sit above Leaflet controls */
.browse-bar { z-index: 1000; }

/* road filter on the cctv map (全部 / 國道 / 快速道路 / 國道+快速) */
.map-filter {
    position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
    z-index: 1000; display: flex;
    background: rgba(18, 22, 28, .92); border: 1px solid var(--line);
    border-radius: 8px; overflow: hidden; box-shadow: 0 2px 10px rgba(0, 0, 0, .4);
    max-width: 96vw;
}
.map-filter button {
    border: 0; border-left: 1px solid var(--line); background: transparent; color: var(--text);
    font-size: 13px; padding: 7px 12px; cursor: pointer; white-space: nowrap;
}
.map-filter button:first-child { border-left: 0; }
.map-filter button.on { background: var(--accent); color: #fff; font-weight: 700; }
@media (max-width: 680px) { .map-filter button { padding: 7px 9px; font-size: 12px; } }

/* ---------- toast ---------- */
.toast {
    position: fixed;
    left: 50%; bottom: 24px;
    transform: translateX(-50%) translateY(8px);
    background: rgba(20, 24, 30, .96);
    color: var(--text);
    border: 1px solid var(--line);
    padding: 10px 16px;
    border-radius: 10px;
    font-size: 14px;
    z-index: 70;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s, transform .2s;
    box-shadow: 0 6px 24px rgba(0, 0, 0, .45);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- touch devices (no hover) ---------- */
/* Without a mouse there is no :hover, so controls can't appear on hover. Instead a
   single tap focuses a tile and the base `.tile.focused .ov` rule reveals its control
   row; here we also surface the channel-flip arrows on that focused tile. Keeping the
   controls tied to focus (not always-on) leaves the rest of the wall clean. */
@media (hover: none) {
    .tile.focused .flip { opacity: 1; pointer-events: auto; }
}

/* ---------- mobile ---------- */
@media (max-width: 680px) {
    .brand .title { display: none; }
    #btn-mute { font-size: 0; }            /* show icon only */
    #btn-mute::before { content: "🔇"; font-size: 16px; }
    .grid { overflow-y: auto; }
    .grid[data-layout="9"],
    .grid[data-layout="12"],
    .grid[data-layout="16"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: none;
        grid-auto-rows: 28vh;
    }
    .grid[data-layout="4"] { grid-template-rows: none; grid-auto-rows: 36vh; }
    .grid[data-layout="pip"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: none;
        grid-auto-rows: 28vh;
    }
    .grid[data-layout="pip"] .tile:nth-child(1) {
        grid-column: 1 / -1;
        grid-row: auto;
        grid-auto-rows: 40vh;
        min-height: 40vh;
    }
    .grid[data-layout="pip"] .tile:nth-child(n+2) { grid-column: auto; }
}
