:root {
    --bg: #eaf7e1;
    --surface: #f4fbee;
    --primary: #7cc36b;
    --primary-dark: #5ea655;
    --accent: #b9e3a7;
    --text: #1f3d1a;
    --muted: #587a55;
    --error: #b3261e;
    --border: #c7e4b6;

    /* Per-(country, category) accent colors. The fill, stroke and
     * placed shades all derive from these via color-mix(). The user
     * can override these at runtime via the per-category color picker;
     * the JS sets the same properties on :root. */
    --col-slovenia-municipalities: #b9e3a7;
    --col-slovenia-towns:          #ff7043;
    --col-slovenia-rivers:         #1e88e5;
    --col-croatia-counties:        #f4b8a8;
    --col-croatia-municipalities:  #fde2d6;
    --col-croatia-towns:           #f1c40f;
    --col-croatia-rivers:          #117a65;
    --col-croatia-islands:         #f5deb3;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

.topbar {
    background: var(--primary);
    color: #fff;
    padding: 0.9rem 1.4rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.brand {
    font-weight: 700;
    font-size: 1.2rem;
    letter-spacing: 0.4px;
}

.dev-marker {
    margin-left: 0.25rem;
    font-size: 0.65rem;
    font-weight: 600;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    background: rgba(255, 255, 255, 0.18);
    padding: 0.05rem 0.35rem;
    border-radius: 3px;
    vertical-align: sub;
}

.topbar .user {
    margin-right: 0.6rem;
    opacity: 0.9;
}
.lang-form {
    margin-right: 0.6rem;
}
.lang-select {
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.4);
    border-radius: 4px;
    padding: 0.25rem 0.4rem;
    font-size: 0.85rem;
    cursor: pointer;
}
.lang-select option { color: #1f3d1a; }
.lang-select:focus {
    outline: none;
    border-color: #fff;
}

.inline-form {
    display: inline;
}

.link-btn {
    background: transparent;
    border: 1px solid #fff;
    color: #fff;
    padding: 0.3rem 0.8rem;
    border-radius: 4px;
    cursor: pointer;
}
.link-btn:hover { background: var(--primary-dark); border-color: var(--primary-dark); }

.container {
    max-width: 720px;
    margin: 2.5rem auto;
    padding: 0 1rem;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 2rem;
    box-shadow: 0 4px 12px rgba(94,166,85,0.08);
}

h1 {
    margin-top: 0;
    color: var(--primary-dark);
}

label {
    display: block;
    margin: 1rem 0 0.3rem;
    font-weight: 600;
    color: var(--muted);
}

input[type=text], input[type=password] {
    width: 100%;
    padding: 0.6rem 0.8rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #fff;
    font-size: 1rem;
}
input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(124,195,107,0.25);
}

.btn-primary {
    margin-top: 1.4rem;
    background: var(--primary);
    color: #fff;
    border: none;
    padding: 0.7rem 1.4rem;
    font-size: 1rem;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
}
.btn-primary:hover { background: var(--primary-dark); }

.error {
    background: #fde7e6;
    color: var(--error);
    padding: 0.6rem 0.8rem;
    border-radius: 6px;
    margin-bottom: 0.6rem;
}

/* Google sign-in button */
.google-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.7rem;
    width: 100%;
    padding: 0.65rem 1rem;
    background: #fff;
    color: #3c4043;
    border: 1px solid #dadce0;
    border-radius: 6px;
    font-size: 0.95rem;
    font-weight: 600;
    text-decoration: none;
    margin: 0.4rem 0 0.8rem;
    transition: background 120ms;
}
.google-btn:hover { background: #f6f9fc; border-color: #c4c7c5; }
.google-btn svg { display: block; flex-shrink: 0; }

.divider {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin: 0.8rem 0 0.4rem;
    color: var(--muted);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.divider::before,
.divider::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* ---------------- Puzzle layout ---------------- */
/* Break the puzzle out of the centered .container layout. */
body.puzzle-fullbleed > main.container {
    max-width: none;
    margin: 0;
    padding: 0;
}
.puzzle-wrap {
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 0;
}
.puzzle-wrap > .puzzle-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.8rem;
    margin: 1rem 1.4rem 0.4rem;
}
.puzzle-wrap > .puzzle-header h1 {
    margin: 0;
    font-size: 1.4rem;
}
.puzzle-stats {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.95rem;
    color: var(--muted);
}
.puzzle-stats strong { color: var(--primary-dark); }

.clock {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.35rem 0.85rem;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--primary-dark);
    box-shadow: 0 1px 3px rgba(94, 166, 85, 0.12);
}
.clock-icon {
    width: 1.15rem;
    height: 1.15rem;
    flex-shrink: 0;
    opacity: 0.85;
}
.header-timer {
    font-family: ui-monospace, "SF Mono", "Roboto Mono", "Menlo", monospace;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--primary-dark);
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.5px;
    line-height: 1;
}
.clock.running { border-color: var(--primary); }
.clock.running .clock-icon { animation: clock-pulse 1s ease-in-out infinite; }
@keyframes clock-pulse {
    0%, 100% { opacity: 0.85; }
    50%      { opacity: 0.45; }
}
.puzzle-wrap > .hint {
    margin: 0 1.4rem 0.8rem;
    color: var(--muted);
    font-size: 0.9rem;
}

