/**
 * Gattai CSS - Multi-Grid Sudoku Styles
 * Extends base style.css
 */

/* ===== Gattai-Specific Variables ===== */
:root {
    /* Grid spacing - used in container size calculations */
    --grid-gap: 0px;
    
    /* Grid line colors - light mode */
    --gattai-outer-border: rgba(0, 0, 0, 0.4);
    --gattai-inner-line: rgba(0, 0, 0, 0.08);
    --gattai-box-line: rgba(0, 0, 0, 0.2);
    
    /* 
     * Cell sizing - CRITICAL for mobile rendering
     * These are calculated based on viewport and logical grid size.
     * Default values assume Samurai mode (21x21 logical grid).
     * JavaScript sets --logical-cols and --logical-rows per mode.
     */
    --gattai-padding: 16px;
    --gattai-max-cell: 32px;
    --gattai-controls-gap: var(--space-sm);
    /* Cell size = min(max cell size, (viewport - padding) / logical columns) */
    --gattai-cell-size: min(var(--gattai-max-cell), calc((100vw - var(--gattai-padding) * 2) / var(--logical-cols, 21)));
}

/* Dark mode - inverted for contrast */
@media (prefers-color-scheme: dark) {
    :root {
        --gattai-outer-border: rgba(255, 255, 255, 0.5);
        --gattai-inner-line: rgba(255, 255, 255, 0.1);
        --gattai-box-line: rgba(255, 255, 255, 0.25);
    }
}

/* ===== Gattai App Container ===== */
.gattai-app {
    max-width: 100%;
}

.gattai-controls {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--gattai-controls-gap);
}

