/* =========================================
   1. GLOBAL VARIABLES
   ========================================= */
:root {
    --board-bg: #111;
    --card-bg: #222;
    --text-color: #f5f5f5;
    --sub-text-color: #ccc;
    --flap-speed: 0.15s;
    --char-width: 32px;
    --char-height: 56px;
    --char-font: 42px;
    --word-height: 56px;
    --local-font: 30px;
    --en-font: 9px;
    --col-gap: 10px;
}

/* =========================================
   2. BASIC LAYOUT (Standard Block)
   ========================================= */
body {
    background-color: var(--board-bg);
    color: var(--text-color);
    font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Helvetica Neue", "Yu Gothic", sans-serif;
    display: block;
    min-height: 100vh;
    margin: 0;
    padding: calc(20px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
    box-sizing: border-box;
}

/* --- Main container: responsible for max width and centering --- */
.main-container {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 30px;
    position: relative;
    /* Stacking context for global lighting */
}

/* Global environmental lighting (Top-left source) */
.main-container::after {
    content: '';
    position: absolute;
    top: -20px;
    left: -20px;
    right: -20px;
    bottom: -20px;
    /* Soft glare across the entire assembly */
    background: linear-gradient(145deg, rgba(255, 255, 255, 0.06) 0%, rgba(255, 255, 255, 0) 50%, rgba(0, 0, 0, 0.05) 100%);
    pointer-events: none;
    z-index: 100;
    border-radius: 20px;
}

/* =========================================
   3. COMPONENTS
   ========================================= */
/* --- Top bar --- */
.top-bar {
    width: 100%;
    display: flex;
    align-items: center;
    /* Center items vertically */
    justify-content: center;
    margin-bottom: 20px;
    gap: 20px;
    height: 70px;
    /* Allocate sufficient space */
}

/* 3. Logo Placeholder */
.header-logo-section {
    width: 70px;
    height: 70px;
    flex-shrink: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.header-logo-section img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.header-info-section {
    display: flex;
    flex-direction: row;
    gap: 30px;
    align-items: center;
}

.header-line-name,
.header-direction {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.header-text-local {
    font-size: 28px;
    font-weight: 900;
    color: #ffffff;
    line-height: 1.1;
    margin-bottom: 2px;
}

.header-text-en {
    font-size: 14px;
    font-weight: 700;
    line-height: 1;
}

/* --- Flip board container --- */
.schedule-board {
    background: #0a0a0a;
    padding: 20px 15px;
    border-radius: 6px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.8);
    border: 6px solid #1c1c1c;
    width: 100%;
    box-sizing: border-box;
}

/* --- Header row --- */
.header-row {
    background: rgba(255, 255, 255, 0.03);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 4px 4px 0 0;
    margin-bottom: 15px;
    padding: 10px 12px;
    box-sizing: border-box;
    /* Alignment with cards: Matches train-group-padding (12px) */
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--col-gap);
    row-gap: 15px;
}

.row-primary {
    display: flex;
    align-items: center;
    gap: var(--col-gap);
    row-gap: 15px;
    /* Vertical gap when wrapping occurs */
    width: 100%;
    flex-wrap: wrap;
}

.header-item {
    display: flex;
    flex-direction: column;
    line-height: 1.2;
}

.header-item span:first-child {
    font-size: 15px;
    font-weight: 800;
    color: #fff;
    letter-spacing: 0.5px;
}

.header-item span:last-child {
    font-size: 11px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.6);
    margin-top: -2px;
}

.header-row .col-plat,
.header-row .col-type,
.header-row .col-no,
.header-row .col-time,
.header-row .col-dest,
.header-row .col-remarks,
.header-row .col-stop {
    align-items: flex-start;
}

.train-group {
    background: linear-gradient(to bottom, #1a1a1a 0%, #0a0a0a 100%);
    border: 1px solid #333;
    border-top-color: rgba(255, 255, 255, 0.1);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);

    margin-bottom: 12px;
    padding: 12px;
    border-radius: 4px;

    display: flex;
    flex-direction: column;
    gap: 8px;
}