#puzzle-stage {
    margin: 0;
    background: var(--surface);
    border: none;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    border-radius: 0;
    box-shadow: 0 4px 12px rgba(94,166,85,0.08) inset;
    width: 100%;
    height: calc(100vh - 180px);
    min-height: 480px;
    overflow: hidden;
    position: relative;
}
#puzzle-stage.solved {
    box-shadow: 0 0 0 4px var(--primary) inset, 0 4px 12px rgba(94,166,85,0.2) inset;
}
#puzzle-svg {
    width: 100%;
    height: 100%;
    display: block;
    background: var(--bg);
    cursor: default;
    touch-action: none;
}

.country-outline {
    stroke-width: 400;
    stroke-linejoin: round;
    pointer-events: none;
}
.country-outline-slovenia {
    fill: rgba(124, 195, 107, 0.10);
    stroke: #5ea655;
}
.country-outline-croatia {
    fill: rgba(192, 57, 43, 0.08);
    stroke: #c0392b;
}
.country-outline.cat-hidden { display: none; }

.piece { cursor: grab; }
.piece.dragging { cursor: grabbing; }

.piece-fill {
    stroke-width: 200;
    stroke-linejoin: round;
    transition: fill 120ms linear, stroke 120ms linear;
}

/* Generic per-category styling driven by --col-{country}-{category}.
 * Polygon-style categories use fill+stroke; rivers stroke only; towns
 * use a thicker stroke. Hover/placed shades derive from the variable. */
.piece[data-country][data-category] .piece-fill {
    fill: var(--piece-color);
    stroke: color-mix(in srgb, var(--piece-color), black 35%);
}
.piece[data-country][data-category]:hover .piece-fill {
    fill: color-mix(in srgb, var(--piece-color), black 18%);
}
.piece[data-country][data-category].placed .piece-fill {
    fill: color-mix(in srgb, var(--piece-color), black 22%);
    stroke: color-mix(in srgb, var(--piece-color), black 50%);
}

/* Towns: dot-style — slightly thicker stroke. */
.piece.cat-towns .piece-fill { stroke-width: 280; }
.piece.cat-towns .piece-label {
    fill: color-mix(in srgb, var(--piece-color), black 55%);
}

/* Rivers: stroke-only line. Use the color as the stroke directly. */
.piece.cat-rivers .piece-fill {
    fill: none !important;
    stroke: var(--piece-color);
    stroke-width: 360;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.piece.cat-rivers:hover .piece-fill {
    stroke: color-mix(in srgb, var(--piece-color), black 30%);
}
.piece.cat-rivers.placed .piece-fill {
    stroke: color-mix(in srgb, var(--piece-color), black 45%);
    stroke-width: 320;
}
.piece.cat-rivers .piece-label {
    fill: color-mix(in srgb, var(--piece-color), black 55%);
    font-style: italic;
}

/* Resolve the color variable for each piece by country+category. */
.piece.country-slovenia.cat-municipalities { --piece-color: var(--col-slovenia-municipalities); }
.piece.country-slovenia.cat-towns          { --piece-color: var(--col-slovenia-towns); }
.piece.country-slovenia.cat-rivers         { --piece-color: var(--col-slovenia-rivers); }
.piece.country-croatia.cat-counties        { --piece-color: var(--col-croatia-counties); }
.piece.country-croatia.cat-municipalities  { --piece-color: var(--col-croatia-municipalities); }
.piece.country-croatia.cat-towns           { --piece-color: var(--col-croatia-towns); }
.piece.country-croatia.cat-rivers          { --piece-color: var(--col-croatia-rivers); }
.piece.country-croatia.cat-islands         { --piece-color: var(--col-croatia-islands); }

/* Hide pieces of inactive categories or countries */
.piece.cat-hidden { display: none; }

/* Invisible wider stroke for rivers — easier to grab and rotate. */
.piece.cat-rivers .piece-hit {
    fill: none;
    stroke: rgba(0, 0, 0, 0);
    stroke-width: 4500;
    stroke-linecap: round;
    stroke-linejoin: round;
    pointer-events: stroke;
}
/* When a river is placed, its fat invisible hit-area must not capture
 * clicks — otherwise it eats events meant for an unplaced piece below. */
.piece.placed .piece-hit { pointer-events: none; }

/* Always-visible label for towns and rivers. */
.piece-label {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-weight: 600;
    paint-order: stroke fill;
    stroke: rgba(255, 255, 255, 0.92);
    stroke-width: 600;
    stroke-linejoin: round;
    pointer-events: none;
    user-select: none;
}
.piece.cat-towns .piece-label {
    font-size: 2900px;
}
.piece.cat-rivers .piece-label {
    font-size: 3600px;
    font-style: italic;
    stroke-width: 750;
}
.piece.placed .piece-label { /* keep visible after placement */ }

/* Secondary button */
.btn-secondary {
    background: #fff;
    color: var(--primary-dark);
    border: 1px solid var(--primary-dark);
    padding: 0.4rem 1rem;
    font-size: 0.9rem;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
}
.btn-secondary:hover { background: var(--accent); color: #fff; }

/* Side panel */
#side-panel {
    position: fixed;
    top: 0;
    right: 0;
    width: 340px;
    max-width: 92vw;
    height: 100vh;
    background: var(--surface);
    border-left: 1px solid var(--border);
    box-shadow: -8px 0 22px rgba(0,0,0,0.10);
    transform: translateX(105%);
    transition: transform 0.25s ease;
    z-index: 200;
    padding: 3.4rem 1.6rem 1.6rem;
    overflow-y: auto;
    box-sizing: border-box;
}
#side-panel.open { transform: translateX(0); }
#side-panel h2 {
    margin: 0 0 1rem;
    color: var(--primary-dark);
    font-size: 1.3rem;
}
.panel-close {
    position: absolute;
    top: 0.5rem;
    right: 0.7rem;
    background: transparent;
    border: none;
    color: var(--muted);
    font-size: 1.8rem;
    cursor: pointer;
    width: 2rem; height: 2rem;
    line-height: 1;
}
.panel-close:hover { color: var(--primary-dark); }

