/* ==========================================================================
   Rail Route - Design tokens & base
   Reusable, scalable styling via CSS custom properties
   ========================================================================== */

:root {
    /* Playful game palette */
    --color-primary: #2563eb;
    --color-primary-dark: #1d4ed8;
    --color-primary-light: #93c5fd;
    --color-primary-pale: #bfdbfe;
    --color-indigo-900: #1e3a8a;

    /* Semantic */
    --color-success: #16a34a;
    --color-success-bg: #dcfce7;
    --color-danger: #dc2626;
    --color-danger-dark: #b91c1c;
    --color-amber: #f59e0b;
    --color-muted: #64748b;
    --color-muted-text: #475569;
    --color-canvas-bg: #fff;

    /* Spacing scale */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 15px;
    --space-lg: 20px;
    --space-xl: 24px;
    --space-2xl: 40px;
    --screen-margin: 10px;

    /* Radii */
    --radius-md: 12px;
    --radius-pill: 50px;
    --radius-full: 50%;

    /* Typography */
    --font-sans: 'Fredoka', sans-serif;
    --text-xs: 0.75rem;
    --text-3xl: 1.875rem;
    --text-4xl: 2.25rem;
    --font-bold: 600;
}

/* ==========================================================================
   Base
   ========================================================================== */

html, body {
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-sans);
    background: #020617;
    color: var(--color-indigo-900);
    overflow: hidden;
    /* Allow vertical scrolling on overlays (modals), but keep horizontal locked
       so the game canvas still feels anchored. */
    touch-action: pan-y;
    user-select: none;
}

/* ==========================================================================
   Layout
   ========================================================================== */

.game-wrapper {
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    background: #000 url("../img/bg.png") center center / cover no-repeat;
}

.game-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    z-index: 10;
    border-radius: 0;
    /* Let the global bg image show through */
    background: transparent;
    cursor: default;
}

.game-canvas:active {
    cursor: default;
}

/* ==========================================================================
   UI layer
   ========================================================================== */

.ui-layer {
    position: absolute;
    inset: 0;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 0;
    z-index: 20;
}

.capacity-card {
    position: absolute;
    right: var(--screen-margin);
    bottom: var(--screen-margin);
    z-index: 25;
    padding: 10px 14px;
    border-radius: 18px;
    background: radial-gradient(circle at 0% 0%, rgba(56, 189, 248, 0.3), rgba(15, 23, 42, 0.96));
    border: 1px solid rgba(148, 163, 184, 0.85);
    box-shadow: 0 18px 40px rgba(15, 23, 42, 0.9);
    color: #e5e7eb;
    min-width: 180px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: auto;
}

.capacity-card__badge {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.16em;
    opacity: 0.9;
}

.capacity-card__value {
    font-size: 2.1rem;
    font-weight: var(--font-bold);
    text-shadow: 0 4px 10px rgba(15, 23, 42, 0.9);
}

.capacity-card__btn {
    margin-top: 2px;
    width: 100%;
    border-radius: 999px;
    border: none;
    padding: 7px 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    background: linear-gradient(90deg, #22c55e, #16a34a);
    color: #ecfdf5;
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(22, 163, 74, 0.7);
    transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease, opacity 0.12s ease;
}

.capacity-card__btn:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 12px 32px rgba(22, 163, 74, 0.9);
    filter: brightness(1.03);
}

.capacity-card__btn:active:not(:disabled) {
    transform: translateY(1px);
    box-shadow: 0 5px 16px rgba(15, 23, 42, 0.95);
}

.capacity-card__btn:disabled {
    background: rgba(15, 23, 42, 0.65);
    color: rgba(148, 163, 184, 0.85);
    box-shadow: none;
    cursor: default;
}

.capacity-card__btn-label {
    text-transform: uppercase;
    letter-spacing: 0.12em;
}

.capacity-card__btn-cost {
    font-weight: 700;
}

.capacity-card__btn-coin {
    width: 20px;
    height: 20px;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at 30% 0%, #fef9c3, #facc15);
    color: #92400e;
    font-size: 0.8rem;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    pointer-events: auto;
    margin: var(--screen-margin);
    background: transparent;
}

