/* ─────────────────────────────────────────────────────────────
   mini-llm — editorial-technical aesthetic
   Fraunces for display, JetBrains Mono for UI/body.
   Monochromatic: ink on paper, no colored accent.
   Dark by default (near-black charcoal); light is a crisp warm
   off-white. Errors use a single muted red (semantic, not decorative).
   ───────────────────────────────────────────────────────────── */

:root {
    /* Light palette — crisp warm off-white. */
    --bg:        #fbfaf5;
    --bg-card:   #ffffff;
    --ink:       #0f0f11;
    --ink-soft:  #6a6860;
    --rule:      #e4dfd3;
    --error:     #9b2a2a;

    --serif: "Fraunces", "Iowan Old Style", Georgia, serif;
    --mono:  "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;

    --radius: 2px;
    --theme-transition: background-color 0.25s ease, color 0.25s ease, border-color 0.25s ease;
}

:root[data-theme="dark"] {
    /* Dark palette (default) — near-black charcoal, no hue cast. */
    --bg:        #070708;
    --bg-card:   #111113;
    --ink:       #ededf0;
    --ink-soft:  #76767a;
    --rule:      #232326;
    --error:     #d97979;
}

* { box-sizing: border-box; }

/* Ensure [hidden] wins over class-based display: flex etc. */
[hidden] { display: none !important; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--mono);
    font-size: 14px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    transition: var(--theme-transition);
}

.controls,
textarea,
.gate input[type="password"],
#output,
.gate {
    transition: var(--theme-transition);
}

/* ─── Layout ────────────────────────────────────────────────── */

.app {
    max-width: 640px;
    margin: 0 auto;
    padding: 4.5rem 1.75rem 6rem;
    position: relative;
}

.theme-toggle {
    position: absolute;
    top: 1.5rem;
    right: 1.75rem;
    padding: 0.25rem 0.5rem;
    font-family: var(--mono);
    font-size: 1.1rem;
    line-height: 1;
    color: var(--ink-soft);
    background: transparent;
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    cursor: pointer;
    letter-spacing: 0;
    text-transform: none;
    transition: color 0.15s ease, border-color 0.15s ease;
}

.theme-toggle:hover:not(:disabled) {
    color: var(--ink);
    background: transparent;
    border-color: var(--ink-soft);
}

header {
    margin-bottom: 3rem;
}

.eyebrow {
    font-family: var(--mono);
    font-size: 0.72rem;
    letter-spacing: 0.22em;
    text-transform: uppercase;
    color: var(--ink-soft);
    margin: 0 0 1.25rem 0;
}

h1 {
    font-family: var(--serif);
    font-weight: 300;
    font-size: clamp(2.6rem, 6vw, 3.6rem);
    letter-spacing: -0.02em;
    line-height: 1;
    margin: 0 0 1rem 0;
    font-style: italic;
}

.subtitle {
    font-family: var(--mono);
    font-size: 0.86rem;
    color: var(--ink-soft);
    max-width: 36ch;
    margin: 0 0 2rem 0;
    line-height: 1.65;
}

.subtitle em {
    font-family: var(--serif);
    font-style: italic;
    font-size: 1em;
    color: var(--ink);
}

.subtitle .num {
    font-variant-numeric: tabular-nums;
    color: var(--ink);
}

.rule {
    border: none;
    border-top: 1px solid var(--rule);
    margin: 0;
}

/* ─── Controls ──────────────────────────────────────────────── */

.controls {
    display: flex;
    flex-direction: column;
    gap: 1.6rem;
    margin-bottom: 2.5rem;
    padding: 1.5rem 1.5rem 1.75rem;
    background: var(--bg-card);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
}

.slider {
    display: block;
}

.label-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 0.65rem;
}

.label-text {
    font-size: 0.72rem;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--ink-soft);
}

.value {
    font-family: var(--mono);
    font-size: 0.95rem;
    font-variant-numeric: tabular-nums;
    color: var(--ink);
}

input[type="range"] {
    width: 100%;
    height: 2px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--rule);
    outline: none;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 14px;
    height: 14px;
    background: var(--ink);
    border-radius: 50%;
    cursor: pointer;
    transition: transform 0.1s ease;
}

input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.15);
}

input[type="range"]::-moz-range-thumb {
    width: 14px;
    height: 14px;
    background: var(--ink);
    border: none;
    border-radius: 50%;
    cursor: pointer;
}

/* ─── Prompt + button ───────────────────────────────────────── */

.prompt-area {
    margin-bottom: 2rem;
}

.field-label {
    font-size: 0.72rem;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--ink-soft);
    margin: 0 0 0.6rem 0;
    display: block;
}

textarea,
.gate input[type="password"] {
    width: 100%;
    padding: 0.9rem 1rem;
    font-family: var(--mono);
    font-size: 0.9rem;
    color: var(--ink);
    background: var(--bg-card);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    line-height: 1.55;
    resize: vertical;
    transition: var(--theme-transition), border-color 0.15s ease;
}