.row-primary {
    display: flex;
    flex-wrap: wrap;
    /* allow flex items to wrap */
    align-items: center;
    gap: var(--col-gap);
    width: 100%;
}

/* =========================================
   4. COLUMN STRATEGY (Smart Flex)
   ========================================= */
[class^="col-"] {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    margin: 0;
}

/* 
   Fixed width columns (Track number, Train Number, Time) 
*/
.col-plat {
    flex: 0 0 auto;
    justify-content: center;
    width: calc((var(--char-width) + 2px) * 3 - 2px);
}

/* minimal 5 digit width */
.col-no {
    flex: 0 0 auto;
    width: calc((var(--char-width) + 2px) * 5 - 2px);
}

.col-time {
    flex: 0 0 auto;
    width: calc((var(--char-width) + 2px) * 5 - 2px);
}

/* 
   Variable width columns (Type, Dest, Remarks) 
*/
.col-type {
    flex: 1 1 var(--col-type-width, 130px);
}

.col-dest {
    flex: 2 1 var(--col-dest-width, 200px);
}

.col-remarks {
    flex: 2 1 var(--col-rem-width, 260px);
}

/* Train Stops column uses Flexbox to wrap */
.col-stop {
    flex: 1 1 100%;
    width: 100%;
}

/* --- Display Mode Specific Styles --- */
/* Concourse Mode: Hide 'col-stop' columns */
body.mode-concourse .header-row .col-stop.header-item,
body.mode-concourse .train-group .col-stop {
    display: none;
}

/* Gate Mode: Hide 'top-bar' and 'col-stop' columns */
body.mode-gate .top-bar,
body.mode-gate .header-row .col-stop.header-item,
body.mode-gate .train-group .col-stop {
    display: none;
}

/* Platform Mode: Hide 'col-plat' columns */
body.mode-platform .header-row .col-plat.header-item,
body.mode-platform .train-group .col-plat {
    display: none;
}

/* =========================================
   5. FLAP UNIT VISUALS (Standard)
   ========================================= */
.flap-unit {
    position: relative;
    display: inline-block;
    background: #000;
    perspective: 1000px;
    border-radius: 2px;
}

/* Retracted/Inward effect: An overlay shadow that simulates the card being "inside" */
.flap-unit::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    border-radius: 2px;
    /* Symmetrical bezel shadow for even lighting */
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.6), inset 0 -1px 2px rgba(255, 255, 255, 0.05);
    pointer-events: none;
}

.flap-char {
    width: var(--char-width);
    height: var(--char-height);
    margin-right: 2px;
}

.flap-unit:last-child {
    margin-right: 0;
}

.flap-word {
    width: 100%;
    height: var(--word-height);
}

.top,
.bottom,
.flap {
    position: absolute;
    left: 0;
    width: 100%;
    height: 50%;
    overflow: hidden;
    background-color: var(--card-bg);
    color: var(--text-color);
    box-sizing: border-box;
    backface-visibility: hidden;
}

/* Lighting and Glare (Matt Design) */
.top::after,
.flap.front::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Even, soft lighting instead of top-left glare */
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.04) 0%, rgba(255, 255, 255, 0.01) 100%);
    pointer-events: none;
}

.bottom::after,
.flap.back::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* Consistent subtle lighting for bottom half */
    background: linear-gradient(to bottom, rgba(255, 255, 255, 0.01) 0%, rgba(0, 0, 0, 0.03) 100%);
    pointer-events: none;
}

/* Content container */
.flap-unit .card-content {
    position: absolute;
    left: 0;
    width: 100%;
    height: 200%;
    display: flex;
    flex-direction: column;
}

/* --- Visual Compensation --- */
/* Prevent characters from looking shorter, because of the center striked line separator*/
.top .card-content,
.flap.front .card-content {
    top: 1px;
}

.bottom .card-content,
.flap.back .card-content {
    top: calc(-100% - 1px);
}

