@font-face {
    font-family: 'Caveat';
    font-style: normal;
    font-weight: 400 700;
    font-display: swap;
    src: url('../../assets/fonts/caveat.ttf') format('truetype');
}
@font-face {
    font-family: 'Patrick Hand';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url('../../assets/fonts/patrick-hand.ttf') format('truetype');
}

/* CSS Variables - Dynamic Transition System */
:root {
    /* Page units are local to the 1280 × 720 notebook, never the browser chrome. */
    --page-width: 1280px;
    --page-height: 720px;
    --page-vw: 12.8px;
    --page-vmin: 7.2px;
    --viewport-width: 100vw;
    --viewport-height: 100vh;
    --viewport-left: 0px;
    --viewport-top: 0px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --background: 39 35% 90%;
    --foreground: 25 15% 20%;
    --card: 39 40% 95%;
    --primary: 25 20% 25%;
    --secondary: 35 25% 85%;
    --accent: 18 60% 55%;
    --destructive: 0 65% 50%;
    --muted-foreground: 25 10% 45%;
    --pencil: 25 15% 25%;
    --pencil-light: 25 10% 55%;

    /* Difficulty Colors */
    --diff-normal: 25 15% 25%;
    --diff-hard: 200 45% 45%;
    --diff-harder: 30 80% 50%;
    --diff-impossible: 0 65% 50%;
}

/* Resets & Base Styles */
* {
    box-sizing: border-box;
    user-select: none;
    margin: 0;
    padding: 0;
}

body,
html {
    width: 100%;
    height: 100%;
    background-color: hsl(var(--background));
    color: hsl(var(--foreground));
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    overflow: hidden;
    touch-action: none;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    transition: background-color 2s ease, color 2s ease;
}

/* Visual Elements & Realism */
.sketchbook-spine {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 40px;
    background: linear-gradient(to right, rgba(0, 0, 0, 0.3) 0%, rgba(0, 0, 0, 0.05) 80%, transparent 100%);
    border-right: 2px dashed hsl(var(--pencil-light) / 0.4);
    z-index: 10;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;
}

.ring-hole {
    width: 15px;
    height: 15px;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 50%;
    box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.8), 1px 1px 1px rgba(255, 255, 255, 0.2);
}

.paper-texture {
    position: absolute;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.08'/%3E%3C/svg%3E");
    box-shadow: inset 0 0 150px rgba(0, 0, 0, 0.1);
    transition: opacity 2s ease;
}

.coffee-stain {
    position: absolute;
    width: 200px;
    height: 200px;
    right: 5%;
    bottom: 10%;
    pointer-events: none;
    background: radial-gradient(circle, transparent 40%, rgba(139, 69, 19, 0.05) 50%, rgba(139, 69, 19, 0.15) 55%, transparent 60%);
    border-radius: 50%;
    filter: blur(2px);
    transform: scaleY(0.8) rotate(15deg);
    z-index: 0;
}

.notebook-lines {
    background-image: repeating-linear-gradient(transparent, transparent 31px,
            hsl(var(--pencil-light) / 0.15) 31px, hsl(var(--pencil-light) / 0.15) 32px);
    background-position-y: 8px;
}

.sketchy-border {
    border: 2px solid hsl(var(--pencil));
    border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
    box-shadow: 3px 4px 0px hsl(var(--pencil) / 0.15);
    transition: border-color 2s ease, box-shadow 2s ease;
}

.sketchy-border-thin {
    border: 1.5px solid hsl(var(--pencil-light));
    border-radius: 15px 225px 15px 255px / 255px 15px 225px 15px;
    transition: border-color 2s ease;
}

.torn-paper {
    clip-path: polygon(0% 2%, 3% 0%, 7% 3%, 11% 1%, 15% 2%, 19% 0%, 23% 2%, 27% 1%, 31% 3%, 35% 0%, 39% 2%, 43% 1%, 47% 3%, 51% 0%, 55% 2%, 59% 1%, 63% 0%, 67% 3%, 71% 1%, 75% 2%, 79% 0%, 83% 3%, 87% 1%, 91% 2%, 95% 0%, 100% 2%, 100% 98%, 97% 100%, 93% 97%, 89% 100%, 85% 98%, 81% 100%, 77% 97%, 73% 100%, 69% 98%, 65% 100%, 61% 97%, 57% 100%, 53% 98%, 49% 100%, 45% 97%, 41% 100%, 37% 98%, 33% 100%, 29% 97%, 25% 100%, 21% 98%, 17% 100%, 13% 97%, 9% 100%, 5% 98%, 0% 100%);
    background-color: hsl(var(--card));
    transition: background-color 2s ease;
}

