:root {
    --bg: #0a0a0a;
    --fg-dim: #444;
    --fg: #e6e6e6;
    --accent: #ffd166;
    --highlight-bg: rgba(255, 209, 102, 0.1);
}

* {
    box-sizing: border-box;
}
html,
body {
    height: 100%;
}
/* Use dynamic viewport height on modern iOS to avoid showing browser UI */
#app {
    height: 100%;
}
@supports (height: 100dvh) {
    #app {
        height: 100dvh;
    }
}
@supports (-webkit-touch-callout: none) {
    /* Older iOS Safari fallback */
    html,
    body {
        height: -webkit-fill-available;
    }
    #app {
        height: -webkit-fill-available;
    }
}
body {
    margin: 0;
    background: var(--bg);
    color: var(--fg);
    font-family:
        ui-sans-serif,
        system-ui,
        -apple-system,
        Segoe UI,
        Roboto,
        "Helvetica Neue",
        Arial,
        "Noto Sans",
        "Apple Color Emoji",
        "Segoe UI Emoji";
    overflow: hidden; /* hide page scroll */
    overscroll-behavior: none; /* prevent rubber-banding that can reveal bars */
}

#playpause-btn {
    position: fixed;
    right: calc(env(safe-area-inset-right, 0px) + 12px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
    z-index: 20;
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
    backdrop-filter: blur(4px);
    cursor: pointer;
}

#playpause-btn:hover {
    background: rgba(255, 255, 255, 0.14);
}

#reload-btn {
    position: fixed;
    left: calc(env(safe-area-inset-left, 0px) + 12px);
    top: calc(env(safe-area-inset-top, 0px) + 12px);
    z-index: 25;
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
    backdrop-filter: blur(4px);
    cursor: pointer;
}

#reload-btn:hover {
    background: rgba(255, 255, 255, 0.14);
}


#ribbon {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    overflow: hidden; /* our horizontal scroller lives inside */
}

#track {
    position: absolute;
    will-change: transform;
    white-space: nowrap;
    user-select: none;
    pointer-events: none;
    filter: blur(0.3px);
}

.phrase {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0 1.25rem;
    color: var(--fg-dim);
    letter-spacing: 0.02em;
    opacity: 0.55; /* nearly unreadable while fast */
    font-size: clamp(32px, 7vh, 64px);
    line-height: 1.15;
    transform-origin: center center;
    transition: transform 180ms ease;
}

.phrase .line {
    display: block;
}

.phrase .uk {
    font-weight: 600;
    font-size: 33%;
}
.phrase .en {
    opacity: 0.9;
}

.sep {
    opacity: 0.35;
    margin: 0 0.75rem;
}

.highlight {
    color: var(--card-fg, var(--fg));
    opacity: 1;
    text-shadow: 0 0 8px rgba(255, 255, 255, 0.15);
    background: var(--card-bg, var(--highlight-bg));
    border-radius: 6px;
    padding: 0.2rem 0.45rem;
    position: relative; /* enable z-index and ::before */
    z-index: 5;
    /* Blur what's behind the card area */
    -webkit-backdrop-filter: blur(8px) saturate(0.9);
    backdrop-filter: blur(8px) saturate(0.9);
}

.highlight::before {
    content: "";
    position: absolute;
    inset: -0.35rem -0.75rem; /* enlarge the glass area */
    background: var(--card-bg, var(--highlight-bg));
    border-radius: 10px;
    -webkit-backdrop-filter: blur(8px) saturate(0.9);
    backdrop-filter: blur(8px) saturate(0.9);
    z-index: -1; /* place underneath the text */
    pointer-events: none;
}

.phrase.zoomed {
    transform: scale(var(--zoom-scale, 1.28));
}

#center-line {
    position: absolute;
    left: 50%;
    top: 0;
    bottom: 0;
    width: 1px;
    background: rgba(255, 255, 255, 0.06);
    transform: translateX(-0.5px);
}

/* History button (above collection button) */
#history-secret {
    position: fixed;
    left: calc(env(safe-area-inset-left, 0px) + 12px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 60px);
    z-index: 22;
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
    backdrop-filter: blur(4px);
    cursor: pointer;
}

#history-secret:hover {
    background: rgba(255, 255, 255, 0.14);
}