.gattai-app header h1 {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.logo-link {
    text-decoration: none;
    color: inherit;
}

.gattai-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.5em;
    font-weight: 600;
    padding: 6px 12px;
    background: var(--accent-soft);
    color: var(--accent);
    border-radius: var(--radius-full);
    vertical-align: middle;
    line-height: 1;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

/* ===== Mode Picker ===== */
.mode-picker {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 0;
    width: 100%;
    max-width: min(580px, 95vmin);
}

.mode-tabs {
    display: flex;
    background: var(--bg-elevated);
    padding: 4px;
    border-radius: var(--radius-full);
    gap: 2px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.mode-tabs::-webkit-scrollbar {
    display: none;
}

.mode-tab {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 14px;
    min-width: 64px;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-secondary);
    border-radius: var(--radius-full);
    white-space: nowrap;
    transition: all 0.2s var(--ease);
}

.mode-icon {
    font-size: 1.1em;
    line-height: 1;
}

/* Twin mode CSS icons - two boxes with smaller one outside the corner */
.twin-icon {
    position: relative;
    width: 14px;
    height: 14px;
    display: inline-block;
}

/* Large box */
.twin-icon::before {
    content: '';
    position: absolute;
    width: 10px;
    height: 10px;
    border: 1.5px solid currentColor;
    border-radius: 1px;
}

/* Small box - positioned outside the corner */
.twin-icon::after {
    content: '';
    position: absolute;
    width: 6px;
    height: 6px;
    border: 1.5px solid currentColor;
    border-radius: 1px;
    background: currentColor;
    opacity: 0.4;
}

/* NW: small box top-left outside */
.twin-icon.nw::before {
    bottom: 0;
    right: 0;
}
.twin-icon.nw::after {
    top: 0;
    left: 0;
}

/* NE: small box top-right outside */
.twin-icon.ne::before {
    bottom: 0;
    left: 0;
}
.twin-icon.ne::after {
    top: 0;
    right: 0;
}

/* SW: small box bottom-left outside */
.twin-icon.sw::before {
    top: 0;
    right: 0;
}
.twin-icon.sw::after {
    bottom: 0;
    left: 0;
}

/* SE: small box bottom-right outside */
.twin-icon.se::before {
    top: 0;
    left: 0;
}
.twin-icon.se::after {
    bottom: 0;
    right: 0;
}

.mode-tab:hover {
    color: var(--text-primary);
    background: var(--bg-secondary);
}

.mode-tab.active {
    background: var(--accent);
    color: white;
    box-shadow: var(--shadow-sm);
}

.mode-icon {
    font-size: 1.2rem;
    line-height: 1;
}

.mode-label {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ===== Timer Container ===== */
.timer-container {
    display: flex;
    justify-content: center;
    margin-bottom: var(--space-md);
}

.timer {
    font-family: var(--font-mono);
    font-size: 1.25rem;
    font-weight: 500;
    color: var(--text-secondary);
    padding: var(--space-xs) var(--space-md);
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
}

/* ===== Gattai Main Container ===== */
.gattai-main {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: var(--space-sm);
    width: 100%;
    /* Allow scrolling if puzzle is larger than viewport (shouldn't happen with proper sizing) */
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

@media (min-width: 900px) {
    :root {
        --gattai-padding: 10px;
        --gattai-max-cell: 42px;
        --gattai-controls-gap: 6px;
    }

    .gattai-app {
        max-width: 1200px;
    }

    header h1 {
        font-size: 36px;
        letter-spacing: 3px;
    }

    .date-nav {
        margin-top: 0;
    }

    .gattai-controls {
        flex-direction: row;
        justify-content: center;
        align-items: center;
        gap: var(--space-md);
    }

    .mode-picker,
    .difficulty-picker {
        max-width: none;
        width: auto;
    }

    .mode-tabs,
    .difficulty-tabs {
        padding: 3px;
        gap: 1px;
    }

    .difficulty-tabs .tab {
        padding: 8px 16px 7px;
        min-width: 72px;
        font-size: 12px;
    }

    .subtitle {
        font-size: 10px;
    }
}

@media (min-width: 1200px) {
    :root {
        --gattai-max-cell: 48px;
        --gattai-padding: 8px;
    }
}

/* ===== Gattai Grid Container ===== */
.gattai-container {
    position: relative;
    display: block;
    /* Transparent background - only the grids themselves should have background */
    background: transparent;
    border-radius: var(--radius-md);
    touch-action: pan-x pan-y pinch-zoom;
    
    /* 
     * EXPLICIT PIXEL-BASED SIZING - Required for mobile compatibility
     * The container size is calculated from cell size × logical grid dimensions.
     * This avoids the aspect-ratio + container-query issues on mobile browsers.
     */
    width: calc(var(--gattai-cell-size) * var(--logical-cols, 21));
    height: calc(var(--gattai-cell-size) * var(--logical-rows, 21));
    
    /* Center the container */
    margin: 0 auto;
}

/* ===== Individual 9×9 Grid ===== */
.gattai-grid {
    position: absolute;
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    /* 
     * NO border on the grid itself - cells handle all borders.
     * This prevents double-borders and misalignment at overlaps.
     */
    overflow: visible;
    background: var(--bg-elevated);
    z-index: 1;

    /* 
     * PIXEL-BASED POSITIONING - Required for mobile compatibility
     * Grid position = grid's starting row/col × cell size
     */
    width: calc(var(--gattai-cell-size) * 9);
    height: calc(var(--gattai-cell-size) * 9);
    left: calc(var(--grid-col) * var(--gattai-cell-size));
    top: calc(var(--grid-row) * var(--gattai-cell-size));
}

/* Active grid elevation */
.gattai-grid.active {
    z-index: 10;
}

/* ===== Cell Styles ===== */
.gattai-grid .cell {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    font-family: var(--font-mono);
    
    /* Font size based on cell size - works on all browsers */
    font-size: calc(var(--gattai-cell-size) * 0.55);
    
    font-weight: 500;
    color: var(--cell-user);
    background: var(--cell-empty-bg);
    
    /* 
     * ALL cells have ALL borders - creates complete grid lines.
     * Using box-sizing ensures borders don't cause overflow.
     * Inner lines are subtle, outer/box lines are more visible.
     */
    box-sizing: border-box;
    border: 1px solid var(--gattai-inner-line);
    
    cursor: pointer;
    user-select: none;
    transition: background 0.15s var(--ease);
    position: relative;
    
    /* Ensure content doesn't overflow */
    overflow: hidden;
}

/* 3x3 Box separators - stronger borders at box boundaries */
.gattai-grid .cell[data-row="0"] {
    border-top-color: var(--gattai-box-line);
}
.gattai-grid .cell[data-row="3"],
.gattai-grid .cell[data-row="6"] {
    border-top-color: var(--gattai-box-line);
}
.gattai-grid .cell[data-col="0"] {
    border-left-color: var(--gattai-box-line);
}
.gattai-grid .cell[data-col="3"],
.gattai-grid .cell[data-col="6"] {
    border-left-color: var(--gattai-box-line);
}

/* Outer edges of each 9x9 grid - strong border */
.gattai-grid .cell[data-row="8"] {
    border-bottom-color: var(--gattai-box-line);
}
.gattai-grid .cell[data-col="8"] {
    border-right-color: var(--gattai-box-line);
}

/* Reserve space for pencil marks */
.gattai-grid .cell.has-pencil {
    padding: 2%;
}

.pencil-marks {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    width: 100%;
    height: 100%;
}

.pencil-mark {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--gattai-cell-size) * 0.25);
    font-weight: 400;
    color: var(--text-tertiary);
    line-height: 1;
}

/* Given cells */
.gattai-grid .cell.given {
    color: var(--cell-given);
    font-weight: 600;
}

/* Shared/overlap cells - NO special highlighting, just normal cells */
.gattai-grid .cell.shared {
    /* No special styling - overlap region looks like regular cells */
}

/* Placeholder for non-canonical overlap cells (completely transparent) */
.gattai-grid .cell-placeholder {
    /* Invisible placeholder to maintain CSS grid layout */
    visibility: hidden;
    background: transparent;
    border: none;
    pointer-events: none;
}

/* Selected cell */
.gattai-grid .cell.selected {
    background: var(--cell-selected);
    z-index: 5;
}

/* Related cells (same row/col/box) */
.gattai-grid .cell.related {
    background: var(--cell-related);
}

/* Conflict cells */
.gattai-grid .cell.conflict {
    background: var(--cell-conflict);
}

/* User-entered values */
.gattai-grid .cell.user {
    color: var(--cell-user);
}

/* ===== Responsive Adjustments ===== */

/* Mobile optimizations */
@media (max-width: 767px) {
    :root {
        /* Reduce padding on mobile to maximize puzzle size */
        --gattai-padding: 8px;
        --gattai-max-cell: 28px;
    }
    
    .mode-tabs {
        padding: 2px;
    }
    
    .mode-tab {
        padding: var(--space-xs) var(--space-sm);
        min-width: 50px;
    }
    
    .mode-icon {
        font-size: 1rem;
    }
    
    .mode-label {
        font-size: 0.55rem;
    }
    
    .gattai-main {
        padding: var(--space-xs);
    }
    
    .timer {
        font-size: 1rem;
    }
}

/* Very small screens */
@media (max-width: 400px) {
    :root {
        /* Minimal padding on very small screens */
        --gattai-padding: 4px;
        --gattai-max-cell: 24px;
    }
    
    .mode-label {
        display: none;
    }
    
    .mode-tab {
        min-width: 40px;
    }
}

/* ===== Touch Device Optimizations ===== */
@media (hover: none) and (pointer: coarse) {
    .gattai-grid .cell {
        /* 
         * REMOVED min-width/min-height - they caused overflow on small screens.
         * Cell size is now controlled entirely by --gattai-cell-size which
         * is calculated from viewport width, ensuring cells always fit.
         */
        touch-action: manipulation;
    }
    
    .gattai-grid .cell:active {
        transform: scale(0.95);
    }
}

/* ===== Print Styles ===== */
@media print {
    .mode-picker,
    .difficulty-picker,
    .toolbar,
    .numpad,
    .timer-container,
    .date-nav {
        display: none !important;
    }
    
    .gattai-container {
        box-shadow: none;
    }
    
    .gattai-grid {
        box-shadow: none;
        border: 2px solid black;
    }
    
    .gattai-grid .cell {
        background: white;
        border-color: #ccc;
    }
    
    /* 3x3 separators in print */
    .gattai-grid .cell[data-row="3"],
    .gattai-grid .cell[data-row="6"] {
        border-top-color: black;
    }
    
    .gattai-grid .cell[data-col="3"],
    .gattai-grid .cell[data-col="6"] {
        border-left-color: black;
    }
}

/* ===== Reduced Motion ===== */
@media (prefers-reduced-motion: reduce) {
    .gattai-grid,
    .gattai-grid .cell,
    .mode-tab {
        transition: none;
    }
}

/* ===== Focus Styles ===== */
.gattai-grid .cell:focus {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.gattai-grid .cell:focus:not(:focus-visible) {
    outline: none;
}

.mode-tab:focus-visible,
.tab:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ===== Check & Hint Animations ===== */
.cell.check-error {
    animation: shake 0.5s ease-in-out;
    background: var(--error) !important;
    color: white !important;
}

.cell.hint-reveal {
    animation: pulse-hint 0.8s ease-out;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-3px); }
    40%, 80% { transform: translateX(3px); }
}