textarea:focus,
.gate input[type="password"]:focus {
    outline: none;
    border-color: var(--ink);
    box-shadow: 0 0 0 3px var(--rule);
}

.action-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 0.9rem;
}

.hint {
    font-size: 0.72rem;
    color: var(--ink-soft);
    letter-spacing: 0.05em;
}

button {
    padding: 0.65rem 1.4rem;
    font-family: var(--mono);
    font-size: 0.82rem;
    font-weight: 500;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--bg);
    background: var(--ink);
    border: 1px solid var(--ink);
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease, letter-spacing 0.2s ease;
}

button:hover:not(:disabled) {
    background: var(--bg);
    color: var(--ink);
    letter-spacing: 0.12em;
}

button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

/* ─── Output ────────────────────────────────────────────────── */

.output-area {
    margin-top: 2.5rem;
    padding-top: 2rem;
    border-top: 1px solid var(--rule);
}

.output-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    margin: 0 0 0.6rem 0;
}

.output-status {
    font-family: var(--mono);
    font-size: 0.68rem;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--ink-soft);
    /* tabular-nums keeps the live timer from shifting the surrounding
       layout as digits tick over. */
    font-variant-numeric: tabular-nums;
    transition: var(--theme-transition);
}

/* The live-timer portion inside the status strip. Slightly muted so
   the state label reads first, the timer second. */
.output-status .timer {
    margin-left: 0.45em;
    opacity: 0.7;
    font-variant-numeric: tabular-nums;
}

/* Queued state is the most important for the user to notice — give
   it a live dot and a slow pulse so it reads as "waiting, alive". */
.output-status[data-state="queued"] {
    color: var(--ink);
    animation: status-pulse 1.6s ease-in-out infinite;
}

.output-status[data-state="queued"]::before {
    content: "●";
    display: inline-block;
    margin-right: 0.45em;
    font-size: 0.85em;
    transform: translateY(-0.05em);
    color: var(--ink);
}

.output-status[data-state="generating"]::before {
    content: "●";
    display: inline-block;
    margin-right: 0.45em;
    font-size: 0.85em;
    transform: translateY(-0.05em);
    color: var(--ink-soft);
    animation: status-pulse 1.1s ease-in-out infinite;
}

.output-status[data-state="error"] {
    color: var(--error);
}

.output-status[data-state="error"]::before {
    content: "×";
    display: inline-block;
    margin-right: 0.45em;
    font-weight: 500;
}

@keyframes status-pulse {
    0%, 100% { opacity: 0.55; }
    50%      { opacity: 1.0;  }
}

/* ─── Stop-reason footer ───────────────────────────────────────
   Sits below the output pre. Shown after a done event instead of
   the header status, so the finish reason reads as a deliberate
   sign-off rather than a transient label. */

.stop-reason {
    margin: 1rem 0 0 0;
    padding-top: 0.9rem;
    border-top: 1px dashed var(--rule);
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.6rem;
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--ink-soft);
    transition: var(--theme-transition);
    animation: stop-reason-enter 0.3s ease both;
}

.stop-reason-main {
    color: var(--ink);
    letter-spacing: 0.02em;
}

.stop-reason-elapsed {
    font-variant-numeric: tabular-nums;
    color: var(--ink-soft);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    font-size: 0.7rem;
}

/* Leading glyph reflects the kind of stop. Keeps the footer
   scannable at a glance. */
.stop-reason::before {
    display: inline-block;
    margin-right: 0.55em;
    font-weight: 500;
}

.stop-reason[data-state="eos"]::before {
    content: "◆";
    color: var(--ink);
}

.stop-reason[data-state="timeout"]::before {
    content: "◷";
    color: var(--ink-soft);
}

.stop-reason[data-state="max_tokens"]::before {
    content: "▣";
    color: var(--ink-soft);
}

.stop-reason[data-state="error"]::before {
    content: "×";
    color: var(--error);
}

.stop-reason[data-state="error"] .stop-reason-main {
    color: var(--error);
}

@keyframes stop-reason-enter {
    from { opacity: 0; transform: translateY(-3px); }
    to   { opacity: 1; transform: translateY(0); }
}

#output {
    padding: 1.25rem 1.35rem;
    font-family: var(--serif);
    font-size: 1rem;
    font-weight: 300;
    line-height: 1.7;
    color: var(--ink);
    background: var(--bg-card);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    white-space: pre-wrap;
    word-wrap: break-word;
    margin: 0;
    font-style: italic;
}

/* ─── Errors ────────────────────────────────────────────────── */

.error {
    color: var(--error);
    font-size: 0.82rem;
    margin: 1rem 0 0;
}

/* ─── Password gate ─────────────────────────────────────────── */

.gate {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
}

.gate-form {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    width: 100%;
    max-width: 320px;
}

.gate-form .eyebrow {
    margin: 0 0 0.5rem 0;
    text-align: center;
}