/* --- History overlay (shown on device shake) --- */
#history-overlay {
    position: fixed;
    inset: 0;
    display: none; /* toggled to flex when active */
    align-items: center;
    justify-content: center;
    z-index: 50;
    background: rgba(0, 0, 0, 0.75);
    -webkit-backdrop-filter: blur(8px) saturate(0.9);
    backdrop-filter: blur(8px) saturate(0.9);
    /* Keep overlay content within safe areas */
    padding: calc(env(safe-area-inset-top, 0px)) calc(env(safe-area-inset-right, 0px)) calc(env(safe-area-inset-bottom, 0px))
        calc(env(safe-area-inset-left, 0px));
}

#history-overlay.active {
    display: flex;
}

.history-panel {
    width: min(92vw, 1000px);
    height: min(86vh, 720px);
    background: rgba(20, 20, 20, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* On touch/mobile screens, make the panel fill the screen safely */
@media (pointer: coarse) {
    .history-panel {
        width: calc(100vw - (env(safe-area-inset-left, 0px) + env(safe-area-inset-right, 0px)));
        height: calc(100dvh - (env(safe-area-inset-top, 0px) + env(safe-area-inset-bottom, 0px)));
        max-width: 100vw;
        max-height: 100dvh;
        border-radius: 0;
        border-left: 0;
        border-right: 0;
    }
}

/* optional header removed to keep overlay minimal by default */

.history-body {
    flex: 1;
    overflow: auto;
}

#history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: clamp(14px, 1.9vh, 16px);
}

#history-table thead th {
    position: sticky;
    top: 0;
    background: rgba(255, 255, 255, 0.06);
    color: var(--fg);
    text-align: left;
    padding: 8px 10px;
    z-index: 1;
}

#history-table tbody td {
    padding: 8px 10px;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    word-break: break-word;
}

#history-table tbody td:last-child {
    width: 40px;
    text-align: center;
    padding: 4px;
}

.history-add-btn {
    appearance: none;
    background: transparent;
    border: 1px solid currentColor;
    color: inherit;
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 14px;
    cursor: pointer;
    transition: background 150ms ease, transform 100ms ease;
}

.history-add-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.history-add-btn:active {
    transform: scale(0.95);
}

#history-close {
    appearance: none;
    border: 0;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    padding: 12px 14px;
    font-size: 14px;
    cursor: pointer;
}

#history-close:hover {
    background: rgba(255, 255, 255, 0.14);
}

/* --- Desk feature: save button on cards --- */
.save-word-btn {
    position: absolute;
    right: -0.75rem;
    bottom: -0.35rem;
    width: 1.25rem;
    height: 1.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    color: inherit;
    opacity: 0.25;
    cursor: pointer;
    pointer-events: auto;
    z-index: 10;
    background: transparent;
    border: none;
    padding: 0;
    transition: opacity 150ms ease, transform 100ms ease;
}

.save-word-btn:hover {
    opacity: 1;
    transform: scale(1.1);
}

.save-word-btn:active {
    transform: scale(0.95);
}

/* Desk button - left bottom of main screen */
#desk-btn {
    position: fixed;
    left: calc(env(safe-area-inset-left, 0px) + 12px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
    z-index: 22; /* above history-secret */
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
    backdrop-filter: blur(4px);
    cursor: pointer;
}

#desk-btn:hover {
    background: rgba(255, 255, 255, 0.14);
}

/* --- Desk overlay (saved words screen) --- */
#desk-overlay {
    position: fixed;
    inset: 0;
    display: none;
    z-index: 60;
    background: rgba(10, 10, 10, 0.95);
    padding: calc(env(safe-area-inset-top, 0px)) calc(env(safe-area-inset-right, 0px)) calc(env(safe-area-inset-bottom, 0px))
        calc(env(safe-area-inset-left, 0px));
    overflow: hidden;
}

#desk-overlay.active {
    display: block;
}

#desk-canvas {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.desk-zone {
    position: relative;
    width: 100%;
    overflow: hidden;
}

#desk-upper-zone {
    flex: 2;
    touch-action: none;
    background-image:
        linear-gradient(rgba(255, 255, 255, 0.07) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 255, 255, 0.07) 1px, transparent 1px);
    background-size: 36px 36px;
}

#desk-divider {
    height: 1px;
    background: rgba(255, 255, 255, 0.15);
    flex-shrink: 0;
}

#desk-lower-zone {
    flex: 1;
    padding: 16px;
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    gap: 12px;
    overflow-y: auto;
}