.panel-section {
    margin-bottom: 1.2rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}
.panel-section:last-child { border-bottom: none; }
.panel-section h3 {
    margin: 0 0 0.4rem;
    font-size: 0.78rem;
    text-transform: uppercase;
    color: var(--muted);
    letter-spacing: 0.6px;
}
.big-stat {
    font-size: 2.2rem;
    font-weight: 700;
    color: var(--primary-dark);
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
}

.highscore-list {
    list-style: none;
    padding: 0;
    margin: 0 0 0.5rem;
    counter-reset: hs;
}
.highscore-list li {
    padding: 0.3rem 0;
    border-bottom: 1px dashed var(--border);
    font-size: 0.9rem;
    counter-increment: hs;
}
.highscore-list li::before {
    content: counter(hs) ". ";
    color: var(--muted);
    margin-right: 0.3rem;
}
.highscore-list li.empty {
    color: var(--muted);
    font-style: italic;
    border: none;
}
.highscore-list li.empty::before { content: ""; }
.highscore-list .meta {
    color: var(--muted);
    font-size: 0.8rem;
    margin-left: 0.3rem;
}
.highscore-list .hs-hint {
    color: var(--primary-dark);
    font-size: 0.85rem;
    margin-left: 0.25rem;
    cursor: help;
}

.text-btn {
    background: none;
    border: none;
    color: var(--muted);
    font-size: 0.82rem;
    cursor: pointer;
    text-decoration: underline;
    padding: 0;
}
.text-btn:hover { color: var(--error); }

.new-game .btn-primary {
    display: block;
    width: 100%;
    margin-top: 0.4rem;
}

/* Side overlay containing categories + countries + start button */
.side-overlay {
    position: absolute;
    top: 0.9rem;
    left: 0.9rem;
    z-index: 10;
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
    width: 240px;
    max-height: calc(100% - 1.8rem);
    overflow-y: auto;
}
.categories-panel,
.countries-panel {
    background: rgba(255,255,255,0.95);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.10);
    padding: 0.7rem 0.9rem;
    font-size: 0.9rem;
    color: var(--text);
}
.categories-panel h4,
.countries-panel h4 {
    margin: 0 0 0.5rem;
    font-size: 0.78rem;
    text-transform: uppercase;
    color: var(--muted);
    letter-spacing: 0.6px;
}
.cat-group { margin-bottom: 0.4rem; }
.cat-group:last-child { margin-bottom: 0; }
.cat-group.hidden { display: none; }
.cat-group-title {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--muted);
    margin-bottom: 0.15rem;
    margin-top: 0.3rem;
    font-weight: 600;
}
.cat-row,
.country-row {
    display: grid;
    grid-template-columns: auto auto 1fr auto;
    align-items: center;
    gap: 0.5rem;
    padding: 0.18rem 0;
    cursor: pointer;
    user-select: none;
}
.country-row { grid-template-columns: auto auto 1fr; }
.cat-row input[type=checkbox],
.country-row input[type=checkbox] {
    margin: 0;
    cursor: pointer;
    accent-color: var(--primary-dark);
}
.cat-color {
    width: 14px; height: 14px;
    border-radius: 3px;
    display: inline-block;
}
.country-swatch {
    width: 14px; height: 14px;
    border-radius: 3px;
    display: inline-block;
}
/* The color picker doubles as the swatch — sized small to match. */
.cat-color-picker {
    width: 18px;
    height: 18px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 3px;
    cursor: pointer;
    background: transparent;
}
.cat-color-picker:hover { border-color: var(--primary-dark); }
.cat-color-picker::-webkit-color-swatch-wrapper { padding: 0; }
.cat-color-picker::-webkit-color-swatch { border: none; border-radius: 2px; }
.cat-color-picker::-moz-color-swatch { border: none; border-radius: 2px; }

