:root {
    color-scheme: light;
    --ink: #172033;
    --muted: #64748b;
    --line: #cbd5e1;
    --soft: #f1f5f9;
    --panel: #ffffff;
    --primary: #0f766e;
    --primary-ink: #ffffff;
    --danger: #b91c1c;
    --shadow: 0 12px 30px rgba(15, 23, 42, .16);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--ink);
    background: #e2e8f0;
}

button, input, select, textarea {
    font: inherit;
}

input, select, textarea {
    width: 100%;
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    background: #fff;
    color: var(--ink);
}

textarea { resize: vertical; }

label {
    display: grid;
    gap: 6px;
    font-size: 14px;
    font-weight: 650;
}

fieldset {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
}

legend {
    padding: 0 8px;
    color: var(--muted);
    font-weight: 750;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: max(12px, env(safe-area-inset-top)) 14px 12px;
    background: #111827;
    color: #fff;
}

.topbar div {
    display: grid;
    gap: 2px;
}

.topbar span {
    color: #cbd5e1;
    font-size: 13px;
}

.icon-link {
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    color: #fff;
    text-decoration: none;
    border: 1px solid rgba(255,255,255,.28);
    border-radius: 8px;
}

.app-shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
    padding: 10px;
    max-width: 1500px;
    margin: 0 auto;
}

.toolbar, .capture-panel, .view, details, .auth-card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    gap: 8px;
    padding: 10px;
    align-items: end;
}

.button, .tab, .icon-button {
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 14px;
    background: #fff;
    color: var(--ink);
    text-decoration: none;
    font-weight: 750;
    cursor: pointer;
}

.button.primary {
    background: var(--primary);
    color: var(--primary-ink);
    border-color: var(--primary);
}

.button.danger {
    background: #fff5f5;
    color: var(--danger);
    border-color: #fecaca;
}

.button:disabled {
    opacity: .65;
    cursor: progress;
}

.capture-panel {
    display: grid;
    gap: 10px;
    padding: 12px;
}

.gps-status {
    display: grid;
    grid-template-columns: 18px 1fr;
    gap: 10px;
    align-items: center;
}

.gps-status small {
    color: var(--muted);
}

.dot {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    box-shadow: 0 0 0 4px rgba(148, 163, 184, .18);
}

.dot.green { background: #16a34a; }
.dot.orange { background: #f59e0b; }
.dot.red { background: #dc2626; }

.record-button {
    width: 100%;
    min-height: 76px;
    border: 0;
    border-radius: 8px;
    background: #0f766e;
    color: #fff;
    font-size: clamp(22px, 6vw, 34px);
    font-weight: 850;
    cursor: pointer;
}

.sample-list {
    display: grid;
    gap: 4px;
    color: var(--muted);
    font-size: 13px;
}

.tabs {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
}

.tab.active {
    background: #172033;
    color: #fff;
    border-color: #172033;
}

.view {
    display: none;
    min-height: 420px;
    overflow: hidden;
}

.view.active {
    display: block;
}

#map {
    width: 100%;
    height: min(68vh, 720px);
    min-height: 430px;
}

.map-actions {
    display: flex;
    gap: 8px;
    padding: 10px;
    border-top: 1px solid var(--line);
}

#planView {
    padding: 10px;
}

#planSvg {
    width: 100%;
    height: min(62vh, 700px);
    min-height: 420px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8fafc;
    touch-action: manipulation;
}

.metrics {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 10px;
}

.metric {
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
}

.metric strong {
    display: block;
    font-size: 18px;
}

#dataView {
    padding: 10px;
}

details {
    margin: 10px 0;
    padding: 10px;
}

summary {
    min-height: 44px;
    display: flex;
    align-items: center;
    font-weight: 850;
    cursor: pointer;
}

.filters {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
    gap: 8px;
}

.filter-pill, .checkbox {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px 10px;
    font-weight: 650;
    background: #fff;
}

.filter-pill input, .checkbox input {
    width: 20px;
    min-height: 20px;
}

.perimeter-list, .shape-list {
    display: grid;
    gap: 8px;
    padding-left: 20px;
}

.perimeter-list li, .shape-item {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px;
    background: #fff;
}

.row-actions {
    display: flex;
    gap: 6px;
    margin-top: 6px;
}

.row-actions button {
    min-height: 36px;
    padding: 6px 10px;
}

.export-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.editor {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 60;
    max-height: 88vh;
    overflow: auto;
    padding: 14px;
    background: #fff;
    border-top: 1px solid var(--line);
    box-shadow: var(--shadow);
    transform: translateY(105%);
    transition: transform .22s ease;
}

.editor.open {
    transform: translateY(0);
}

.editor form, .stack {
    display: grid;
    gap: 12px;
}

.stack.compact {
    max-width: 720px;
}

.editor-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.editor h2 {
    margin: 0;
    font-size: 20px;
}

.icon-button {
    width: 44px;
    padding: 0;
    font-size: 28px;
    line-height: 1;
}

.field-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.form-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
}

