/* ============================================================
   Base: reset, paper shell, handwriting type, drawn controls, top bar
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { height: 100%; -webkit-text-size-adjust: 100%; }

body {
    min-height: 100%;
    font-family: var(--font-body);
    font-size: 18px;
    line-height: 1.3;
    color: var(--ink);
    background: var(--paper);
    -webkit-font-smoothing: antialiased;
    overflow-x: hidden;
    overscroll-behavior-y: none;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
button:disabled { cursor: not-allowed; }
input { -webkit-appearance: none; appearance: none; }
a { color: var(--ink-me); }
[hidden] { display: none !important; }

h1, h2, h3 { font-family: var(--font-hand); font-weight: 700; line-height: 1; }
h1 { font-size: 2.6rem; }
h2 { font-size: 1.7rem; }
h3 { font-size: 1.3rem; }

:focus-visible { outline: 2px dashed var(--ink-me); outline-offset: 3px; }

/* --- Paper ---------------------------------------------------------
   Ruled lines and a red margin on the shell; a faint grain over it. */
.app {
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    width: 100%;
    max-width: var(--content-max);
    margin: 0 auto;
    background-color: var(--paper);
    background-image:
        linear-gradient(90deg, transparent 44px, var(--margin-line) 44px, var(--margin-line) 46px, transparent 46px),
        repeating-linear-gradient(180deg, transparent 0 31px, var(--rule) 31px 32px);
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.08);
    overflow-x: clip;
}
.app::before {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0.35;
    mix-blend-mode: multiply;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.35 0 0 0 0 0.3 0 0 0 0 0.2 0 0 0 0.12 0'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)'/%3E%3C/svg%3E");
    z-index: 0;
}
.app > * { position: relative; z-index: 1; }
.app.screen-game { height: 100vh; height: 100dvh; overflow: hidden; }

