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

* { 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;
}

.topbar .user {
    margin-right: 0.8rem;
    opacity: 0.9;
}

.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); }
.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 {
    fill: rgba(124,195,107,0.10);
    stroke: var(--primary-dark);
    stroke-width: 400;
    stroke-linejoin: round;
    pointer-events: none;
}

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

/* Default (municipalities) */
.piece-fill {
    fill: var(--accent);
    stroke: var(--primary-dark);
    stroke-width: 200;
    stroke-linejoin: round;
    transition: fill 120ms linear;
}
.piece:hover .piece-fill { fill: var(--primary); }

.piece.placed { cursor: default; pointer-events: none; }
.piece.placed .piece-fill {
    fill: var(--primary);
    stroke: var(--primary-dark);
}

/* Towns: orange-red filled circles */
.piece.cat-towns .piece-fill {
    fill: #ff7043;
    stroke: #b03a17;
    stroke-width: 280;
}
.piece.cat-towns:hover .piece-fill { fill: #ff5722; }
.piece.cat-towns.placed .piece-fill { fill: #b03a17; stroke: #5a1d0a; }

/* Rivers: blue stroked lines, no fill */
.piece.cat-rivers .piece-fill {
    fill: none;
    stroke: #1e88e5;
    stroke-width: 360;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.piece.cat-rivers:hover .piece-fill { stroke: #0d47a1; }
.piece.cat-rivers.placed .piece-fill { stroke: #0d47a1; stroke-width: 320; }

/* Hide pieces of inactive categories */
.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;
}

/* 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;
    fill: #5a1d0a;
}
.piece.cat-rivers .piece-label {
    font-size: 3600px;
    fill: #0d47a1;
    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;
}

.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;
}

/* Categories panel (left overlay on stage) */
.categories-panel {
    position: absolute;
    top: 0.9rem;
    left: 0.9rem;
    z-index: 10;
    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;
    min-width: 200px;
    font-size: 0.9rem;
    color: var(--text);
}
.categories-panel h4 {
    margin: 0 0 0.5rem;
    font-size: 0.78rem;
    text-transform: uppercase;
    color: var(--muted);
    letter-spacing: 0.6px;
}
.cat-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;
}
.cat-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;
}
.cat-color[data-category="municipalities"] { background: var(--accent); border: 1px solid var(--primary-dark); }
.cat-color[data-category="towns"]          { background: #ff7043; border: 1px solid #b03a17; }
.cat-color[data-category="rivers"]         { background: #1e88e5; border: 1px solid #0d47a1; }
.cat-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); }

/* 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; }
}