.header__left {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.header__level {
    font-size: 1rem;
    font-weight: var(--font-bold);
    text-transform: uppercase;
    letter-spacing: 0.14em;
    color: #ffffff;
    text-shadow: 0 2px 4px rgba(15, 23, 42, 0.9);
}

.header__goal {
    font-size: 0.95rem;
    color: var(--color-muted-text);
    font-weight: 500;
    display: none;
}

.header__actions {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    margin-left: auto;
}

.header__coins {
    pointer-events: none;
}

.coin-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: 999px;
    background: radial-gradient(circle at 20% 0%, #facc15, #f97316);
    box-shadow: 0 10px 26px rgba(15, 23, 42, 0.75);
    border: 1px solid rgba(252, 211, 77, 0.85);
    color: #1f2933;
    font-size: 0.9rem;
    font-weight: 600;
    text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}

.coin-pill__icon {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at 30% 0%, #fef9c3, #facc15);
    color: #92400e;
    box-shadow: 0 0 0 2px rgba(250, 204, 21, 0.7);
    font-size: 0.9rem;
}

.coin-pill__value {
    min-width: 40px;
    text-align: right;
}

.header__paths {
    min-width: 120px;
    padding: 6px 14px;
    border-radius: 999px;
    background: rgba(15, 23, 42, 0.78);
    border: 1px solid rgba(148, 163, 184, 0.9);
    box-shadow: 0 10px 26px rgba(15, 23, 42, 0.65);
    color: #e5e7eb;
    font-size: 0.85rem;
    font-weight: 500;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.header-btn {
    width: 42px;
    height: 42px;
    border-radius: var(--radius-full);
    border: none;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.1rem;
    cursor: pointer;
    background: rgba(15, 23, 42, 0.72);
    color: #e5e7eb;
    border: 1px solid rgba(148, 163, 184, 0.85);
    box-shadow: 0 10px 26px rgba(15, 23, 42, 0.65);
    transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease, color 0.12s ease;
}

.header-btn:hover {
    transform: translateY(-1px);
    background: rgba(30, 64, 175, 0.88);
    color: #f9fafb;
    box-shadow: 0 14px 32px rgba(15, 23, 42, 0.85);
}

.header-btn:active {
    transform: translateY(1px);
    box-shadow: 0 6px 18px rgba(15, 23, 42, 0.9);
}

.header-btn--secondary {
    background: rgba(15, 23, 42, 0.72);
    color: #f9fafb;
}

.header-btn--secondary:hover {
    background: rgba(30, 64, 175, 0.88);
}

.header-btn--reset {
    background: rgba(15, 23, 42, 0.7);
    color: #e5e7eb;
}

.header-btn--reset:hover {
    background: rgba(15, 23, 42, 0.9);
    color: #f9fafb;
}

/* ==========================================================================
   Modals
   ========================================================================== */

.modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    backdrop-filter: blur(18px);
    z-index: 50;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: var(--space-xl) var(--space-md);
    box-sizing: border-box;
    /* Let tall content scroll on small screens instead of hiding buttons */
    overflow-y: auto;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.modal.active {
    opacity: 1;
    pointer-events: auto;
}

.modal__card {
    background: radial-gradient(circle at 0% 0%, #ffffff, #f1f5f9);
    padding: var(--space-xl);
    border-radius: 28px;
    text-align: center;
    max-width: 360px;
    width: 88%;
    max-height: calc(100vh - 80px);
    overflow-y: auto;
    box-shadow:
        0 26px 60px rgba(15, 23, 42, 0.45),
        0 0 0 1px rgba(148, 163, 184, 0.35);
    border: 1px solid rgba(148, 163, 184, 0.55);
}

.modal__logo {
    display: block;
    max-width: 160px;
    width: 50%;
    margin: 0 auto var(--space-lg);
    filter: drop-shadow(0 14px 22px rgba(15, 23, 42, 0.45));
}

.modal__title {
    font-size: var(--text-3xl);
    font-weight: var(--font-bold);
    color: var(--color-primary);
    margin-bottom: var(--space-xs);
    text-shadow: 0 1px 0 rgba(255, 255, 255, 0.8);
}

.modal__title--success {
    color: var(--color-success);
    font-size: var(--text-4xl);
}

.modal__title--danger {
    color: var(--color-danger);
    font-size: var(--text-4xl);
}

.modal__desc {
    color: var(--color-muted-text);
    font-size: 1.02rem;
    line-height: 1.6;
}

.modal__desc--spaced {
    margin-bottom: var(--space-md);
}

.modal__emoji {
    font-size: 4rem;
    margin-bottom: var(--space-md);
    line-height: 1;
}

/* ==========================================================================
   Responsive layout
   Keep the 12x12 board aspect via JS; here we just
   adapt UI scale, spacing and hit areas.
   ========================================================================== */

/* Small Mobile (≤480px) */
@media (max-width: 480px) {
    :root {
        --screen-margin: 6px;
    }

    .header {
        margin: var(--screen-margin);
    }

    .header__level {
        font-size: 0.85rem;
    }

    .header__goal {
        font-size: 0.8rem;
    }

    .header-btn {
        width: 34px;
        height: 34px;
        font-size: 0.95rem;
    }

    .coin-pill {
        padding: 4px 10px;
        font-size: 0.8rem;
    }

    .coin-pill__icon {
        width: 18px;
        height: 18px;
        font-size: 0.75rem;
    }

    .header__paths {
        min-width: 90px;
        padding: 4px 10px;
        font-size: 0.75rem;
    }

    .modal__card {
        padding: 14px;
        border-radius: 20px;
        width: 86%;
    }

    .modal__title {
        font-size: 1.5rem;
    }

    .modal__desc {
        font-size: 0.9rem;
    }

    .btn-main {
        font-size: 0.95rem;
        padding: 10px 26px;
    }

    .tut-grid {
        grid-template-columns: 1fr;
        text-align: center;
    }

    .cell-menu__btn {
        width: 42px;
        height: 42px;
        font-size: 1.3rem;
    }

    #junctionMenu {
        padding: 18px 16px;
    }
}

/* Mobile (≤640px) */
@media (max-width: 640px) {
    .header__actions {
        gap: 6px;
    }

    .tut-grid {
        margin: var(--space-lg) 0;
    }

    .solution-modal__canvas-wrap {
        --solution-size: min(320px, 80vw);
    }
}

/* Tablet Portrait (641px-768px) */
@media (min-width: 641px) and (max-width: 768px) {
    :root {
        --screen-margin: 10px;
    }

    .modal__card {
        max-width: 360px;
        max-height: 100vh;
    }

    .solution-modal__canvas-wrap {
        --solution-size: min(360px, 80vw);
    }
}

/* Tablet Landscape (769px-1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
    :root {
        --screen-margin: 14px;
    }

    .header__level {
        font-size: 1.05rem;
    }

    .solution-modal__canvas-wrap {
        --solution-size: min(420px, 70vh);
    }
}

/* Desktop (≥1025px) */
@media (min-width: 1025px) {
    :root {
        --screen-margin: 18px;
    }

    .modal__card {
        max-width: 400px;
        max-height: 85vh;
    }
}

/* Landscape Mobile (orientation: landscape) */
@media (max-width: 812px) and (max-height: 480px) and (orientation: landscape) {
    .header {
        margin: 4px 6px;
    }

    .header-btn {
        width: 32px;
        height: 32px;
    }

    .header__paths {
        padding: 3px 8px;
        font-size: 0.7rem;
    }

    .coin-pill {
        padding: 3px 8px;
        font-size: 0.75rem;
    }

    .modal__card {
        padding: 12px 16px;
        border-radius: 18px;
        max-width: 320px;
        width: 84%;
        max-height: 80vh;
    }

    .modal__emoji {
        font-size: 3rem;
    }

    .btn-main {
        padding: 8px 20px;
        font-size: 0.9rem;
    }
}

/* ==========================================================================
   Buttons
   ========================================================================== */

.btn-main {
    background: radial-gradient(circle at 0% 0%, #60a5fa, #2563eb 55%, #1d4ed8 100%);
    color: #fff;
    font-family: var(--font-sans);
    font-size: 1.1rem;
    font-weight: var(--font-bold);
    padding: 14px 40px;
    border-radius: var(--radius-pill);
    border: none;
    cursor: pointer;
    margin-top: var(--space-lg);
    text-transform: uppercase;
    letter-spacing: 0.12em;
    box-shadow:
        0 10px 0 rgba(15, 23, 42, 0.8),
        0 18px 40px rgba(37, 99, 235, 0.55);
    transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

.btn-main:hover {
    transform: translateY(-2px);
    filter: brightness(1.03);
    box-shadow:
        0 12px 0 rgba(15, 23, 42, 0.85),
        0 24px 55px rgba(37, 99, 235, 0.65);
}

.btn-main:active {
    transform: translateY(3px);
    box-shadow:
        0 5px 0 rgba(15, 23, 42, 0.9),
        0 14px 30px rgba(30, 64, 175, 0.7);
}

.btn-main--danger {
    background: linear-gradient(180deg, var(--color-danger) 0%, var(--color-danger-dark) 100%);
    box-shadow: 0 6px 0 var(--color-danger-dark), 0 8px 20px rgba(220, 38, 38, 0.35);
}

.btn-main--danger:hover {
    box-shadow: 0 8px 0 var(--color-danger-dark), 0 10px 24px rgba(220, 38, 38, 0.4);
}

.btn-main--danger:active {
    box-shadow: 0 3px 0 var(--color-danger-dark);
}

/* ==========================================================================
   Tutorial grid (start modal)
   ========================================================================== */

.tut-grid {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: var(--space-md) var(--space-lg);
    text-align: left;
    margin: var(--space-xl) 0;
    padding: var(--space-md) var(--space-lg);
    color: var(--color-muted-text);
    background: rgba(226, 232, 240, 0.7);
    border-radius: 18px;
}

.tut-grid__text {
    align-self: center;
    font-size: 1rem;
}

.tut-icon {
    width: 44px;
    height: 44px;
    background: linear-gradient(145deg, var(--color-primary-light), var(--color-primary-pale));
    border-radius: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
    color: var(--color-primary);
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.2);
}

.tut-icon--warning {
    background: linear-gradient(145deg, #fef3c7, #fde68a);
    color: #b45309;
    box-shadow: 0 2px 8px rgba(245, 158, 11, 0.25);
}

.tut-icon--success {
    background: linear-gradient(145deg, var(--color-success-bg), #bbf7d0);
    color: var(--color-success);
    box-shadow: 0 2px 8px rgba(22, 163, 74, 0.2);
}

/* ==========================================================================
   Solution modal
   ========================================================================== */

.solution-modal .modal__card {
    max-width: 520px;
}

.solution-modal__canvas-wrap {
    --solution-size: 400px;
    width: var(--solution-size);
    height: var(--solution-size);
    margin: var(--space-lg) 0;
    border-radius: var(--radius-md);
    overflow: hidden;
    background: var(--color-canvas-bg);
    border: 4px solid var(--color-primary-light);
}

.solution-modal__canvas {
    display: block;
    width: 100%;
    height: 100%;
}

/* ==========================================================================
   Cell menu (click on cell: place path or clear)
   ========================================================================== */

.cell-menu {
    position: fixed;
    z-index: 30;
    display: flex;
    gap: var(--space-xs);
    padding: var(--space-sm);
    background: linear-gradient(180deg, #fff 0%, #f1f5f9 100%);
    border-radius: 14px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2), 0 0 0 2px var(--color-primary-light);
    pointer-events: auto;
}

.cell-menu[hidden] {
    display: none !important;
}

.cell-menu__btn {
    width: 48px;
    height: 48px;
    border: none;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.9);
    color: var(--color-primary);
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.12s ease, background 0.12s ease;
}

.cell-menu__btn:hover {
    background: var(--color-primary-pale);
    transform: scale(1.08);
}

.cell-menu__btn:active {
    transform: scale(0.96);
}

.cell-menu__btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

.cell-menu__btn--rotate {
    color: var(--color-primary);
}

.cell-menu__btn--rotate:hover:not(:disabled) {
    background: var(--color-primary-pale);
}

.cell-menu__btn--clear {
    color: var(--color-danger);
}

.cell-menu__btn--clear:hover {
    background: var(--color-danger-bg, #fef2f2);
}

.cell-menu__btn--go {
    /* Filled game-y drive button: solid color with white icon */
    background: var(--color-success);
    color: #ffffff;
    border-radius: 12px;
    border: none;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
    font-size: 1.6rem;
}

.cell-menu__btn--go:hover {
    filter: brightness(1.08);
}

/* Color variants for drive buttons (per-destination junction color) */
.cell-menu__btn--go-red {
    background: #ef4444;
}

.cell-menu__btn--go-blue {
    background: #3b82f6;
}

.cell-menu__btn--go-green {
    background: #22c55e;
}

.cell-menu__btn--go-yellow {
    background: #eab308;
}

.cell-menu__btn--go-orange {
    background: #f97316;
}

.cell-menu__btn--go-purple {
    background: #a855f7;
}

.cell-menu-overlay {
    position: absolute;
    inset: 0;
    z-index: 25;
    pointer-events: none;
}

.cell-menu-overlay[hidden] {
    display: none !important;
}

.cell-menu-overlay.active {
    pointer-events: auto;
    display: block;
}

/* Junction passenger & destination layout */
#junctionMenu {
    flex-direction: column;
    align-items: center;
    gap: 18px;
    padding: 22px 24px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.98) 0%, rgba(248, 250, 252, 0.98) 100%);
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.2), 0 2px 8px rgba(15, 23, 42, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.8);
    overflow: visible;
}

#junctionMenu .cell-menu__btn {
    width: 44px;
    height: 44px;
    font-size: 1.25rem;
}