.desk-card {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0.35rem 0.5rem;
    border-radius: 6px;
    font-size: clamp(14px, 3vh, 24px);
    line-height: 1.2;
    cursor: grab;
    user-select: none;
    touch-action: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    transition: transform 100ms ease, box-shadow 100ms ease;
    position: relative;
}

.desk-card.in-upper-zone {
    position: absolute;
}

.desk-card.in-lower-zone {
    position: relative;
}

.desk-card.dragging {
    cursor: grabbing;
    transform: scale(1.05);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
    z-index: 100;
}

.desk-card .en {
    font-weight: 600;
}

.desk-card .wavelength {
    font-size: 0.4em;
    font-weight: 200;
    font-variant-numeric: tabular-nums;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    padding: 0.05em 0.3em;
    border-radius: 3px;
    background: rgba(0, 0, 0, 0.2);
    letter-spacing: 0.03em;
    position: absolute;
    right: 0.3em;
    bottom: 0.2em;
    z-index: 1;
}

/* Collection control buttons */
#desk-clear,
#desk-back,
#desk-grid,
#desk-help {
    position: fixed;
    z-index: 70;
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
    backdrop-filter: blur(4px);
    cursor: pointer;
}

#desk-clear {
    left: calc(env(safe-area-inset-left, 0px) + 12px);
    top: calc(env(safe-area-inset-top, 0px) + 12px);
}

#desk-back {
    left: calc(env(safe-area-inset-left, 0px) + 12px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
}

#desk-grid {
    right: calc(env(safe-area-inset-right, 0px) + 12px);
    top: calc(env(safe-area-inset-top, 0px) + 12px);
    min-width: 36px;
    text-align: center;
}

#desk-help {
    right: calc(env(safe-area-inset-right, 0px) + 12px);
    top: calc(env(safe-area-inset-top, 0px) + 52px);
    min-width: 36px;
    text-align: center;
}

#desk-clear:hover,
#desk-back:hover,
#desk-grid:hover,
#desk-help:hover,
#desk-3d:hover {
    background: rgba(255, 255, 255, 0.14);
}

/* 3D View button */
#desk-3d {
    position: fixed;
    right: calc(env(safe-area-inset-right, 0px) + 12px);
    top: calc(env(safe-area-inset-top, 0px) + 52px);
    z-index: 70;
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.05em;
    backdrop-filter: blur(4px);
    cursor: pointer;
    min-width: 36px;
    text-align: center;
}

/* 3D View overlay */
#desk-3d-overlay {
    position: fixed;
    inset: 0;
    display: none;
    z-index: 80;
    background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 50%, #0a0a0a 100%);
    padding: calc(env(safe-area-inset-top, 0px)) calc(env(safe-area-inset-right, 0px)) calc(env(safe-area-inset-bottom, 0px))
        calc(env(safe-area-inset-left, 0px));
    overflow: hidden;
}

#desk-3d-overlay.active {
    display: block;
}

#desk-3d-scene {
    position: relative;
    width: 100%;
    height: 100%;
    perspective: 1200px;
    perspective-origin: 50% 50%;
    overflow: hidden;
}

#desk-3d-world {
    position: absolute;
    width: 100%;
    height: 100%;
    transform-style: preserve-3d;
    /* "Looking down at a table" view */
    transform: rotateX(35deg) scale(0.85);
    transition: transform 0.5s ease;
}

/* 3D Card */
.desk-card-3d {
    position: absolute;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0.25rem 0.4rem;
    border-radius: 4px;
    font-size: clamp(7px, 1.25vh, 11px);
    line-height: 1.2;
    cursor: default;
    user-select: none;
    box-shadow: 
        0 4px 12px rgba(0, 0, 0, 0.4),
        0 0 0 1px rgba(255, 255, 255, 0.1) inset;
    transform-style: preserve-3d;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    backface-visibility: hidden;
}

.desk-card-3d:hover {
    transform: translateZ(calc(var(--card-z, 0) * 1px + 20px)) scale(1.05);
    box-shadow: 
        0 8px 24px rgba(0, 0, 0, 0.5),
        0 0 0 1px rgba(255, 255, 255, 0.2) inset;
}

.desk-card-3d .en {
    font-weight: 600;
}