.sheet-main { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.content { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.screen { display: none; }
.screen.active { display: block; }
#game-screen.active { display: flex; flex-direction: column; flex: 1; min-height: 0; }

/* --- Drawn controls ---------------------------------------------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: var(--tap);
    padding: 0 18px;
    border: 1.6px solid var(--ink);
    border-radius: var(--sketch-radius);
    background: var(--paper-2);
    box-shadow: var(--shadow-key);
    font-family: var(--font-body);
    font-size: 1.1rem;
    line-height: 1;
    white-space: nowrap;
    color: var(--ink);
    transition: transform var(--dur-fast) var(--ease-smooth), box-shadow var(--dur-fast) var(--ease-smooth), background var(--dur) var(--ease-smooth);
}
.btn:active { transform: translate(1.5px, 2px); box-shadow: none; }
.btn-block { width: 100%; }
.btn-primary { background: var(--sticky-yellow); }
.btn-secondary { background: var(--paper-2); }
.btn-accent { background: var(--sticky-blue); }
.btn-danger { color: var(--pen-red); border-color: var(--pen-red); box-shadow: 1.5px 2px 0 var(--pen-red); }
.btn-ghost { border-style: dashed; box-shadow: none; }
.btn:disabled { opacity: 0.45; box-shadow: none; }

.chip-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    padding: 0 14px;
    border: 1.6px solid var(--ink-me);
    border-radius: var(--sketch-oval);
    background: transparent;
    font-family: var(--font-hand);
    font-weight: 700;
    font-size: 1.2rem;
    color: var(--ink-me);
    white-space: nowrap;
    transition: transform var(--dur-fast) var(--ease-smooth), background var(--dur-fast) var(--ease-smooth);
}
.chip-btn:active { transform: scale(0.96); background: rgba(31, 58, 147, 0.08); }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    color: var(--ink);
    position: relative;
    transition: background var(--dur-fast) var(--ease-smooth), transform var(--dur-fast) var(--ease-smooth);
}
.icon-btn:active { transform: scale(0.9); }
.icon-btn svg { width: 22px; height: 22px; }

.text-input {
    width: 100%;
    min-height: var(--tap);
    padding: 0 6px;
    border: none;
    border-bottom: 2px solid var(--ink);
    background: transparent;
    color: var(--ink-me);
    font-family: var(--font-hand);
    font-size: 22px;
    font-weight: 500;
}
.text-input:focus { outline: none; border-bottom-color: var(--ink-me); }
.text-input::placeholder { color: var(--ink-3); font-weight: 500; }

.badge-dot {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--pen-red);
    display: none;
}
.badge-dot.active { display: block; animation: stickOn var(--dur-slow) var(--ease-spring); }

.section-title {
    display: block;
    font-family: var(--font-body);
    font-size: 1.15rem;
    color: var(--ink-2);
    margin-bottom: 8px;
}

/* A fresh sheet stuck onto the page */
.card, .note {
    position: relative;
    background: var(--paper-2);
    box-shadow: var(--shadow-note);
    padding: 16px 18px;
}
.tape {
    position: absolute;
    top: -10px;
    left: 50%;
    width: 74px;
    height: 22px;
    margin-left: -37px;
    background: rgba(255, 255, 255, 0.55);
    border: 1px solid rgba(0, 0, 0, 0.08);
    transform: rotate(-3deg);
    pointer-events: none;
}

.sticky {
    position: relative;
    padding: 14px 16px 16px;
    box-shadow: var(--shadow-note);
    background: var(--sticky-yellow);
}
.sticky.pink { background: var(--sticky-pink); }
.sticky.blue { background: var(--sticky-blue); }
.sticky.green { background: var(--sticky-green); }

/* --- Top bar ------------------------------------------------------ */
.topbar {
    position: sticky;
    top: 0;
    z-index: 50;
    display: grid;
    grid-template-columns: 48px 1fr 88px;
    align-items: center;
    min-height: calc(54px + var(--safe-top));
    padding: var(--safe-top) 8px 0;
    background: linear-gradient(180deg, var(--paper) 70%, rgba(251, 246, 233, 0));
}
.topbar-left { display: flex; align-items: center; }
.topbar-right { display: flex; align-items: center; justify-content: flex-end; gap: 2px; }
.topbar-center { text-align: center; min-width: 0; }
.brand { font-size: 1.6rem; color: var(--ink-me); cursor: pointer; transform: rotate(-2deg); display: inline-block; }
.subtitle {
    margin-top: 0;
    font-size: 1rem;
    color: var(--ink-2);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.topbar-mode { display: none; }

/* Home: the top bar is the hero */
.app.screen-main-menu .topbar {
    position: relative;
    display: block;
    text-align: left;
    padding: calc(40px + var(--safe-top)) 24px 0 60px;
    background: transparent;
    z-index: auto; /* let the about drawer cover the hero */
}
.app.screen-main-menu .topbar-left,
.app.screen-main-menu .topbar-right { display: none; }
.app.screen-main-menu .topbar-center { text-align: left; }
.app.screen-main-menu .brand { font-size: 3.6rem; line-height: 0.9; cursor: default; }
.app.screen-main-menu .brand .header-bar-title { position: relative; display: inline-block; }
.app.screen-main-menu .brand .header-bar-title::after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    bottom: -8px;
    height: 12px;
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 220 14' preserveAspectRatio='none'%3E%3Cpath d='M3 9c40-8 70 6 110 0s70-8 104 2' fill='none' stroke='%231F3A93' stroke-width='2.5' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat center / 100% 100%;
    animation: drawUnderline 0.7s var(--ease-out-expo) 0.2s both;
}
.app.screen-main-menu .subtitle { font-size: 1.2rem; margin-top: 14px; white-space: normal; color: var(--ink-2); }
.intro { display: none; }
.app.screen-main-menu .intro { display: block; margin-top: 10px; font-size: 1rem; line-height: 1.4; color: var(--ink-3); max-width: 34ch; }

/* Lobby / game: compact */
.app.screen-game-room .topbar-right { visibility: hidden; }
.app.screen-game #close-game-room-btn { display: none; }
.app.screen-game .subtitle { display: none; }
.app.screen-matchmaking .topbar,
.app.screen-countdown .topbar { display: none; }

/* Menu popover: a scrap of paper */
.menu-anchor { position: relative; }
.popover {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    min-width: 200px;
    padding: 8px;
    background: var(--paper-2);
    box-shadow: var(--shadow-note-lg);
    transform: rotate(1.5deg) scale(0.96);
    transform-origin: top right;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--dur) var(--ease-out-expo), transform var(--dur) var(--ease-spring);
    z-index: 60;
}
.popover.active { opacity: 1; transform: rotate(1.5deg) scale(1); pointer-events: auto; }
.popover-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: var(--tap);
    padding: 0 10px;
    font-family: var(--font-hand);
    font-weight: 700;
    font-size: 1.3rem;
    text-align: left;
    color: var(--ink);
}
.popover-item:hover { background: var(--highlighter); }
.popover-item.danger { color: var(--pen-red); }
.popover-divider { height: 1px; margin: 4px 6px; background: var(--pencil); }

