/**
 * Custom Train Tracks - Modern Railway Puzzle Styling
 */

:root {
    --bg-primary: #0f172a;
    --bg-surface: #1e293b;
    --bg-card: #273549;
    --border-color: #334155;
    --border-subtle: #1e293b;

    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;

    --accent-rail: #38bdf8;
    --accent-rail-hover: #0ea5e9;
    --accent-gold: #fbbf24;

    --status-satisfied: #10b981;
    --status-satisfied-bg: rgba(16, 185, 129, 0.15);
    --status-over: #ef4444;
    --status-over-bg: rgba(239, 68, 68, 0.2);
    --status-under: #64748b;
    --status-under-bg: rgba(100, 116, 139, 0.1);

    --cell-bg: #33463a;
    --cell-fixed-bg: #2c3d33;
    --cell-hover: #405849;
    --cell-border: #405348;

    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --radius-full: 9999px;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 12px 28px rgba(0, 0, 0, 0.5);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow-x: hidden;
}

/* App Header */
header {
    width: 100%;
    max-width: 900px;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
}

.logo-section {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    font-size: 28px;
    filter: drop-shadow(0 2px 6px rgba(56, 189, 248, 0.4));
}

h1 {
    font-size: 20px;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--text-primary);
}

.subtitle {
    font-size: 12px;
    color: var(--accent-rail);
    font-weight: 500;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.timer-pill {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    padding: 6px 14px;
    border-radius: var(--radius-full);
    font-size: 15px;
    font-family: monospace;
    font-weight: bold;
    color: var(--accent-rail);
    box-shadow: var(--shadow-sm);
}

/* Main Layout */
main {
    width: 100%;
    max-width: 900px;
    padding: 16px 20px 40px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

/* Level & Seed Bar */
.level-bar {
    width: 100%;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 10px 16px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.daily-group, .size-group, .preset-group {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--text-secondary);
}

.daily-label {
    font-weight: 600;
    color: var(--accent-rail);
    margin-right: 4px;
}

.date-picker-button {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    outline: none;
    transition: border-color 0.2s;
    font-family: inherit;
    cursor: pointer;
}

.date-picker-button:hover,
.date-picker-button:focus-visible {
    border-color: var(--accent-rail);
    background: var(--bg-primary);
}

select, input[type="text"] {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    outline: none;
    transition: border-color 0.2s;
}

select:focus, input[type="text"]:focus {
    border-color: var(--accent-rail);
}

button {
    cursor: pointer;
    font-family: inherit;
    font-size: 13px;
    font-weight: 600;
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    background: var(--bg-card);
    color: var(--text-primary);
    transition: all 0.15s ease-in-out;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

button:hover {
    background: var(--border-color);
    border-color: var(--accent-rail);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

button:disabled:hover {
    background: var(--bg-card);
    border-color: var(--border-color);
}

button.primary {
    background: #0284c7;
    border-color: #38bdf8;
    color: #ffffff;
}

button.primary:hover {
    background: #0369a1;
}

button.active {
    background: var(--accent-rail);
    color: #0f172a;
    border-color: #38bdf8;
}

/* Board Wrapper */
.board-wrapper {
    position: relative;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 16px;
    box-shadow: var(--shadow-lg);
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    max-width: 650px;
    touch-action: none;
}

.route-train-unit {
    position: absolute;
    z-index: 20;
    pointer-events: none;
    opacity: 0;
    filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.65));
    will-change: left, top, transform;
}

.route-engine {
    width: clamp(54px, 11%, 76px);
    aspect-ratio: 58 / 36;
}

.route-carriage {
    width: clamp(50px, 10%, 70px);
    aspect-ratio: 50 / 32;
}

.route-train-unit svg {
    display: block;
    width: 100%;
    height: 100%;
    overflow: visible;
}

/* Grid Layout */
#grid-container {
    display: grid;
    gap: 3px;
    background-color: var(--border-subtle);
    padding: 4px;
    border-radius: var(--radius-md);
    width: 100%;
    aspect-ratio: 1 / 1;
}

.clue-spacer {
    width: 100%;
    height: 100%;
}

/* Clue Elements */
.col-clue, .row-clue {
    background: var(--status-under-bg);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    font-weight: 700;
    font-size: 18px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
    min-width: 32px;
    min-height: 32px;
}

.col-clue {
    flex-direction: column;
    padding: 4px 0;
}

.row-clue {
    flex-direction: row;
    padding: 0 6px;
}

.clue-target {
    font-size: 20px;
}

/* Clue Status Colors */
.status-satisfied {
    background: var(--status-satisfied-bg) !important;
    color: var(--status-satisfied) !important;
    border: 1px solid rgba(16, 185, 129, 0.4);
    box-shadow: 0 0 8px rgba(16, 185, 129, 0.2);
}

.status-over {
    background: var(--status-over-bg) !important;
    color: var(--status-over) !important;
    border: 1px solid rgba(239, 68, 68, 0.4);
    animation: shake 0.3s ease;
}

.status-under {
    background: var(--status-under-bg);
    color: var(--text-secondary);
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-2px); }
    75% { transform: translateX(2px); }
}