@keyframes pulse-hint {
    0% { background: var(--success); transform: scale(1.1); }
    100% { background: transparent; transform: scale(1); }
}
/* ===== T048: Completion Badge & Next Challenge (Gattai Parity) ===== */

/* Completion badge styles */
.completion-badge {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md);
    margin: var(--space-md) auto;
    background: linear-gradient(135deg, rgba(34, 197, 94, 0.15), rgba(16, 185, 129, 0.1));
    border: 1px solid rgba(34, 197, 94, 0.3);
    border-radius: var(--radius-md);
    font-family: var(--font-display);
    font-weight: 600;
    color: var(--success);
    max-width: fit-content;
}

.completion-badge .badge-icon {
    font-size: 18px;
}

.completion-badge .badge-text {
    font-size: 14px;
}

/* Next challenge suggestion styles */
.next-challenge {
    display: flex;
    gap: var(--space-sm);
    justify-content: center;
    margin-top: var(--space-md);
    flex-wrap: wrap;
}

.btn-play-again,
.btn-next-difficulty {
    padding: 10px 20px;
    font-family: var(--font-display);
    font-size: 14px;
    font-weight: 600;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
}

.btn-play-again {
    background: var(--accent);
    color: white;
    box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
}

.btn-play-again:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(124, 58, 237, 0.4);
}