/* --- Shared juice ------------------------------------------------- */
@keyframes stickOn {
    0% { opacity: 0; transform: scale(1.12) rotate(var(--tilt, 0deg)); box-shadow: 8px 12px 20px rgba(0, 0, 0, 0.25); }
    60% { opacity: 1; transform: scale(0.98) rotate(var(--tilt, 0deg)); }
    100% { opacity: 1; transform: scale(1) rotate(var(--tilt, 0deg)); box-shadow: var(--shadow-note); }
}
@keyframes drawUnderline { from { clip-path: inset(0 100% 0 0); } to { clip-path: inset(0 0 0 0); } }
@keyframes fadeUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes wobble {
    0%, 100% { transform: rotate(0); }
    25% { transform: rotate(-2.5deg) translateX(-3px); }
    75% { transform: rotate(2.5deg) translateX(3px); }
}

@media (min-width: 600px) {
    :root { --gutter: 24px; --content-max: 520px; }
    .app.screen-main-menu .brand { font-size: 4.2rem; }
}

/* --- Desktop: an open spiral notebook -----------------------------
   From 1024px the app is a notebook lying open on a desk. The left page
   is the main sheet; the right page holds the code entry and about copy
   at home, and the margin-notes chat plus the drawn keys in a game.
   ui/layout.js moves the join row and the keyboard between pages. */
@media (min-width: 1024px) {
    :root { --desk: #6B563F; --desk-2: #55432F; --page-h: min(780px, calc(100vh - 72px)); }
    body {
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 36px 0;
        background: radial-gradient(ellipse at 30% 20%, var(--desk) 0%, var(--desk-2) 70%);
    }
    .app {
        display: grid;
        grid-template-columns: minmax(0, 1fr) 36px minmax(0, 1fr);
        width: min(1180px, calc(100vw - 72px));
        max-width: none;
        height: var(--page-h);
        min-height: 0;
        margin: 0;
        border-radius: 6px;
        overflow: hidden;
        background-color: var(--paper);
        background-image: none;
        box-shadow: 8px 14px 34px rgba(0, 0, 0, 0.4);
        transform: rotate(-0.6deg);
    }
    .app.screen-game { height: var(--page-h); }
    /* the spiral binding */
    .app::after {
        content: '';
        grid-column: 2;
        grid-row: 1;
        position: relative;
        z-index: 2;
        background:
            repeating-linear-gradient(180deg, transparent 0 14px, var(--ink) 14px 18px, transparent 18px 30px) 0 0 / 100% 100%,
            linear-gradient(90deg, transparent 12px, var(--paper) 12px, var(--paper) 24px, transparent 24px);
        opacity: 0.55;
        box-shadow: inset 6px 0 10px -8px rgba(0, 0, 0, 0.35), inset -6px 0 10px -8px rgba(0, 0, 0, 0.35);
    }
    .sheet-main {
        grid-column: 1;
        grid-row: 1;
        min-height: 0;
        height: 100%;
        overflow-y: auto;
        background-image:
            linear-gradient(90deg, transparent 44px, var(--margin-line) 44px, var(--margin-line) 46px, transparent 46px),
            repeating-linear-gradient(180deg, transparent 0 31px, var(--rule) 31px 32px);
    }
    .app.screen-game .sheet-main { overflow: hidden; }
    .topbar { position: sticky; top: 0; }
    .app.screen-main-menu .topbar { padding-top: 44px; }
    /* right page */
    .page-right {
        grid-column: 3;
        grid-row: 1;
        min-height: 0;
        height: 100%;
        overflow-y: auto;
        background-image: repeating-linear-gradient(180deg, transparent 0 31px, var(--rule) 31px 32px);
    }
    /* matchmaking and countdown use the whole spread */
    .app.screen-matchmaking .sheet-main,
    .app.screen-countdown .sheet-main { grid-column: 1 / -1; background-image: repeating-linear-gradient(180deg, transparent 0 31px, var(--rule) 31px 32px); }
    .app.screen-matchmaking::after,
    .app.screen-countdown::after { display: none; }
}