.country-swatch-slovenia { background: var(--col-slovenia-municipalities); border: 1px solid color-mix(in srgb, var(--col-slovenia-municipalities), black 35%); }
.country-swatch-croatia  { background: var(--col-croatia-counties); border: 1px solid color-mix(in srgb, var(--col-croatia-counties), black 35%); }

.cat-name, .country-name { color: var(--text); }
.cat-count {
    color: var(--muted);
    font-variant-numeric: tabular-nums;
    font-size: 0.82rem;
}
.cat-row.complete .cat-name { color: var(--primary-dark); font-weight: 700; }
.cat-row.complete .cat-count { color: var(--primary-dark); }
.cat-row input[type=checkbox]:disabled,
.country-row input[type=checkbox]:disabled { cursor: not-allowed; }
.cat-row.locked, .country-row.locked { cursor: default; opacity: 0.85; }
.cat-row.locked .cat-name,
.country-row.locked .country-name { color: var(--muted); }

.cat-start {
    width: 100%;
    padding: 0.55rem 0.8rem;
    font-size: 0.95rem;
    margin-top: 0;
}
.cat-start:disabled {
    background: var(--border);
    cursor: not-allowed;
}

.cat-solve {
    width: 100%;
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
}
.cat-solve:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

/* Help / hint toggle — pill switch */
.help-toggle {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.4rem 0.75rem;
    background: rgba(255,255,255,0.95);
    border: 1px solid var(--border);
    border-radius: 999px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.06);
    cursor: pointer;
    user-select: none;
    font-size: 0.85rem;
    color: var(--text);
}
.help-toggle:hover { border-color: var(--primary-dark); }
.help-toggle input[type=checkbox] {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}
.help-switch {
    position: relative;
    width: 32px;
    height: 18px;
    background: var(--border);
    border-radius: 999px;
    transition: background 0.18s ease;
    flex-shrink: 0;
}
.help-switch::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 14px;
    height: 14px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 1px 2px rgba(0,0,0,0.25);
    transition: transform 0.18s ease;
}
.help-toggle input:checked + .help-switch { background: var(--primary); }
.help-toggle input:checked + .help-switch::after { transform: translateX(14px); }
.help-toggle input:focus-visible + .help-switch {
    outline: 2px solid var(--primary-dark);
    outline-offset: 2px;
}
.help-label { font-weight: 600; }

/* Ghost outline layer — light shapes showing correct piece positions. */
#ghost-layer { pointer-events: none; }
#ghost-layer.ghost-hidden { display: none; }
.ghost { pointer-events: none; }
.ghost .ghost-path {
    fill: none;
    stroke: color-mix(in srgb, var(--piece-color), black 30%);
    stroke-width: 220;
    stroke-linejoin: round;
    stroke-linecap: round;
    stroke-dasharray: 1400 700;
    opacity: 0.45;
    pointer-events: none;
}
.ghost.cat-towns .ghost-path {
    stroke-width: 220;
    stroke-dasharray: 700 700;
    opacity: 0.55;
}
.ghost.cat-rivers .ghost-path {
    stroke-width: 280;
    stroke-dasharray: 1500 800;
    opacity: 0.5;
}

/* Zoom controls */
.zoom-controls {
    position: absolute;
    bottom: 0.9rem;
    right: 0.9rem;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    z-index: 10;
}
.zoom-controls button {
    width: 2.4rem;
    height: 2.4rem;
    background: rgba(255,255,255,0.95);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--primary-dark);
    box-shadow: 0 2px 4px rgba(0,0,0,0.10);
}
.zoom-controls button:hover {
    background: var(--accent);
    color: #fff;
}

/* Flash message */
.flash-msg {
    position: absolute;
    top: 0.8rem;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0,0,0,0.78);
    color: #fff;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-size: 0.9rem;
    z-index: 50;
    animation: flashOut 2.2s forwards;
    pointer-events: none;
}
@keyframes flashOut {
    0%   { opacity: 1; }
    70%  { opacity: 1; }
    100% { opacity: 0; }
}
