/* Colours. The app follows the light or dark setting of the operating system; the dark values are
   in the media query below. Style everything through these variables. */

:root {
    color-scheme: light dark;
    --rail-width: 80px;

    --rail: #1b2430;
    --rail-text: #aab6c3;
    --rail-active: #ffffff;
    --rail-hover: rgba(255, 255, 255, .07);
    --rail-pill: rgba(90, 170, 245, .24);

    --canvas: #eef1f4;
    --surface: #ffffff;
    --surface-muted: #f6f8fa;
    --float: rgba(255, 255, 255, .97);
    --shadow: 0 1px 2px rgba(16, 24, 40, .12), 0 4px 14px rgba(16, 24, 40, .16);
    --border: #dfe4ea;
    --control: #c9d1da;
    --field: #ffffff;
    --text: #1d2733;
    --muted: #5f6d7c;
    --hover: rgba(100, 116, 139, .12);
    --accent: #1e88e5;
    --accent-soft: #e3effc;
    --on-accent: #ffffff;
    --danger: #c62828;
    --danger-border: #e6b3b3;
    --danger-soft: #fdecea;
    --on-danger: #ffffff;
    --warning-soft: #fff4e0;
    --warning-text: #8a5300;
    --overdue-text: #b45f00;
    --keeps: #2e7d32;
    --preview: #8e24aa;

    /* Status and boundary colours are the same in both themes; the map uses the same values in js/map.js. */
    --status-free: #43a047;
    --status-checked-out: #1e88e5;
    --status-overdue: #fb8c00;
    --status-not-closed: #9e9e9e;
    --status-candidate: #fdd835;
    --status-not-worked: #8d6e63;
    --boundary: #9c27b0;
}

@media (prefers-color-scheme: dark) {
    :root {
        --rail: #0a0f14;
        --rail-text: #8795a5;
        --rail-hover: rgba(255, 255, 255, .06);
        --rail-pill: rgba(78, 163, 240, .26);

        --canvas: #10161d;
        --surface: #18202a;
        --surface-muted: #141b23;
        --float: rgba(26, 34, 44, .96);
        --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .45);
        --border: #28333f;
        --control: #3a4654;
        --field: #10161d;
        --text: #e2e8ee;
        --muted: #93a2b2;
        --hover: rgba(148, 163, 184, .12);
        --accent: #4ea3f0;
        --accent-soft: rgba(78, 163, 240, .16);
        --on-accent: #06121f;
        --danger: #ef8a80;
        --danger-border: rgba(239, 138, 128, .4);
        --danger-soft: rgba(239, 83, 80, .14);
        --on-danger: #1f0806;
        --warning-soft: rgba(251, 140, 0, .14);
        --warning-text: #ffb866;
        --overdue-text: #ffb05c;
        --keeps: #7ccf80;
    }
}

[hidden] {
    display: none !important;
}

html, body {
    height: 100%;
    margin: 0;
}

body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    color: var(--text);
    background: var(--canvas);
}

a {
    color: var(--accent);
}

.icon {
    flex: none;
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Frame: navigation rail on the left, the page on the right */

.app {
    display: flex;
    height: 100vh;
}

.rail {
    display: flex;
    flex: none;
    flex-direction: column;
    gap: 2px;
    box-sizing: border-box;
    width: var(--rail-width);
    padding: 12px 6px;
    overflow-y: auto;
    background: var(--rail);
    color: var(--rail-text);
    /* Native parts such as the language list stay dark on the dark rail. */
    color-scheme: dark;
}

.rail-logo {
    display: grid;
    flex: none;
    place-items: center;
    width: 40px;
    height: 40px;
    margin: 0 auto 14px;
    border-radius: 11px;
    background: var(--accent);
    color: var(--on-accent);
}

.rail-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 4px 2px 8px;
    border-radius: 10px;
    color: inherit;
    font-size: 11px;
    line-height: 1.2;
    text-align: center;
    text-decoration: none;
    hyphens: auto;
    overflow-wrap: anywhere;
    cursor: pointer;
}