/* Typography & Animations */
.font-sketch {
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
}

.font-hand {
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
}

@keyframes subtleFloat {

    0%,
    100% {
        transform: translateY(0) rotate(-1deg);
    }

    50% {
        transform: translateY(-4px) rotate(1deg);
    }
}

@keyframes pulseAlert {

    0%,
    100% {
        transform: scale(1);
        filter: brightness(1);
    }

    50% {
        transform: scale(1.05);
        filter: brightness(1.2);
    }
}

@keyframes scribbleIn {
    0% {
        opacity: 0;
        transform: translateY(8px) rotate(-1deg);
    }

    100% {
        opacity: 1;
        transform: translateY(0) rotate(0deg);
    }
}

.title-float {
    animation: subtleFloat 4s ease-in-out infinite;
}

.pulse-alert {
    animation: pulseAlert 1.5s infinite;
    color: hsl(var(--accent));
}

/* Layout */
#viewport {
    position: fixed;
    left: var(--viewport-left);
    top: var(--viewport-top);
    width: var(--viewport-width);
    height: var(--viewport-height);
    padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
    overflow: hidden;
}

#game-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1;
}

#aspect-wrapper {
    position: absolute;
    left: 50%;
    top: 50%;
    width: var(--page-width);
    height: var(--page-height);
    transform: translate(-50%, -50%) scale(var(--game-scale, 0));
    background: transparent;
    overflow: hidden;
}

#game-canvas {
    display: block;
    width: 100%;
    height: 100%;
    cursor: crosshair;
    z-index: 2;
}

/* UI Panel structure */
#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: calc(1 * var(--page-vw)) calc(1 * var(--page-vw)) calc(1 * var(--page-vw)) 45px;
    z-index: 10;
}

#top-bar {
    display: flex;
    justify-content: space-between;
    align-items: stretch;
    background: hsl(var(--card) / 0.95);
    height: 19%;
    pointer-events: auto;
    padding: calc(1 * var(--page-vw));
    position: relative;
    backdrop-filter: blur(4px);
}

/* Stats Panel */
#stats-panel {
    width: 18%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: calc(0.5 * var(--page-vw));
    position: relative;
}

.stat-line {
    font-size: calc(2.3 * var(--page-vmin));
    color: hsl(var(--foreground));
    line-height: 1.2;
    font-weight: bold;
}

.stat-val {
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(3 * var(--page-vmin));
    text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.1);
}

.text-gold {
    color: #b8860b;
}

.text-xp {
    color: #4682b4;
}

/* Top Left Actions (Badge & Pause) */
#top-left-actions {
    position: relative;
    flex: 0 0 112px;
    align-self: center;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: calc(0.5 * var(--page-vw));
    z-index: 20;
    transform: rotate(-3deg);
}

#diff-badge {
    font-size: calc(1.8 * var(--page-vmin));
    padding: 4px 12px;
    border-radius: 4px;
    font-weight: bold;
    color: white;
    box-shadow: 2px 3px 6px rgba(0, 0, 0, 0.25);
    text-align: center;
}

#btn-pause,
#btn-music,
.game-speed {
    background: hsl(var(--card));
    font-size: calc(1.8 * var(--page-vmin));
    padding: 4px 12px;
    cursor: pointer;
    color: hsl(var(--foreground));
    box-shadow: 2px 3px 6px rgba(0, 0, 0, 0.25);
    transition: all 0.2s;
}

#btn-speed {
    position: absolute;
    top: 0;
    left: 72px;
    min-width: 3em;
    padding-left: 5px;
    padding-right: 5px;
    white-space: nowrap;
}

#btn-pause { width: 66px; }

#btn-music {
    width: calc(3.2 * var(--page-vmin));
    min-width: 32px;
    padding-left: 0;
    padding-right: 0;
    line-height: 1;
}