.junction-menu__section {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: 12px;
    overflow: visible;
}

.junction-menu__label {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--color-primary);
    margin: 0;
    text-align: center;
}

.junction-menu__passenger-row {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
    align-items: center;
    max-width: 100%;
    min-height: 48px;
    padding: 6px 0;
    overflow: visible;
}

.junction-menu__dest-row {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}

/* Single solid circle per passenger (no inner dot) */
.junction-menu__passenger-pill {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    padding: 0;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    position: relative;
    flex-shrink: 0;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    overflow: visible;
}

.junction-menu__passenger-pill--red {
    background: #ef4444;
}

.junction-menu__passenger-pill--blue {
    background: #3b82f6;
}

.junction-menu__passenger-pill--green {
    background: #22c55e;
}

.junction-menu__passenger-pill--yellow {
    background: #eab308;
}

.junction-menu__passenger-pill--orange {
    background: #f97316;
}

.junction-menu__passenger-pill--purple {
    background: #a855f7;
}

.junction-menu__passenger-pill:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.junction-menu__passenger-pill--active {
    outline: none;
    box-shadow: 0 0 0 3px #fff, 0 0 0 5px var(--color-primary);
    transform: scale(1.08);
}

/* Dots still used elsewhere (e.g. on grid) */
.junction-menu__passenger-dot {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid rgba(15, 23, 42, 0.2);
}

.junction-menu__passenger-dot--red { background: #ef4444; }
.junction-menu__passenger-dot--blue { background: #3b82f6; }
.junction-menu__passenger-dot--green { background: #22c55e; }
.junction-menu__passenger-dot--yellow { background: #eab308; }
.junction-menu__passenger-dot--orange { background: #f97316; }
.junction-menu__passenger-dot--purple { background: #a855f7; }

/* Count badge when multiple passengers share same color - inside circle bottom-right */
.junction-menu__passenger-count {
    position: absolute;
    bottom: -2px;
    right: -2px;
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
    border-radius: 9px;
    background: #0f172a;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
    border: 2px solid #fff;
}