.desk-card-3d .wavelength {
    font-size: 0.4em;
    font-weight: 200;
    font-variant-numeric: tabular-nums;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    padding: 0.05em 0.3em;
    border-radius: 3px;
    background: rgba(0, 0, 0, 0.25);
    letter-spacing: 0.03em;
    position: absolute;
    right: 0.3em;
    bottom: 0.2em;
}

/* 3D Card depth indicator (side) */
.desk-card-3d::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 8px;
    background: linear-gradient(to right, rgba(0,0,0,0.3), transparent);
    border-radius: 8px 0 0 8px;
    transform: translateX(-100%) rotateY(-90deg);
    transform-origin: right center;
}

/* 3D View back button */
#desk-3d-back {
    position: fixed;
    left: calc(env(safe-area-inset-left, 0px) + 12px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
    z-index: 90;
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
    backdrop-filter: blur(4px);
    cursor: pointer;
}

#desk-3d-back:hover {
    background: rgba(255, 255, 255, 0.14);
}

/* 3D View controls */
#desk-3d-controls {
    position: fixed;
    right: calc(env(safe-area-inset-right, 0px) + 12px);
    top: calc(env(safe-area-inset-top, 0px) + 52px);
    z-index: 90;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#desk-3d-controls button {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.08);
    color: var(--fg);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    font-size: 18px;
    font-weight: 500;
    backdrop-filter: blur(4px);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 150ms ease, transform 100ms ease;
}

#desk-3d-controls button:hover {
    background: rgba(255, 255, 255, 0.14);
}

#desk-3d-controls button:active {
    transform: scale(0.95);
}

/* 3D View info */
#desk-3d-info {
    position: fixed;
    left: calc(env(safe-area-inset-left, 0px) + 12px);
    top: calc(env(safe-area-inset-top, 0px) + 52px);
    z-index: 90;
    background: rgba(0, 0, 0, 0.5);
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 11px;
    color: var(--fg);
    opacity: 0.7;
    font-variant-numeric: tabular-nums;
}

/* Draggable cursor for 3D scene */
#desk-3d-scene {
    cursor: grab;
}

#desk-3d-scene.dragging {
    cursor: grabbing;
}

/* Wavelength legend */
#desk-3d-legend {
    position: fixed;
    right: calc(env(safe-area-inset-right, 0px) + 12px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
    z-index: 90;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    background: rgba(0, 0, 0, 0.5);
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

#desk-3d-legend span {
    font-size: 11px;
    color: var(--fg);
    opacity: 0.7;
    font-variant-numeric: tabular-nums;
}

#desk-3d-legend .gradient-bar {
    width: 12px;
    height: 120px;
    background: linear-gradient(to top,
        rgb(148, 0, 211) 0%,    /* violet ~380nm */
        rgb(75, 0, 130) 15%,   /* indigo */
        rgb(0, 0, 255) 30%,    /* blue */
        rgb(0, 255, 255) 45%,  /* cyan */
        rgb(0, 255, 0) 55%,    /* green */
        rgb(255, 255, 0) 70%,  /* yellow */
        rgb(255, 127, 0) 85%,  /* orange */
        rgb(255, 0, 0) 100%    /* red ~700nm */
    );
    border-radius: 2px;
    opacity: 0.8;
}

/* ===================== Portrait mode ===================== */
@media (orientation: portrait) {
    /* Vertical tape: track flows top-to-bottom */
    #ribbon {
        align-items: stretch;
        justify-content: center;
    }

    #track {
        white-space: normal;
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .phrase {
        font-size: clamp(18px, 4vw, 28px);
        margin: 0.6rem 0;
    }

    .sep {
        margin: 0.15rem 0;
    }

    /* Center-line becomes horizontal in portrait */
    #center-line {
        width: 100%;
        height: 1px;
        left: 0;
        top: 50%;
        bottom: auto;
        transform: translateY(-0.5px);
    }

    /* Desk overlay: taller upper zone */
    #desk-upper-zone {
        flex: 3;
    }

    #desk-lower-zone {
        flex: 1;
        max-height: 25vh;
    }

    .desk-card {
        font-size: clamp(12px, 3vw, 20px);
    }

    /* History: smaller font for portrait */
    .history-body {
        font-size: clamp(12px, 1.5vh, 14px);
    }

    /* 3D view: adjusted perspective for portrait */
    #desk-3d-world {
        transform: rotateX(25deg) scale(0.7);
    }

    .desk-card-3d {
        font-size: clamp(6px, 1.2vw, 10px);
    }
}