#btn-music.music-muted {
    opacity: 0.58;
}

#btn-pause:hover,
#btn-music:hover,
.game-speed:hover {
    transform: scale(1.05);
}

/* Center Panel */
#center-panel {
    width: 28%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    position: relative;
}

#age-title {
    font-size: calc(4 * var(--page-vmin));
    color: hsl(var(--primary));
    text-transform: capitalize;
    font-weight: bold;
}

.center-btns {
    display: flex;
    gap: calc(1 * var(--page-vw));
    width: 100%;
    height: 60%;
}

/* Action Panel */
#action-panel {
    width: 44%;
    display: flex;
    flex-direction: column;
}

#tabs {
    display: flex;
    justify-content: flex-end;
    gap: calc(0.5 * var(--page-vw));
    height: 30%;
}

.tab-btn {
    background: transparent;
    color: hsl(var(--muted-foreground));
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(2.5 * var(--page-vmin));
    border: none;
    cursor: pointer;
    padding: 0 calc(1 * var(--page-vw));
    font-weight: bold;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    pointer-events: auto;
}

.tab-btn.active {
    color: hsl(var(--foreground));
    border-bottom: 3px solid hsl(var(--accent));
    transform: translateY(-3px) scale(1.05);
}

.tab-btn:hover:not(.active) {
    color: hsl(var(--pencil));
    transform: translateY(-1px);
}

#build-menu {
    display: flex;
    gap: calc(1 * var(--page-vw));
    height: 70%;
    justify-content: flex-end;
    align-items: center;
}

/* General Buttons */
.btn {
    background: hsl(var(--secondary) / 0.5);
    color: hsl(var(--foreground));
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(2.1 * var(--page-vmin));
    cursor: pointer;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    flex: 1;
    max-width: calc(11 * var(--page-vw));
    height: 100%;
    transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: auto;
    position: relative;
}

.btn:hover:not(:disabled) {
    background: hsl(var(--secondary));
    transform: scale(1.04) rotate(1.5deg);
    box-shadow: 2px 4px 8px rgba(0, 0, 0, 0.1);
}

.btn:active:not(:disabled) {
    transform: scale(0.96);
    box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.1);
}

.btn:disabled {
    opacity: 0.4;
    filter: grayscale(1);
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn-cost {
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-weight: bold;
    font-size: calc(2.2 * var(--page-vmin));
    margin-top: 2px;
}

.btn-evolve {
    background: hsl(var(--accent) / 0.15);
    border-color: hsl(var(--accent));
    color: hsl(var(--accent));
}

.btn-special {
    background: hsl(var(--destructive) / 0.1);
    border-color: hsl(var(--destructive));
    color: hsl(var(--destructive));
}

.upgrade-val {
    font-size: calc(1.6 * var(--page-vmin));
    color: hsl(var(--muted-foreground));
    line-height: 1;
}

/* Tooltip System */
#tooltip {
    position: absolute;
    pointer-events: none;
    z-index: 100;
    background: hsl(var(--card) / 0.95);
    backdrop-filter: blur(4px);
    padding: calc(1 * var(--page-vw));
    max-width: calc(20 * var(--page-vw));
    transform: translate(-50%, 10px);
    opacity: 0;
    transition: opacity 0.2s;
    box-shadow: 4px 6px 12px rgba(0, 0, 0, 0.15);
}

#tooltip.visible {
    opacity: 1;
}

.tt-title {
    font-size: calc(2.5 * var(--page-vmin));
    font-weight: bold;
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    color: hsl(var(--primary));
    margin-bottom: calc(0.5 * var(--page-vmin));
    border-bottom: 1px dashed hsl(var(--pencil-light));
    padding-bottom: 2px;
}

.tt-stat {
    font-size: calc(1.8 * var(--page-vmin));
    color: hsl(var(--foreground));
    display: flex;
    justify-content: space-between;
    margin-bottom: 2px;
}

.tt-desc {
    font-size: calc(1.6 * var(--page-vmin));
    color: hsl(var(--muted-foreground));
    margin-top: calc(1 * var(--page-vmin));
    font-style: italic;
}