.btn-next-difficulty {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-next-difficulty:hover {
    background: var(--bg-tertiary);
    transform: translateY(-1px);
}

/* Rank badge and best times list styles */
.rank-badge {
    font-size: 18px;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: var(--space-sm);
    text-align: center;
}

.best-times-list {
    margin-top: var(--space-lg);
    margin-bottom: var(--space-lg);
    padding: var(--space-md) var(--space-lg);
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    min-width: 200px;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.best-times-title {
    font-size: 11px;
    font-weight: 700;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin-bottom: var(--space-md);
    padding-bottom: var(--space-sm);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    text-align: center;
}

.best-time-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 4px;
    font-family: var(--font-mono);
    font-size: 14px;
    border-radius: var(--radius-sm);
    transition: background 0.15s ease;
}

.best-time-row:not(:last-child) {
    margin-bottom: 4px;
}

.best-time-row.current {
    color: var(--accent);
    font-weight: 600;
    background: rgba(124, 58, 237, 0.1);
    padding-left: 8px;
    padding-right: 8px;
}

.best-time-rank {
    font-size: 16px;
}

.best-time-value {
    color: var(--text-secondary);
}

.best-time-row.current .best-time-value {
    color: var(--accent);
}

/* Completed grid styling for gattai */
#gattai-container.completed .sudoku-cell {
    opacity: 0.85;
    pointer-events: none;
}

#gattai-container.completed .sudoku-cell.user-filled {
    color: var(--success);
}

/* Dark mode adjustments */
@media (prefers-color-scheme: dark) {
    .completion-badge {
        background: linear-gradient(135deg, rgba(34, 197, 94, 0.2), rgba(16, 185, 129, 0.15));
        border-color: rgba(34, 197, 94, 0.4);
    }
    
    .btn-next-difficulty {
        background: rgba(255, 255, 255, 0.1);
        color: var(--text-primary);
        border-color: rgba(255, 255, 255, 0.15);
    }
    
    .btn-next-difficulty:hover {
        background: rgba(255, 255, 255, 0.15);
    }
    
    .best-times-list {
        background: rgba(255, 255, 255, 0.05);
    }
}