/* --- Pure alphanumeric columns --- */
.flap-char .card-content {
    justify-content: center;
    align-items: center;
    font-size: var(--char-font);
    font-family: "Helvetica Neue",
        Helvetica,
        Arial,
        sans-serif;
}

.local-text {
    height: 72%;
    width: 100%;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    font-size: var(--local-font);
    font-weight: 800;
    padding-bottom: 0px;
    line-height: 1;
    white-space: nowrap;
    overflow: hidden;
}

.en-text {
    height: 28%;
    width: 100%;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    font-size: var(--en-font);
    color: rgba(255, 255, 255, 0.7);
    font-weight: 600;
    padding-top: 3px;
    line-height: 1;
    white-space: nowrap;
    overflow: hidden;
}

.col-stop .local-text,
.col-stop .en-text,
.col-remarks .local-text,
.col-remarks .en-text,
.col-dest .local-text,
.col-dest .en-text {
    justify-content: flex-start;
    padding-left: 15px;
}

/* Structural style */
.top,
.flap.front {
    top: 0;
    border-radius: 2px 2px 0 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.3);
    z-index: 1;
    transform-origin: bottom;
}

.bottom,
.flap.back {
    top: 50%;
    border-radius: 0 0 2px 2px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 0;
    transform-origin: top;
}

.flap.front {
    z-index: 2;
    will-change: transform, filter;
}

.flap.back {
    z-index: 3;
    transform: rotateX(180deg);
    will-change: transform, filter;
}

/* Animation */
.flipping .flap.front {
    animation: flip-down-front var(--flap-speed) ease-in forwards;
}

.flipping .flap.back {
    animation: flip-down-back var(--flap-speed) ease-out forwards;
}

.flipping .bottom::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(to bottom, #000, transparent);
    opacity: 0;
    animation: cast-shadow var(--flap-speed) linear;
    /* The speed here will automatically follow the variable */
    /* Make sure the z-index is high enough to cover the text */
    z-index: 20;
    pointer-events: none;
}

/* --- Physical Thickness Simulation --- */
/* 
   When flipping, a "thickness layer" is observable in the center of the container.
   It simulates the bottom edge of the card facing the user when it is flipped to a 90-degree horizontal position.
*/
.flap-unit.flipping::after {
    content: '';
    position: absolute;
    top: 50%;
    left: -3px;
    /* Visibly wider than the card: overflows 3px to the left */
    width: calc(100% + 6px);
    /* Visibly wider than the card: total width + 6px */
    height: 0;
    /* Initial height is 0 */

    background-color: #373737;
    /* With gray reflection, physical edge of the card */
    border-radius: 2px;
    /* The edges are slightly rounded, like plastic parts */

    /* Centered positioning */
    transform: translateY(-50%);
    z-index: 50;
    /* Must be the highest, covering all text and light and shadow */

    /* Animation binding: follows the page turning speed */
    animation: thickness-pulse var(--flap-speed) ease-in-out;
}

/* Thickness pulse animation */
@keyframes thickness-pulse {
    0% {
        height: 0px;
        opacity: 0;
    }

    30% {
        opacity: 1;
        height: 1px;
        /* Appears in advance */
    }

    50% {
        height: 2px;
        /* Physical thickness at 90 degrees */
    }

    70% {
        height: 1px;
        opacity: 1;
    }

    100% {
        height: 0px;
        opacity: 0;
    }
}

/* 
   Ambient light reflection effect 
   1. brightness(>1): Brighten, simulate a black card turning gray when exposed to light.
   2. contrast(<1): Reduce contrast, simulate the "blown-out" feeling of text and background blending together during reflection.
*/

/* Top half falling (Front) */
@keyframes flip-down-front {
    0% {
        transform: rotateX(0deg);
        filter: brightness(1) contrast(1);
    }

    25% {
        /* 
           Matt Highlight Moment
           The reflection is more diffused and less intense than a glossy surface.
        */
        filter: brightness(1.2);
    }

    50% {
        /* 90 degrees horizontal */
        filter: brightness(0.6) contrast(1);
    }

    100% {
        transform: rotateX(-180deg);
        /* Completely flipped down */
        filter: brightness(0.1) contrast(1);
    }
}