/* Commander HUD: base health, player prompt, and director response */
#commander-hud {
    position: absolute;
    width: calc(100% - 40px);
    left: 40px;
    top: 21.5%;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 0 calc(2 * var(--page-vw));
    gap: calc(2 * var(--page-vw));
    pointer-events: none;
    z-index: 30;
}

.commander-col {
    width: clamp(12rem, calc(27 * var(--page-vw)), 25rem);
    display: flex;
    flex-direction: column;
    gap: calc(0.7 * var(--page-vmin));
    position: relative;
    pointer-events: none;
}

#director-center-status {
    flex: 1 1 auto;
    max-width: calc(34 * var(--page-vw));
    min-height: calc(5 * var(--page-vmin));
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: calc(0.35 * var(--page-vmin));
    pointer-events: auto;
    color: hsl(var(--pencil));
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    text-align: center;
}

#emotion-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    min-height: calc(2.1 * var(--page-vmin));
    max-width: 100%;
    padding: 0.08rem 0.45rem;
    color: hsl(var(--pencil));
    background: hsl(var(--background) / 0.62);
    border: 1px dashed hsl(var(--pencil-light) / 0.58);
    border-radius: 999px;
    font-size: calc(1.6 * var(--page-vmin));
    font-weight: bold;
    line-height: 1;
    white-space: nowrap;
}

.emotion-icon {
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
    font-size: calc(1.2 * var(--page-vmin));
    line-height: 1;
}

#agreement-strip {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.25rem;
    max-height: calc(5.4 * var(--page-vmin));
    overflow: hidden;
}

.agreement-chip {
    border: 1px dashed hsl(var(--pencil-light) / 0.58);
    border-radius: 999px;
    background: hsl(var(--card) / 0.76);
    color: hsl(var(--muted-foreground));
    font: inherit;
    font-size: calc(1.28 * var(--page-vmin));
    line-height: 1;
    padding: 0.08rem 0.38rem;
    cursor: pointer;
    white-space: nowrap;
    max-width: 12rem;
    overflow: hidden;
    text-overflow: ellipsis;
}

.agreement-chip:hover {
    color: hsl(var(--destructive));
    border-color: hsl(var(--destructive) / 0.55);
    background: hsl(var(--background) / 0.86);
}

.hp-wrapper {
    position: relative;
    width: 100%;
    height: calc(3.5 * var(--page-vmin));
    background: hsl(var(--card));
    padding: 2px;
    box-shadow: 2px 3px 0px rgba(0, 0, 0, 0.1);
    pointer-events: none;
}

.hp-fill {
    height: 100%;
    background: repeating-linear-gradient(45deg, hsl(120, 50%, 60%), hsl(120, 50%, 60%) 10px, hsl(120, 50%, 50%) 10px, hsl(120, 50%, 50%) 20px);
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

#enemy-hp-fill {
    background: repeating-linear-gradient(-45deg, hsl(0, 60%, 60%), hsl(0, 60%, 60%) 10px, hsl(0, 60%, 50%) 10px, hsl(0, 60%, 50%) 20px);
    float: right;
}

.hp-text {
    position: absolute;
    width: 100%;
    text-align: center;
    top: 0;
    font-size: calc(2.5 * var(--page-vmin));
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-weight: bold;
    color: hsl(var(--foreground));
    text-shadow: 1px 1px 0px rgba(255, 255, 255, 0.7);
}

.director-writing {
    position: relative;
    pointer-events: auto;
    color: hsl(var(--pencil));
    text-shadow: 0.5px 0.5px 0 hsl(var(--background) / 0.7);
}

.player-writing {
    min-height: calc(5.2 * var(--page-vmin));
    padding: calc(0.2 * var(--page-vmin)) calc(0.35 * var(--page-vmin)) 0;
}

.ai-writing {
    min-height: calc(12.5 * var(--page-vmin));
    padding: calc(0.2 * var(--page-vmin)) calc(0.35 * var(--page-vmin)) 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    text-align: right;
}

#director-form {
    display: block;
}

#player-last-line {
    min-height: calc(2.35 * var(--page-vmin));
    color: hsl(var(--pencil) / 0.76);
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(2.05 * var(--page-vmin));
    font-weight: bold;
    line-height: 1.08;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
}