.rail-pill {
    display: grid;
    place-items: center;
    width: 52px;
    height: 30px;
    border-radius: 15px;
    transition: background-color .15s;
}

.rail-item:hover .rail-pill {
    background: var(--rail-hover);
}

.rail-item.active {
    color: var(--rail-active);
    font-weight: 600;
}

.rail-item.active .rail-pill {
    background: var(--rail-pill);
}

.rail-item:focus-visible, .rail-item.language:focus-within {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.rail-spacer {
    flex: 1;
}

.rail-item.language {
    position: relative;
}

/* Invisible and stretched over the whole item, so a click anywhere opens the native list. */
.rail-item.language select {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    opacity: 0;
    cursor: pointer;
}

/* The sign-out button looks like the other rail items. */
.rail form {
    display: contents;
}

button.rail-item {
    width: 100%;
    border: 0;
    background: none;
    font: inherit;
    font-size: 11px;
}

.content {
    position: relative;
    display: flex;
    flex: 1;
    flex-direction: column;
    min-width: 0;
    overflow-y: auto;
}

/* Map page: map with legend and tool dock, sidebar on the right */

.workspace {
    display: flex;
    flex: 1;
    min-height: 0;
}

.map-area {
    position: relative;
    display: flex;
    flex: 1;
    min-width: 0;
    container-type: inline-size;
}

.map {
    flex: 1;
    min-width: 0;
}

.legend {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 1;
    box-sizing: border-box;
    max-width: calc(100% - 24px);
    padding: 7px 11px;
    border-radius: 9px;
    background: var(--float);
    box-shadow: var(--shadow);
    font-size: 12px;
    pointer-events: none;
}

.legend-status {
    margin-bottom: 5px;
    padding-bottom: 5px;
    border-bottom: 1px solid var(--border);
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.legend-items {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 12px;
}

.legend-items span {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    white-space: nowrap;
}

.swatch {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 3px;
    background: var(--status-free);
}

.swatch.checked-out { background: var(--status-checked-out); }
.swatch.overdue { background: var(--status-overdue); }
.swatch.not-closed { background: var(--status-not-closed); }
.swatch.candidate { background: var(--status-candidate); }

/* A dotted line, like the city boundary on the map. */
.swatch.boundary {
    height: 0;
    border-top: 3px dotted var(--boundary);
    border-radius: 0;
    background: none;
}

/* Sits above Google's logo and next to its zoom buttons. */
.dock {
    position: absolute;
    bottom: 32px;
    left: 50%;
    z-index: 2;
    display: flex;
    padding: 5px;
    transform: translateX(-50%);
    border-radius: 12px;
    background: var(--float);
    box-shadow: var(--shadow);
}

.dock-group {
    display: flex;
    gap: 2px;
}

.dock-group + .dock-group {
    margin-left: 5px;
    padding-left: 5px;
    border-left: 1px solid var(--border);
}

.tool-button {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    /* Wide enough for single words like "Straßenkarte"; longer labels wrap between words. */
    width: 80px;
    padding: 7px 4px 6px;
    border: 0;
    border-radius: 8px;
    background: none;
    color: var(--text);
    font: inherit;
    font-size: 11px;
    line-height: 1.2;
    text-align: center;
    cursor: pointer;
}

.tool-button:hover {
    background: var(--hover);
}

.tool-button.active {
    background: var(--accent-soft);
    color: var(--accent);
}

/* Narrow maps show only the icons; the title attribute still names each tool. */
@container (max-width: 640px) {
    .tool-label {
        display: none;
    }

    .tool-button {
        width: 44px;
        padding: 10px 4px;
    }
}

.setup {
    flex: 1;
    max-width: 720px;
    padding: 32px;
}

.setup pre {
    overflow-x: auto;
    padding: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
}

.panel {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    width: 340px;
    min-height: 0;
    background: var(--surface);
    border-left: 1px solid var(--border);
}

.panel-header {
    display: flex;
    flex: none;
    align-items: center;
    gap: 8px;
    box-sizing: border-box;
    min-height: 52px;
    padding: 6px 8px 6px 16px;
    border-bottom: 1px solid var(--border);
}

.panel-header h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.spacer {
    flex: 1;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
}

.panel-section {
    padding: 14px 16px;
}

.panel-section + .panel-section {
    border-top: 1px solid var(--border);
}

.panel-section h3, .card h2 {
    margin: 0 0 10px;
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
}

.form h4 {
    margin: 4px 0 10px;
    font-size: 13px;
    font-weight: 600;
}

.help ul {
    margin: 0;
    padding-left: 18px;
    color: var(--muted);
    line-height: 1.5;
}

.territory-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.territory-list li {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 3px 9px;
    border: 1px solid var(--border);
    border-radius: 6px;
}

.territory-list .swatch {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

/* Town headings in the territory list */
.group-title {
    margin: 14px 0 6px;
    font-size: 13px;
    font-weight: 600;
}

h3 + .group-title {
    margin-top: 0;
}

.badge {
    color: var(--muted);
    font-size: 11px;
}

/* Assignment status */

.status-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 10px;
    background: var(--status-free);
    color: #ffffff;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}

.status-badge + .status-badge {
    margin-left: 4px;
}

.status-badge.checked-out { background: var(--status-checked-out); }
.status-badge.overdue { background: var(--status-overdue); }
.status-badge.not-worked { background: var(--status-not-worked); }

.badges {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 0 0 10px;
}

dl.facts {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 4px 12px;
    margin: 0 0 12px;
}

dl.facts dt {
    color: var(--muted);
}

dl.facts dd {
    margin: 0;
}

.overdue {
    color: var(--overdue-text);
    font-weight: 700;
}

dd.notes {
    white-space: pre-wrap;
}

.return-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    margin-bottom: 10px;
}

.panel .return-row label {
    flex: 1;
    margin-bottom: 0;
}

ul.history {
    margin: 0;
    padding: 0;
    list-style: none;
}

ul.history li {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0 8px;
    padding: 6px 4px;
    border-bottom: 1px solid var(--border);
}

ul.history li:last-child {
    border-bottom: 0;
}

ul.history li.selected {
    background: var(--accent-soft);
}

ul.history .when {
    color: var(--muted);
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}

ul.history .icon-button {
    grid-row: 1 / span 2;
    grid-column: 2;
    align-self: center;
}

/* Forms */

.panel label, .form label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 10px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
}

.panel :is(input, textarea, select), .form :is(input, textarea, select) {
    padding: 6px 8px;
    border: 1px solid var(--control);
    border-radius: 7px;
    background: var(--field);
    color: var(--text);
    font: inherit;
    font-size: 14px;
    font-weight: 400;
}

.panel :is(input, textarea, select):focus, .form :is(input, textarea, select):focus {
    border-color: var(--accent);
    outline: 2px solid var(--accent);
    outline-offset: -1px;
}

.panel :is(input, textarea, select):disabled {
    opacity: .6;
}

.field-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.field-row > label {
    flex: 1;
    min-width: 130px;
}

.hint {
    margin: 0 0 10px;
    color: var(--muted);
}

.warning {
    display: flex;
    gap: 8px;
    margin: 0 0 10px;
    padding: 8px 10px;
    border-radius: 8px;
    background: var(--warning-soft);
    color: var(--warning-text);
}

.warning .icon {
    margin-top: 1px;
}

.error {
    color: var(--danger);
}

kbd {
    padding: 0 4px;
    background: var(--surface);
    border: 1px solid var(--control);
    border-bottom-width: 2px;
    border-radius: 4px;
    font: 12px ui-monospace, monospace;
}

/* Buttons */

.actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
}