/* Bottom half catching */
@keyframes flip-down-back {
    0% {
        transform: rotateX(180deg);
        filter: brightness(0.1);
    }

    50% {
        filter: brightness(0.6);
    }

    75% {
        /* 
           [Matt Highlight Moment]
           Subtle diffused reflection as it catches the light.
        */
        filter: brightness(1.2);
    }

    100% {
        transform: rotateX(0deg);
        filter: brightness(1);
    }
}

@keyframes cast-shadow {
    0% {
        opacity: 0;
    }

    50% {
        opacity: 0.6;
    }

    100% {
        opacity: 0;
    }
}

.board-footer {
    margin-top: 25px;
    width: 100%;
    text-align: center;
    letter-spacing: 1px;
}

.board-footer a {
    color: #666;
    text-decoration: none;
    transition: color 0.2s;
}

.board-footer a:hover {
    color: #f39c12;
}

footer {
    margin-top: auto;
    padding-top: 50px;
    color: #555;
    font-size: 0.9rem;
    text-align: center;
}

footer a {
    color: #888;
    text-decoration: none;
    font-weight: bold;
}

footer a:hover {
    color: #f39c12;
}

.copyright {
    font-size: 10px;
    color: #333;
    font-weight: 400;
}

.copyright a {
    color: #444;
    text-decoration: none;
    border-bottom: 1px dotted #444;
    transition: color 0.2s;
}

.copyright a:hover {
    color: #f39c12;
    border-bottom-color: #f39c12;
}

/* =========================================
   6. MOBILE ZOOM (Simulate "Smaller Text")
   ========================================= */
@media screen and (max-width: 800px) {
    .main-container {
        overflow-x: hidden;
        padding: 10px 0;
        gap: 15px;
    }

    .top-bar {
        height: auto;
        justify-content: center;
        align-items: center;
        gap: 15px;
        margin-bottom: 5px;
    }

    .header-logo-section {
        width: 48px;
        height: 48px;
    }

    .header-info-section {
        flex-direction: column;
        align-items: center;
        gap: 8px;
    }

    .header-text-local {
        font-size: 20px;
    }

    .header-text-en {
        font-size: 11px;
    }

    .header-row {
        display: none;
    }

    .schedule-board {
        --scale-factor: 0.65;
        transform: scale(var(--scale-factor));
        transform-origin: top left;
        padding: 10px;
        border-width: 4px;
        width: calc(100% / var(--scale-factor));
        box-shadow: none;
    }
}

/* =========================================
   7. SYSTEM STATUS (Error Handling)
   ========================================= */
.system-status {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90%;
    max-width: 600px;
    z-index: 9999;

    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none;
}

/* Base style for the error text */
.status-text {
    width: 100%;
    text-align: center;
    padding: 20px 0;
    background: rgba(0, 0, 0, 0.85);
    border: 2px solid #FFBF00;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.9);
    border-radius: 8px;
    backdrop-filter: blur(4px);
}

/* Error State */
.status-error {
    color: #FFBF00;
    /* Amber */
    font-weight: 800;
    text-shadow: 0 0 15px rgba(255, 191, 0, 0.5);
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 25px 20px;
}

.status-main {
    font-size: 30px;
    letter-spacing: 2px;
}

.status-description {
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.5px;
    line-height: 1.6;
    opacity: 0.9;
    text-shadow: none;
}

.status-ok {
    color: #2ecc71;
    text-shadow: 0 0 5px rgba(46, 204, 113, 0.8);
    opacity: 0.5;
    font-size: 14px;
}

/* Blur effect for the board when data is stale/error */
.schedule-board.board-error #board-rows {
    filter: blur(4px) grayscale(0.5);
    pointer-events: none;
    transition: filter 0.5s ease, opacity 0.5s ease;
    opacity: 0.6;
}

/* Optional: Add a subtle overlay to the header when error occurs */
.schedule-board.board-error .header-row {
    opacity: 0.3;
    transition: opacity 0.5s ease;
}