.player-last-text {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    min-width: 0;
    white-space: nowrap;
}

.player-last-message {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.player-last-seen {
    flex: 0 0 auto;
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(1.25 * var(--page-vmin));
    line-height: 1;
    border: 1px dashed hsl(var(--pencil-light) / 0.62);
    border-radius: 999px;
    padding: 0.03rem 0.32rem;
    text-transform: lowercase;
    letter-spacing: 0.01em;
}

.player-last-seen.is-pending {
    color: hsl(var(--muted-foreground) / 0.9);
    background: hsl(var(--background) / 0.72);
}

.player-last-seen.is-seen {
    color: hsl(var(--accent));
    border-color: hsl(var(--accent) / 0.58);
    background: hsl(var(--accent) / 0.14);
}

.director-input-row {
    display: flex;
    align-items: flex-end;
    gap: 0.25rem;
    border-bottom: 1.5px dashed hsl(var(--pencil-light) / 0.72);
}

#director-input {
    flex: 1;
    min-width: 0;
    border: none;
    background: transparent;
    outline: none;
    color: hsl(var(--pencil));
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(2.45 * var(--page-vmin));
    font-weight: bold;
    line-height: 1;
    padding: 0 0 calc(0.15 * var(--page-vmin));
}

#director-input:focus {
    color: hsl(var(--foreground));
}

#director-input::placeholder {
    color: hsl(var(--pencil-light) / 0.78);
    font-style: normal;
}

#director-form button {
    flex: 0 0 auto;
    width: 2.1rem;
    min-height: 2rem;
    border: none;
    background: transparent;
    color: hsl(var(--pencil));
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(2.8 * var(--page-vmin));
    font-weight: bold;
    cursor: pointer;
    line-height: 1;
    padding: 0;
    transform: rotate(-5deg);
    transition: transform 0.12s, color 0.12s;
}

#director-form button:hover {
    color: hsl(var(--accent));
    transform: scale(1.12) rotate(0deg);
}

#gemma-card {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.35rem;
    min-height: calc(1.35 * var(--page-vmin));
    margin-top: calc(0.15 * var(--page-vmin));
}

#gemma-status {
    min-width: 0;
    color: hsl(var(--muted-foreground) / 0.82);
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(1.25 * var(--page-vmin));
    line-height: 1;
    overflow: hidden;
    text-overflow: ellipsis;
}

#gemma-card button {
    flex: 0 0 auto;
    border: none;
    background: transparent;
    color: hsl(var(--accent));
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(1.65 * var(--page-vmin));
    font-weight: bold;
    cursor: pointer;
    padding: 0;
    text-decoration: underline;
    text-decoration-thickness: 1px;
}

#gemma-card button:disabled {
    opacity: 0.55;
    cursor: not-allowed;
    text-decoration: none;
}

#director-memory {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    justify-content: flex-end;
    min-height: calc(1.45 * var(--page-vmin));
    margin-bottom: calc(0.2 * var(--page-vmin));
}

.memory-chip {
    color: hsl(var(--muted-foreground) / 0.85);
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(1.55 * var(--page-vmin));
    font-weight: bold;
    line-height: 1;
    text-decoration: underline;
    text-decoration-color: hsl(var(--pencil-light) / 0.45);
    text-decoration-thickness: 1px;
}

#director-log {
    min-height: calc(5.6 * var(--page-vmin));
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: flex-start;
    overflow: hidden;
}