.gate-form h2 {
    font-family: var(--serif);
    font-style: italic;
    font-weight: 300;
    font-size: 1.75rem;
    margin: 0 0 1rem 0;
    text-align: center;
    letter-spacing: -0.01em;
}

.gate-form button {
    margin-top: 0.25rem;
}

.gate-form .error {
    text-align: center;
    margin-top: 0.5rem;
}

/* ─── Seed block (the user's prompt, shown above the continuation) ─ */

/* Block-level inside the <pre> so the thin rule and eyebrow line up
   cleanly. Muted ink so the eye registers the weight change at the
   rule — "this is input, what follows is model output". */
#output .seed {
    display: block;
    margin-bottom: 0.9em;
    padding-bottom: 0.9em;
    border-bottom: 1px solid var(--rule);
    animation: seed-enter 0.4s cubic-bezier(0.2, 0.7, 0.2, 1) both;
}

/* Editorial eyebrow above the prompt. Non-italic mono so it reads as
   a label rather than prose. */
#output .seed::before {
    content: "seed";
    display: block;
    margin-bottom: 0.5em;
    font-family: var(--mono);
    font-style: normal;
    font-size: 0.64rem;
    font-weight: 500;
    letter-spacing: 0.24em;
    text-transform: uppercase;
    color: var(--ink-soft);
    opacity: 0.75;
}

#output .seed-body {
    color: var(--ink-soft);
    /* Keep the italic Fraunces that the rest of #output uses so the
       seed and continuation share typographic voice. */
}

@keyframes seed-enter {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ─── Streaming feedback ────────────────────────────────────── */

/* Each chunk arrives in its own span so we can fade it in. Using
   textContent on the span guarantees model output is rendered as
   text, never parsed as HTML. */
#output .chunk {
    animation: chunk-fade 0.28s ease both;
}

@keyframes chunk-fade {
    from { opacity: 0; filter: blur(1px); }
    to   { opacity: 1; filter: blur(0); }
}

/* Solid write-position marker at the output tail while streaming.
   Earlier drafts had it blink — with chunks arriving every few
   hundred ms, a blinking cursor fought the text motion for the eye.
   Solid + slightly translucent reads as "next character lands here"
   without adding another rhythm. The pulsing output-card border is
   already the "still generating" signal. */
#output .cursor {
    display: inline-block;
    width: 0.5ch;
    margin-left: 1px;
    color: var(--ink);
    opacity: 0.7;
    font-style: normal;
    animation: cursor-appear 0.25s ease both;
}

@keyframes cursor-appear {
    from { opacity: 0; }
    to   { opacity: 0.7; }
}

/* Subtle breathing outline on the output card while actively streaming. */
.output-area.streaming #output {
    animation: output-pulse 2.2s ease-in-out infinite;
}

@keyframes output-pulse {
    0%, 100% { border-color: var(--rule); }
    50%      { border-color: var(--ink-soft); }
}

/* ─── Entrance motion ───────────────────────────────────────── */

@keyframes enter-up {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* Stagger the main app's children. The theme toggle is absolutely
   positioned, so exclude it and let it fade in uniformly. */
.app > *:not(.theme-toggle) {
    animation: enter-up 0.55s cubic-bezier(0.2, 0.7, 0.2, 1) both;
}

.app > header                  { animation-delay: 0.05s; }
.app > .controls               { animation-delay: 0.14s; }
.app > .prompt-area            { animation-delay: 0.22s; }
.app > .output-area            { animation-delay: 0.30s; }
.app > .meta                   { animation-delay: 0.38s; }

.theme-toggle {
    animation: enter-up 0.45s cubic-bezier(0.2, 0.7, 0.2, 1) both;
    animation-delay: 0.08s;
}

/* Same treatment for the password gate form. */
.gate-form > * {
    animation: enter-up 0.5s cubic-bezier(0.2, 0.7, 0.2, 1) both;
}
.gate-form > *:nth-child(1) { animation-delay: 0.06s; }
.gate-form > *:nth-child(2) { animation-delay: 0.14s; }
.gate-form > *:nth-child(3) { animation-delay: 0.22s; }
.gate-form > *:nth-child(4) { animation-delay: 0.30s; }

/* ─── Meta footer ───────────────────────────────────────────── */

.meta {
    margin-top: 3rem;
    padding-top: 1.25rem;
    border-top: 1px solid var(--rule);
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.75rem;
    font-size: 0.68rem;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--ink-soft);
}

.meta .sep {
    color: var(--rule);
    user-select: none;
}

/* ─── Ambient chrome — paper-grain noise overlay ────────────── */

/* Fixed, non-interactive, on top of everything but at a very low
   opacity so it only registers as subtle texture. Works on both
   themes because the noise is neutral gray. */
body::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 9999;
    opacity: 0.035;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='220' height='220'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix type='saturate' values='0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
}

:root[data-theme="dark"] body::before {
    opacity: 0.06;
}

/* ─── Reduced motion ────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    #output .cursor { animation: none; opacity: 0.7; }
}
