:root {
    --b: #0b57d0;
    --b-hover: #0842a0;
    --bg: #fafafa;
    --card: #ffffff;
    --border: #dde1e6;
    --muted: #666;
    --radius: 12px;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html, body {
    /* prevent horizontal scroll on iOS Safari (100vw includes scrollbar gutter) */
    overflow-x: hidden;
}

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: #1a1a1a;
    margin: 0;
    padding: 0;
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;     /* prevent iOS auto-resize on rotate */
    -webkit-tap-highlight-color: transparent;
}

/* --- Main container — full-width to fit report tables --- */
.wrap {
    max-width: 100%;
    margin: 16px auto;
    background: var(--card);
    padding: 20px;
    border-radius: var(--radius);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 4px 12px rgba(0, 0, 0, 0.04);
}

h1 {
    margin: 0 0 16px;
    font-size: 1.5rem;
    font-weight: 700;
    max-width: 900px;
}

/* --- Form grid — constrained width so it doesn't stretch on wide screens --- */
.form-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 14px;
    margin-bottom: 18px;
    max-width: 920px;
}

.form-grid label {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.form-grid label span {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.form-grid input,
.form-grid select {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    font-family: inherit;
    background: #fff;
    transition: border-color 0.15s, box-shadow 0.15s;
    outline: none;
}

.form-grid input:focus,
.form-grid select:focus {
    border-color: var(--b);
    box-shadow: 0 0 0 3px rgba(11, 87, 208, 0.12);
}

/* --- Button --- */
button {
    background: var(--b);
    color: #fff;
    border: none;
    padding: 12px 28px;
    font-size: 1rem;
    font-weight: 600;
    font-family: inherit;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s;
}

button:hover:not(:disabled) {
    background: var(--b-hover);
}

button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* --- Report container — responsive scroll --- */
.report-container {
    margin-top: 20px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px;
    min-height: 60px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.report-container:empty {
    display: none;
}

/* --- Muted helper text --- */
.muted {
    color: var(--muted);
    font-style: italic;
}

/* --- Loading spinner --- */
.spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid var(--border);
    border-top-color: var(--b);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    vertical-align: middle;
    margin-right: 8px;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* --- Offline bar --- */
.offline-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    padding: 8px 12px;
    background: #f5f5f5;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 14px;
    font-size: .88rem;
    max-width: 920px;
}
.offline-bar.is-offline {
    background: #fff8e1;
    border-color: #f59e0b;
}
.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}
.status-dot.online  { background: #22c55e; }
.status-dot.offline { background: #f59e0b; }
.offline-bar button {
    padding: 5px 12px;
    font-size: .82rem;
    font-weight: 600;
}

/* --- Bulk download panel --- */
.bulk-panel {
    background: #f0f7ff;
    border: 1px solid #90caf9;
    border-radius: 10px;
    padding: 16px 18px;
    margin-bottom: 16px;
    max-width: 920px;
}
.bulk-progress-bar-bg {
    height: 8px;
    background: #dde1e6;
    border-radius: 4px;
    overflow: hidden;
}
.bulk-progress-bar {
    height: 100%;
    background: #0277bd;
    border-radius: 4px;
    width: 0%;
    transition: width 0.4s ease;
}
.bulk-log {
    max-height: 120px;
    overflow-y: auto;
    font-size: .78rem;
    font-family: monospace;
    background: #fff;
    border: 1px solid #dde1e6;
    border-radius: 6px;
    padding: 6px 8px;
    margin-top: 6px;
    color: #333;
    line-height: 1.6;
}

/* --- Tabs --- */
.tab-bar { display:flex; gap:0; margin-bottom:18px; border-bottom:2px solid #ddd; max-width:920px; }
.tab-btn { padding:10px 20px; border:none; background:none; font-size:.95rem; font-weight:600; color:var(--muted); cursor:pointer; border-bottom:2px solid transparent; margin-bottom:-2px; transition:all .15s; }
.tab-btn:hover { color:var(--b); }
.tab-btn.active { color:var(--b); border-bottom-color:var(--b); }

/* --- Map search box --- */
.map-search-wrap {
    display: flex;
    gap: 6px;
    margin-bottom: 8px;
}
.map-search-wrap input {
    flex: 1;
    padding: 7px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: .9rem;
    background: #fff;
    color: #1a1a1a;
}
.map-search-wrap input:focus {
    outline: none;
    border-color: var(--b);
    box-shadow: 0 0 0 2px rgba(11,87,208,.15);
}
.map-search-wrap button {
    padding: 7px 14px;
    font-size: .85rem;
    white-space: nowrap;
}

/* --- Map containers — full width, dynamic-viewport height (handles iOS address bar) --- */
#report-map, #scan-map {
    width: 100%;
    max-width: 100%;
    height: 52vh;            /* fallback for older browsers */
    height: 52dvh;           /* dynamic viewport: stable while iOS address bar shrinks/expands */
    min-height: 320px;
    border: 1px solid #ddd;
    border-radius: 8px;
    margin-bottom: 14px;
    z-index: 1;
    /* allow native pan/zoom gestures to coexist with Leaflet's touch handlers */
    touch-action: pan-x pan-y;
}

/* --- Scan results --- */
.scan-results-grid { display:flex; flex-direction:column; gap:12px; margin-top:16px; }
.spot-card { background:#fff; border:1px solid #eee; border-radius:10px; padding:14px 16px; }
.spot-card h3 { margin:0 0 6px; font-size:1.05rem; }
.spot-coords { font-size:.82rem; color:var(--muted); margin:0 0 10px; }
.windows { display:flex; gap:8px; flex-wrap:wrap; }
.window-chip { background:#f0f4ff; border:1px solid #e0e7ff; border-radius:6px; padding:5px 10px; font-size:.83rem; }
.window-time { font-weight:600; }
.window-score { color:var(--muted); margin-left:6px; }

/* --- Spot report inside details --- */
.spot-card details { margin-top:10px; }
.spot-card details > div { overflow-x:auto; -webkit-overflow-scrolling:touch; }

/* --- Best-target area table: row-number button → open report --- */
.bt-row-link {
    background: #0b57d0; color: #fff; border: none; border-radius: 4px;
    padding: 2px 7px; font-size: .82rem; font-weight: 700;
    cursor: pointer; white-space: nowrap;
}
.bt-row-link:hover { background: #1565c0; }

/* --- Best Target day cards --- */
.bt-day-card { background:#fff; border:1px solid #e0e7ef; border-radius:10px; margin-bottom:10px; overflow:hidden; }
.bt-day-header { background:#0b57d0; color:#fff; font-weight:700; font-size:.95rem; padding:7px 14px; letter-spacing:.03em; }
.bt-tech-block { padding:8px 14px 6px; border-bottom:1px solid #f0f4fa; }
.bt-tech-block:last-child { border-bottom:none; }
.bt-tech-label { font-size:.78rem; font-weight:700; text-transform:uppercase; color:#0b57d0; margin-bottom:6px; letter-spacing:.06em; }

/* Category group (collapsible) */
.bt-cat-group { border:1px solid #e8eef7; border-radius:7px; margin-bottom:6px; overflow:hidden; }
.bt-cat-group-header {
    display:flex; align-items:center; gap:8px; padding:6px 10px;
    background:#f4f7fd; cursor:pointer; user-select:none;
    font-size:.87rem;
}
.bt-cat-group-header:hover { background:#e8f0fe; }
.bt-cat-group-name { flex:1; font-weight:700; color:#333; }
.bt-cat-group-best { font-size:.84rem; color:#555; }
.bt-cat-chevron { font-size:.75rem; color:#888; transition:transform .2s; }
.bt-cat-group.bt-open .bt-cat-chevron { transform:rotate(180deg); }
.bt-cat-species-list { display:none; }
.bt-cat-group.bt-open .bt-cat-species-list { display:block; }

/* Species table */
.bt-species-table { width:100%; border-collapse:collapse; font-size:.84rem; }
.bt-species-table td { padding:4px 6px; vertical-align:middle; border-top:1px solid #f0f4fa; }
.bt-species-row:hover td { background:#f9fbff; }
.bt-sp-name { color:#333; white-space:nowrap; padding-left:12px !important; }
.bt-sp-score { width:48px; text-align:right; padding-right:6px !important; white-space:nowrap; }
.bt-sp-bar { width:80px; padding:0 8px; }
.bt-sp-win { color:#555; font-size:.78rem; }

/* Score mini-bar */
.bt-score-bar-bg { display:inline-block; width:72px; height:6px; background:#e0e7ef; border-radius:3px; vertical-align:middle; }
.bt-score-bar-fill { display:block; height:6px; border-radius:3px; transition:width .3s; }

/* Legacy table (kept for scan best-target) */
.bt-cat-table { width:100%; border-collapse:collapse; font-size:.88rem; }
.bt-cat-table td { padding:3px 6px 3px 0; vertical-align:top; }
.bt-cat-name { color:#333; white-space:nowrap; width:38%; }
.bt-cat-score { width:14%; text-align:right; padding-right:10px !important; }
.bt-cat-win { color:#555; font-size:.82rem; }

/* ===================================================================
   Best Target Per-Species area results (bsp-*)
   =================================================================== */

.bsp-group {
    border: 1px solid #e0e7ef;
    border-radius: 9px;
    margin-bottom: 7px;
    overflow: hidden;
}
.bsp-group-header {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 12px;
    background: #f4f7fd;
    cursor: pointer; user-select: none;
    font-size: .87rem;
}
.bsp-group-header:hover { background: #e8f0fe; }
.bsp-dot {
    width: 10px; height: 10px; border-radius: 50%;
    flex-shrink: 0;
}
.bsp-sp-label { font-weight: 700; color: #1a237e; white-space: nowrap; }
.bsp-tech-label { flex: 1; color: #555; font-size: .8rem; }
.bsp-best-score { font-size: .9rem; white-space: nowrap; }
.bsp-chevron { font-size: .75rem; color: #888; transition: transform .2s; }
.bsp-group.bsp-open .bsp-chevron { transform: rotate(180deg); }
.bsp-spots-list { display: none; }
.bsp-group.bsp-open .bsp-spots-list { display: block; }

.bsp-spot-row {
    display: flex; align-items: center; gap: 8px;
    padding: 5px 12px;
    border-top: 1px solid #f0f4fa;
    font-size: .83rem;
    flex-wrap: wrap;
}
.bsp-spot-row:hover { background: #f9fbff; }
.bsp-rank  { color: #888; width: 22px; flex-shrink: 0; font-size: .75rem; }
.bsp-window { white-space: nowrap; color: #333; font-weight: 600; min-width: 90px; }
.bsp-score { width: 36px; text-align: right; flex-shrink: 0; }
.bsp-conditions { flex: 1; color: #666; font-size: .78rem; }
.bsp-coords { flex-shrink: 0; }
.bsp-coords .bt-row-link {
    font-size: .78rem; padding: 2px 6px;
    background: #e8f0fe; color: #1a73e8;
    border: 1px solid #c5d8fb; border-radius: 4px;
    cursor: pointer;
}
.bsp-coords .bt-row-link:hover { background: #d2e3fc; }

/* ===================================================================
   Responsive breakpoints
   =================================================================== */

@media (min-width: 1600px) {
    .wrap { max-width: 98vw; }
}

@media (max-width: 1200px) {
    .wrap { padding: 14px; }
    .form-grid { max-width: 100%; }
    .offline-bar, .bulk-panel, .tab-bar { max-width: 100%; }
}

@media (max-width: 900px) {
    .wrap { padding: 10px; margin: 8px; }
    .form-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }
    #report-map, #scan-map { max-width: 100%; }
}

@media (max-width: 700px) {
    .wrap { margin: 6px; padding: 8px; }
    .form-grid { grid-template-columns: 1fr; }
    h1 { font-size: 1.3rem; }
    .offline-bar { gap: 6px; }
    .offline-bar button { padding: 7px 10px; }
}

/* iPhone / small phones — tighter layout + smaller map height */
@media (max-width: 480px) {
    .wrap { margin: 4px; padding: 6px; border-radius: 8px; }
    h1 { font-size: 1.15rem; margin-bottom: 10px; }

    /* Stack the offline bar vertically so buttons don't overflow */
    .offline-bar {
        flex-wrap: wrap;
        gap: 5px;
    }
    .offline-bar button {
        font-size: .8rem;
        padding: 6px 8px;
        flex: 1 1 auto;
        min-width: 0;
    }

    /* Tabs full-width and tappable */
    .tab-bar { gap: 4px; }
    .tab-btn { flex: 1; padding: 9px 6px; font-size: .9rem; }

    /* Smaller maps on phones; leave room for controls + results */
    #report-map, #scan-map {
        height: 46vh;
        height: 46dvh;
        min-height: 260px;
        margin-bottom: 10px;
    }

    /* Search box stack */
    .map-search-wrap { flex-wrap: wrap; gap: 6px; }
    .map-search-wrap input { flex: 1 1 100%; }

    /* Action buttons: full-width rows */
    button { font-size: .9rem; }

    /* Tables in reports: keep horizontal scroll smooth */
    table { font-size: .8rem; }
}