.director-msg {
    max-width: 100%;
    color: hsl(var(--pencil));
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(2.35 * var(--page-vmin));
    font-weight: bold;
    line-height: 1.08;
    word-break: break-word;
    user-select: text;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.director-msg.has-thoughts {
    display: block;
    overflow: visible;
    cursor: pointer;
    -webkit-line-clamp: unset;
    text-align: right;
}

.director-msg-main {
    display: flex;
    justify-content: flex-end;
    align-items: baseline;
    gap: 0.35rem;
    min-width: 0;
}

.director-msg-text {
    min-width: 0;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
}

.director-thought-toggle {
    flex: 0 0 auto;
    border: 1px dashed hsl(var(--pencil-light) / 0.58);
    background: hsl(var(--background) / 0.5);
    color: hsl(var(--pencil-light));
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(1.35 * var(--page-vmin));
    line-height: 1;
    padding: 0 0.25rem 0.08rem;
    cursor: pointer;
}

.director-msg.has-thoughts.thoughts-open .director-thought-toggle {
    color: hsl(var(--accent));
    border-color: hsl(var(--accent) / 0.58);
}

.director-thought-panel {
    max-height: 0;
    margin-top: 0;
    overflow: hidden;
    border-top: 1px dashed transparent;
    opacity: 0;
    transition: max-height 0.2s ease, opacity 0.2s ease, margin-top 0.2s ease;
}

.director-msg.has-thoughts.thoughts-open .director-thought-panel {
    max-height: calc(3.4 * var(--page-vmin));
    margin-top: calc(0.25 * var(--page-vmin));
    border-color: hsl(var(--pencil-light) / 0.5);
    opacity: 1;
}

.director-thought-viewport {
    width: 100%;
    overflow: hidden;
    white-space: nowrap;
}

.director-thought-track {
    display: inline-block;
    min-width: 100%;
    color: hsl(var(--muted-foreground) / 0.9);
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(1.45 * var(--page-vmin));
    line-height: 1.12;
    white-space: nowrap;
    animation: thoughtRoll 18s linear infinite;
}

@keyframes thoughtRoll {
    0% {
        transform: translateX(18%);
    }

    100% {
        transform: translateX(-100%);
    }
}

#director-signal-log {
    min-height: calc(2.35 * var(--page-vmin));
    max-height: calc(2.35 * var(--page-vmin));
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: flex-start;
    overflow: hidden;
    margin-top: calc(0.25 * var(--page-vmin));
}

.director-signal-msg {
    max-width: 100%;
    color: hsl(var(--muted-foreground) / 0.86);
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    font-size: calc(1.65 * var(--page-vmin));
    line-height: 1.12;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    user-select: text;
}

.is-writing {
    clip-path: inset(0 100% 0 0);
    animation: pencilWrite 0.48s steps(18, end) forwards;
}

.ai-writing .is-writing {
    clip-path: inset(0 0 0 100%);
    animation-name: pencilWriteRight;
}

.is-erasing {
    animation: pencilErase 0.2s ease forwards;
}

@keyframes pencilWrite {
    0% {
        opacity: 0.2;
        clip-path: inset(0 100% 0 0);
        filter: blur(0.4px);
    }

    100% {
        opacity: 1;
        clip-path: inset(0 0 0 0);
        filter: blur(0);
    }
}

@keyframes pencilWriteRight {
    0% {
        opacity: 0.2;
        clip-path: inset(0 0 0 100%);
        filter: blur(0.4px);
    }

    100% {
        opacity: 1;
        clip-path: inset(0 0 0 0);
        filter: blur(0);
    }
}

@keyframes pencilErase {
    0% {
        opacity: 1;
        filter: blur(0);
        transform: translateY(0);
    }

    100% {
        opacity: 0;
        filter: blur(1px);
        transform: translateY(calc(-0.2 * var(--page-vmin)));
    }
}

/* Game Over / Start Screens */
.overlay-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: hsl(var(--background) / 0.85);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    z-index: 50;
    text-align: center;
    backdrop-filter: blur(8px);
    transition: opacity 0.5s;
}

.overlay-content {
    width: 70%;
    max-width: 800px;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: calc(4 * var(--page-vw));
    background: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.05'/%3E%3C/svg%3E"), hsl(var(--card));
}

.overlay-screen h1 {
    font-size: calc(10 * var(--page-vmin));
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    color: hsl(var(--foreground));
    margin-bottom: calc(1 * var(--page-vmin));
    text-shadow: 3px 3px 0px hsl(var(--pencil-light)/0.3);
}

.overlay-screen p {
    font-size: calc(3.5 * var(--page-vmin));
    margin-bottom: calc(3 * var(--page-vmin));
    color: hsl(var(--muted-foreground));
    line-height: 1.4;
}

/* Difficulty Grid Menu */
.difficulty-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: calc(2 * var(--page-vw));
    width: 100%;
    margin-top: calc(2 * var(--page-vmin));
}