/* Grid Cells */
.grid-cell {
    position: relative;
    background-color: var(--cell-bg);
    background-image:
        radial-gradient(ellipse at 18% 78%, rgba(210, 195, 112, 0.16) 0 3%, transparent 4%),
        radial-gradient(ellipse at 74% 28%, rgba(142, 170, 104, 0.14) 0 3%, transparent 4%),
        repeating-linear-gradient(112deg, transparent 0 13px, rgba(226, 211, 137, 0.035) 14px 15px),
        linear-gradient(145deg, rgba(255, 255, 255, 0.025), rgba(13, 30, 22, 0.12));
    background-size: 31px 27px, 37px 33px, auto, auto;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.15s ease, transform 0.1s ease;
    overflow: hidden;
}

.grid-cell:hover {
    background-color: var(--cell-hover);
}

#grid-container.puzzle-locked .grid-cell,
#grid-container.puzzle-locked .col-clue,
#grid-container.puzzle-locked .row-clue {
    cursor: default;
}

#grid-container.puzzle-locked .grid-cell:hover {
    background-color: var(--cell-bg);
}

.cell-fixed {
    background-color: var(--cell-fixed-bg);
}

.cell-x {
    color: #f87171;
}

.cell-error {
    outline: 2px solid var(--status-over);
    box-shadow: 0 0 10px rgba(239, 68, 68, 0.4);
}

.cell-highlight {
    animation: pulse 1s infinite alternate;
}

@keyframes pulse {
    0% { outline: 2px solid var(--accent-rail); }
    100% { outline: 3px solid var(--accent-gold); }
}

.tile-svg {
    width: 100%;
    height: 100%;
    display: block;
    pointer-events: none;
}