button.primary, button.secondary, button.danger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 34px;
    padding: 0 14px;
    border: 1px solid transparent;
    border-radius: 8px;
    font: inherit;
    font-weight: 500;
    cursor: pointer;
}

button .icon {
    width: 16px;
    height: 16px;
}

button.primary {
    background: var(--accent);
    color: var(--on-accent);
}

button.primary:hover:not(:disabled) {
    filter: brightness(1.08);
}

button.secondary {
    background: var(--surface);
    border-color: var(--control);
    color: var(--text);
}

button.secondary:hover:not(:disabled) {
    background-image: linear-gradient(var(--hover), var(--hover));
}

button.danger {
    background: transparent;
    border-color: var(--danger-border);
    color: var(--danger);
}

button.danger:hover:not(:disabled) {
    background: var(--danger-soft);
}

button:disabled {
    opacity: .5;
    cursor: default;
}

button:focus-visible, a:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

button.link {
    padding: 0;
    border: 0;
    background: none;
    color: var(--accent);
    font: inherit;
    cursor: pointer;
}

.icon-button {
    display: inline-grid;
    flex: none;
    place-items: center;
    width: 34px;
    height: 34px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: none;
    color: var(--muted);
    cursor: pointer;
}

.icon-button:hover {
    background: var(--hover);
    color: var(--text);
}