.diff-btn {
    font-size: calc(3.5 * var(--page-vmin));
    padding: calc(1.5 * var(--page-vw));
    color: hsl(var(--card));
    cursor: pointer;
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    font-weight: bold;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
}

.diff-btn:hover {
    transform: scale(1.05) rotate(-2deg);
    box-shadow: 4px 6px 0px hsl(var(--pencil)/0.2);
}

.diff-normal {
    background: hsl(var(--diff-normal));
}

.diff-hard {
    background: hsl(var(--diff-hard));
}

.diff-harder {
    background: hsl(var(--diff-harder));
}

.diff-impossible {
    background: hsl(var(--diff-impossible));
}

.legal-link {
    margin-top: calc(2 * var(--page-vmin));
    color: hsl(var(--muted-foreground));
    font-size: calc(2 * var(--page-vmin));
    font-family: 'Patrick Hand', 'Comic Sans MS', 'Segoe Print', cursive;
    text-decoration: underline;
    text-underline-offset: 3px;
}

.legal-link:hover {
    color: hsl(var(--accent));
}

.medal-icon {
    position: absolute;
    top: -10px;
    right: -10px;
    font-size: calc(3 * var(--page-vmin));
    filter: drop-shadow(2px 2px 1px rgba(0, 0, 0, 0.3));
    display: none;
}

.beaten .medal-icon {
    display: block;
    animation: subtleFloat 3s infinite;
}

.hidden {
    display: none !important;
}

.fade-out {
    opacity: 0;
    pointer-events: none;
}

/* Preloader */
#preloader {
    position: absolute;
    z-index: 100;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: hsl(var(--background));
}

.loader-brush {
    font-size: 3vmin;
    margin-top: 10px;
    font-family: 'Caveat', 'Comic Sans MS', 'Segoe Print', cursive;
    color: hsl(var(--accent));
    animation: pulseAlert 1s infinite;
}

#loading-bar-wrap {
    width: 40vw;
    height: 10px;
    background: hsl(var(--pencil-light)/0.3);
    margin-top: 2vmin;
    border-radius: 5px;
    overflow: hidden;
}

#loading-bar {
    height: 100%;
    width: 0%;
    background: hsl(var(--accent));
    transition: width 0.2s;
}

#pause-overlay {
    z-index: 60;
}

.pause-speed {
    margin-top: 12px;
    font-size: calc(2.6 * var(--page-vmin));
    padding: calc(0.8 * var(--page-vw)) calc(2.6 * var(--page-vw));
}

/* Optional local opponent controls keep the notebook's existing fonts and ink. */
.gemma-settings { display: block; margin: 10px auto 0; padding: 5px 12px; font-size: max(14px, calc(2 * var(--page-vmin))); background: transparent; cursor: pointer; }
.web-gemma-dialog {
    position: fixed;
    margin: 0;
    inset: auto;
    left: calc(var(--viewport-left) + (var(--viewport-width) + var(--safe-left) - var(--safe-right)) / 2);
    top: calc(var(--viewport-top) + (var(--viewport-height) + var(--safe-top) - var(--safe-bottom)) / 2);
    transform: translate(-50%, -50%);
    width: min(560px, var(--viewport-width) - var(--safe-left) - var(--safe-right) - 24px);
    max-width: none;
    max-height: calc(var(--viewport-height) - var(--safe-top) - var(--safe-bottom) - 24px);
    overflow-y: auto;
    touch-action: pan-y;
    overscroll-behavior: contain;
    padding: 24px;
    background: #fffaf0;
    color: #302b25;
    font-family: 'Patrick Hand', cursive;
    font-size: 18px;
}
.web-gemma-dialog::backdrop { background: rgb(35 30 25 / 65%); }
.web-gemma-dialog h2 { font-size: 32px; margin: 0 0 12px; }
.web-gemma-dialog p { margin: 12px 0; line-height: 1.35; }
.web-gemma-dialog button { font: inherit; color: inherit; padding: 8px 12px; background: #ebe3d5; cursor: pointer; }
.web-gemma-dialog button:disabled { opacity: .5; cursor: default; }
.gemma-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.web-gemma-dialog .gemma-small { font-size: 14px; }