.coord-readout {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

.leaflet-div-icon {
    background: transparent;
    border: 0;
}

.point-marker {
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    color: #fff;
    border: 3px solid #fff;
    box-shadow: 0 2px 8px rgba(15, 23, 42, .35);
    font-weight: 850;
}

.auth-screen {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 16px;
    background: #dbeafe;
}

.auth-card {
    width: min(100%, 440px);
    padding: 22px;
    box-shadow: var(--shadow);
}

.auth-card h1 {
    margin: 0 0 8px;
}

.alert {
    padding: 10px 12px;
    border-radius: 8px;
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

.print-page {
    background: #fff;
}

.print-sheet {
    width: 297mm;
    max-width: 100%;
    margin: 0 auto;
}

.print-sheet svg {
    width: 100%;
    height: auto;
}

@media (min-width: 980px) {
    .app-shell {
        grid-template-columns: 370px minmax(0, 1fr);
        align-items: start;
    }

    .toolbar, .capture-panel, .tabs {
        grid-column: 1 / -1;
    }

    .view.active {
        grid-column: 2;
        grid-row: 4 / span 2;
    }

    #dataView.active {
        grid-column: 1;
        grid-row: 4 / span 2;
        max-height: 720px;
        overflow: auto;
    }

    .editor {
        inset: 92px 16px 16px auto;
        width: 390px;
        max-height: calc(100vh - 108px);
        border: 1px solid var(--line);
        border-radius: 8px;
        transform: translateX(115%);
    }

    .editor.open {
        transform: translateX(0);
    }
}

@media print {
    .topbar, .app-shell { display: none; }
    body { background: #fff; }
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

body {
    height: 100dvh;
}

.topbar {
    position: relative;
    height: 58px;
    min-height: 58px;
    padding: 8px 12px;
}

.topbar strong {
    font-size: 24px;
}

.icon-link {
    width: 42px;
    height: 42px;
}

.app-shell {
    height: calc(100dvh - 58px);
    max-width: none;
    margin: 0;
    padding: 8px;
    gap: 8px;
    grid-template-rows: 58px 152px 48px minmax(0, 1fr);
    overflow: hidden;
}

.toolbar {
    min-height: 0;
    padding: 8px;
    grid-template-columns: minmax(0, 1fr) 94px 94px;
}

.toolbar label {
    min-width: 0;
}

.toolbar label,
.toolbar select,
.toolbar .button {
    font-size: 14px;
}

.toolbar select,
.toolbar .button {
    min-height: 40px;
    padding: 7px 9px;
}

.capture-panel {
    min-height: 0;
    padding: 8px;
    gap: 8px;
    overflow: hidden;
}

.gps-status {
    grid-template-columns: 16px 1fr;
    gap: 8px;
    min-height: 42px;
}

.gps-status strong {
    font-size: 17px;
    line-height: 1.15;
}

.gps-status small {
    display: block;
    max-height: 34px;
    overflow: hidden;
    font-size: 12px;
    line-height: 1.25;
}

.survey-actions {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
}

.survey-button {
    min-width: 0;
    min-height: 72px;
    border: 0;
    border-radius: 8px;
    color: #fff;
    font-size: clamp(18px, 5vw, 28px);
    font-weight: 900;
    letter-spacing: 0;
    cursor: pointer;
}

.survey-button.start { background: #15803d; }
.survey-button.next { background: #0f766e; }
.survey-button.poi { background: #2563eb; }
.survey-button.stop { background: #b91c1c; }

.survey-button:disabled {
    opacity: .55;
    cursor: wait;
}

.sample-list {
    min-height: 18px;
    max-height: 18px;
    overflow: hidden;
    white-space: nowrap;
    font-size: 12px;
}

.tabs {
    min-height: 0;
}

.tab {
    min-height: 48px;
    padding: 6px 8px;
    font-size: 18px;
}

.view {
    min-height: 0;
    height: 100%;
}

.view.active {
    display: flex;
    flex-direction: column;
}

#mapView {
    position: relative;
}

#map {
    flex: 1 1 auto;
    height: 100%;
    min-height: 0;
}

.map-actions {
    position: absolute;
    z-index: 500;
    right: 8px;
    bottom: 8px;
    padding: 0;
    border: 0;
    background: transparent;
}

.map-actions .button {
    min-height: 42px;
    box-shadow: var(--shadow);
}

#planView {
    padding: 6px;
    overflow: hidden;
}

.metrics {
    flex: 0 0 auto;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    margin-bottom: 6px;
}

.metric {
    padding: 6px;
    min-width: 0;
}

.metric span {
    display: block;
    overflow: hidden;
    white-space: nowrap;
    font-size: 11px;
    color: var(--muted);
}

.metric strong {
    font-size: 15px;
    line-height: 1.15;
}

#planSvg {
    flex: 1 1 auto;
    height: auto;
    min-height: 0;
}

#dataView {
    padding: 8px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

.editor {
    left: 8px;
    right: 8px;
    bottom: 8px;
    max-height: calc(100dvh - 76px);
    border: 1px solid var(--line);
    border-radius: 8px;
}

@media (max-width: 420px) {
    .app-shell {
        grid-template-rows: 52px 148px 46px minmax(0, 1fr);
        padding: 6px;
        gap: 6px;
    }

    .toolbar {
        grid-template-columns: minmax(0, 1fr) 82px 82px;
        padding: 6px;
        gap: 6px;
    }

    .survey-actions {
        gap: 6px;
    }

    .survey-button {
        min-height: 68px;
        font-size: 19px;
    }

    .tab {
        min-height: 46px;
        font-size: 17px;
    }
}

@media (min-width: 980px) {
    .app-shell {
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: 58px 152px 48px minmax(0, 1fr);
    }

    .toolbar,
    .capture-panel,
    .tabs,
    .view.active,
    #dataView.active {
        grid-column: auto;
        grid-row: auto;
    }

    #dataView.active {
        max-height: none;
    }
}