.icon-button .icon, .tool-button .icon {
    width: 20px;
    height: 20px;
}

/* Messages. Inverted colours, like a notification from the operating system. */

.snackbar {
    position: fixed;
    bottom: 24px;
    left: calc(var(--rail-width) + (100vw - var(--rail-width)) / 2);
    z-index: 50;
    display: flex;
    align-items: center;
    gap: 10px;
    box-sizing: border-box;
    width: max-content;
    max-width: min(560px, calc(100vw - var(--rail-width) - 32px));
    padding: 6px 6px 6px 14px;
    transform: translateX(-50%);
    border-radius: 10px;
    background: var(--text);
    color: var(--surface);
    box-shadow: var(--shadow);
    animation: snackbar-in .2s ease-out;
}

/* On the map it sits above the dock. */
.snackbar.over-map {
    position: absolute;
    left: 50%;
    bottom: 116px;
    z-index: 3;
    max-width: calc(100% - 32px);
}

.snackbar.error {
    background: var(--danger);
    color: var(--on-danger);
}

.snackbar-text {
    flex: 1;
}

.snackbar .icon-button {
    color: inherit;
}

.snackbar .icon-button:hover {
    background: rgba(127, 127, 127, .25);
    color: inherit;
}

@keyframes snackbar-in {
    from {
        opacity: 0;
        translate: 0 8px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .snackbar {
        animation: none;
    }

    .rail-pill {
        transition: none;
    }
}

/* Split/merge decision; floats over the sidebar so the highlighted map stays visible. */

.decision {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 100;
    box-sizing: border-box;
    width: min(380px, calc(100vw - 32px));
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    padding: 16px;
    background: var(--surface);
    border-top: 4px solid var(--preview);
    border-radius: 10px;
    box-shadow: 0 10px 30px rgba(15, 23, 42, .35);
}

.decision h2 {
    margin: 0 0 6px;
    font-size: 17px;
}

.decision fieldset {
    margin: 0 0 12px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
}

.decision legend {
    padding: 0 4px;
    font-weight: 700;
}

.part-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin: 6px 0;
}

.part-row input.number {
    width: 110px;
    padding: 4px 6px;
    border: 1px solid var(--control);
    border-radius: 6px;
    background: var(--field);
    color: var(--text);
    font: inherit;
}

.choice {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
}

.keeps {
    color: var(--keeps);
    font-weight: 600;
}

/* Map overlays created by js/map.js */

.tm-label {
    position: absolute;
    transform: translate(-50%, -50%);
    color: #fff;
    font: 700 14px system-ui, sans-serif;
    text-shadow: 0 0 3px #000, 0 0 2px #000;
    white-space: nowrap;
    pointer-events: none;
}

.tm-label.candidate {
    color: #fff3e0;
    font-size: 12px;
    font-weight: 600;
}