.cell-scenery { background: #52654a; }

.scenery-svg {
    animation: scenery-arrive 900ms cubic-bezier(0.22, 0.8, 0.32, 1) both;
    transform-origin: center bottom;
}

.cell-x.x-clearing .tile-svg {
    animation: x-clear 150ms ease-in forwards;
}

@keyframes x-clear {
    to { opacity: 0; transform: scale(0.72); }
}

@keyframes scenery-arrive {
    from { opacity: 0; transform: scale(0.72) translateY(8px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
    .scenery-svg,
    .cell-x.x-clearing .tile-svg { animation: none; }
}

/* Toolbar */
.toolbar-section {
    width: 100%;
    max-width: 650px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 10px 14px;
}

.tool-group, .action-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.tool-btn {
    padding: 6px 10px;
    font-size: 13px;
}

.shortcut-badge {
    font-size: 10px;
    opacity: 0.6;
    background: rgba(0, 0, 0, 0.3);
    padding: 1px 4px;
    border-radius: 4px;
    margin-left: 2px;
}

/* Status Message */
.status-bar {
    width: 100%;
    max-width: 650px;
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    text-align: center;
    font-weight: 500;
}

.status-info {
    background: rgba(56, 189, 248, 0.1);
    color: #7dd3fc;
    border: 1px solid rgba(56, 189, 248, 0.2);
}

.status-error {
    background: rgba(239, 68, 68, 0.15);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.3);
}

.status-warning {
    background: rgba(251, 191, 36, 0.15);
    color: #fde047;
    border: 1px solid rgba(251, 191, 36, 0.3);
}

.status-success {
    background: rgba(16, 185, 129, 0.2);
    color: #6ee7b7;
    border: 1px solid rgba(16, 185, 129, 0.4);
}

/* Modals */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(15, 23, 42, 0.75);
    backdrop-filter: blur(6px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
}

.modal-backdrop.hidden {
    display: none;
}

.modal-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 24px;
    max-width: 500px;
    width: 100%;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 12px;
}

.modal-header h2 {
    font-size: 18px;
    font-weight: 700;
}

.rules-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.rules-list strong {
    color: var(--text-primary);
}

.rule-item {
    background: var(--bg-primary);
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    border-left: 3px solid var(--accent-rail);
}

.settings-section {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.settings-section strong {
    color: var(--text-primary);
}

.settings-section p {
    margin-top: 4px;
    font-size: 14px;
}

.calendar-card {
    max-width: 900px;
}

.calendar-header > div {
    text-align: left;
}

.calendar-header p {
    margin-top: 3px;
    color: var(--text-secondary);
    font-size: 13px;
}

.calendar-navigation {
    display: flex;
    justify-content: space-between;
    gap: 8px;
}

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

.calendar-month {
    min-width: 0;
    padding: 12px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
}

.calendar-month h3 {
    margin-bottom: 10px;
    color: var(--text-primary);
    font-size: 15px;
    text-align: center;
}

.calendar-weekdays,
.calendar-days {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 4px;
}

.calendar-weekdays {
    margin-bottom: 5px;
}

.calendar-weekdays span {
    color: var(--text-muted);
    font-size: 10px;
    font-weight: 700;
    text-align: center;
}

.calendar-day,
.calendar-day-spacer {
    aspect-ratio: 1;
    min-width: 0;
}

button.calendar-day {
    position: relative;
    justify-content: center;
    padding: 0;
    border-color: transparent;
    background: transparent;
    font-size: 12px;
}

button.calendar-day:hover:not(:disabled) {
    background: var(--cell-hover);
}

button.calendar-day.today {
    border-color: var(--accent-rail);
}

button.calendar-day.selected {
    color: #082f49;
    background: #7dd3fc;
    border-color: #bae6fd;
}

button.calendar-day.completed {
    color: #d1fae5;
    background: rgba(16, 185, 129, 0.2);
    border-color: rgba(52, 211, 153, 0.55);
}

button.calendar-day.completed.selected {
    color: #052e2b;
    background: #6ee7b7;
}

button.calendar-day.completed::after {
    content: "✓";
    position: absolute;
    right: 2px;
    bottom: 0;
    color: #34d399;
    font-size: 8px;
    font-weight: 900;
}

button.calendar-day.completed.selected::after {
    color: #065f46;
}

button.calendar-day:disabled {
    color: #475569;
    background: transparent;
    border-color: transparent;
    cursor: default;
    opacity: 0.45;
}

.calendar-legend {
    display: flex;
    justify-content: center;
    gap: 20px;
    color: var(--text-secondary);
    font-size: 12px;
}

.calendar-legend span {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.legend-swatch {
    width: 12px;
    height: 12px;
    border-radius: 4px;
}

.legend-swatch.completed {
    background: rgba(16, 185, 129, 0.45);
    border: 1px solid #34d399;
}

.legend-swatch.selected {
    background: #7dd3fc;
    border: 1px solid #bae6fd;
}

button.danger-button {
    color: #fecaca;
    background: rgba(239, 68, 68, 0.14);
    border-color: rgba(239, 68, 68, 0.45);
}

button.danger-button:hover {
    color: #ffffff;
    background: rgba(239, 68, 68, 0.3);
    border-color: #ef4444;
}

.victory-card {
    text-align: center;
    align-items: center;
}

.victory-actions {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 10px;
}

.victory-actions button {
    padding: 10px 18px;
    font-size: 15px;
}

.victory-icon {
    font-size: 54px;
    animation: bounce 1s infinite alternate;
}

@keyframes bounce {
    0% { transform: translateY(0); }
    100% { transform: translateY(-8px); }
}

/* Mobile Responsiveness */
@media (max-width: 600px) {
    body {
        align-items: stretch;
    }
    main {
        max-width: none;
        padding: 10px 0 28px;
        gap: 10px;
    }
    .level-bar,
    .status-bar,
    .toolbar-section {
        width: calc(100% - 16px);
        margin-inline: auto;
    }
    .board-wrapper {
        width: 100vw;
        max-width: none;
        padding: 2px 0;
        border-right: 0;
        border-left: 0;
        border-radius: 0;
        box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
    }
    #grid-container {
        gap: clamp(1px, 0.65vw, 3px);
        padding: 2px;
        border-radius: 0;
    }
    .col-clue,
    .row-clue {
        min-width: 0;
        min-height: 0;
        border-radius: 3px;
    }
    .col-clue {
        padding: 2px 0;
    }
    .row-clue {
        padding: 0 3px;
    }
    header {
        padding: 10px 8px;
        flex-direction: column;
        align-items: stretch;
        gap: 9px;
    }
    .header-controls {
        width: 100%;
        justify-content: space-between;
        gap: 4px;
    }
    .header-controls button {
        padding: 6px 8px;
        font-size: 11px;
    }
    .timer-pill {
        padding: 6px 10px;
        font-size: 13px;
    }
    .level-bar {
        padding: 8px;
        gap: 8px;
    }
    .daily-group {
        width: 100%;
    }
    .date-picker-button {
        flex: 1;
        justify-content: center;
        min-width: 0;
    }
    .size-group,
    .preset-group {
        min-width: 0;
    }
    .size-group select,
    .preset-group select {
        max-width: 135px;
        padding-inline: 6px;
    }
    h1 {
        font-size: 17px;
    }
    .board-wrapper {
        padding: 2px 0;
    }
    .col-clue, .row-clue {
        font-size: 12px;
        min-width: 0;
        min-height: 0;
    }
    .clue-target {
        font-size: 13px;
    }
    .toolbar-section {
        padding: 8px 6px;
        justify-content: center;
    }
    .tool-group,
    .action-group {
        flex-wrap: wrap;
        justify-content: center;
    }
    .calendar-card {
        padding: 14px 8px;
    }
    .calendar-months {
        gap: 4px;
    }
    .calendar-month {
        padding: 6px 3px;
    }
    .calendar-month h3 {
        font-size: 11px;
    }
    .calendar-weekdays,
    .calendar-days {
        gap: 1px;
    }
    button.calendar-day {
        font-size: 9px;
        border-radius: 3px;
    }
    button.calendar-day.completed::after {
        display: none;
    }
}