.tm-label.open {
    color: #eee;
    font-style: italic;
}

.tm-label.preview {
    padding: 2px 7px;
    background: #8e24aa;
    border-radius: 4px;
    text-shadow: none;
}

.tm-snap {
    position: absolute;
    width: 14px;
    height: 14px;
    margin: -9px 0 0 -9px;
    background: rgba(255, 179, 0, .35);
    border: 2px solid #ffb300;
    border-radius: 50%;
    pointer-events: none;
}

.tm-snap.segment {
    border-radius: 3px;
}

/* A click here ends the line */
.tm-snap.ends {
    background: #ffb300;
    box-shadow: 0 0 0 4px rgba(255, 179, 0, .35);
}

/* Pages without the map: header bar, then the page content */

.page-header {
    position: sticky;
    top: 0;
    z-index: 5;
    padding: 14px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.page-header h1 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
}

.page-header .hint {
    margin: 2px 0 0;
    font-size: 13px;
}

/* FocusOnNavigate moves the focus to the heading for screen readers; no visible ring needed. */
h1:focus {
    outline: none;
}

.page-body {
    box-sizing: border-box;
    width: 100%;
    max-width: 760px;
    padding: 20px 24px 32px;
}

.page-body.wide {
    max-width: 1140px;
}

.master-data {
    display: grid;
    grid-template-columns: minmax(260px, 340px) 1fr;
    gap: 20px;
    align-items: start;
}

@media (max-width: 860px) {
    .master-data {
        grid-template-columns: 1fr;
    }
}

.card {
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
}

.search {
    display: flex;
    align-items: center;
    gap: 8px;
    box-sizing: border-box;
    height: 36px;
    margin-bottom: 12px;
    padding: 0 10px;
    border: 1px solid var(--control);
    border-radius: 8px;
    background: var(--field);
    color: var(--muted);
}

.search:focus-within {
    border-color: var(--accent);
    outline: 2px solid var(--accent);
    outline-offset: -1px;
}

.search .icon {
    width: 16px;
    height: 16px;
}

.search input {
    flex: 1;
    min-width: 0;
    border: 0;
    outline: none;
    background: none;
    color: var(--text);
    font: inherit;
}

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 0 0 14px;
}

.filters a {
    padding: 5px 12px;
    background: var(--surface);
    border: 1px solid var(--control);
    border-radius: 16px;
    color: var(--text);
    text-decoration: none;
}

.filters a:hover {
    background-image: linear-gradient(var(--hover), var(--hover));
}

.filters a.active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--on-accent);
}

.filters .count {
    margin-left: 4px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
}

table.list {
    width: 100%;
    border-collapse: collapse;
}

table.list th, table.list td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    font-variant-numeric: tabular-nums;
}

table.list tbody tr:last-child td {
    border-bottom: 0;
}

table.list th {
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
}

table.list tbody tr:hover td {
    background: var(--surface-muted);
}

table.list tr.selected td {
    background: var(--accent-soft);
}

table.list .number {
    text-align: right;
}

table.list .row-actions {
    width: 1%;
    padding-block: 2px;
    text-align: right;
    white-space: nowrap;
}

.territory-link {
    font-weight: 600;
    text-decoration: none;
}

/* Login page: only the form, centred on the canvas */

.login-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    box-sizing: border-box;
    min-height: 100vh;
    padding: 24px 16px;
}

.login-card {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    width: 100%;
    max-width: 340px;
    padding: 24px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.login-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

.login-brand .rail-logo {
    margin: 0;
}

.login-brand h1 {
    margin: 0;
    font-size: 18px;
}

.login-card .error {
    margin: 0 0 10px;
}

.login-card button.primary {
    margin-top: 6px;
}

.login-languages {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px 14px;
    font-size: 13px;
}

.login-languages a {
    color: var(--muted);
    text-decoration: none;
}

.login-languages a[aria-current] {
    color: var(--text);
    font-weight: 600;
}
