/* ===================================================================
   Tokens
   =================================================================== */
:root {

    /* Background & surfaces */
    --bg: #FFFFFF;                 /* Pure white for cards */
    --bg-muted: #fcfbe5;           /* Light gray background */
    --bg-hover: #F0F2F2;           /* Hover state background */
        
    --hanko:        #B3312A;
    --hanko-dark:   #8F251F;
    --hanko-soft:   #F3DCD3;
    /* Borders & lines */
    --border: #D5D9D9;             /* Subtle border color */
    --border-light: #E7E9E9;       /* Lighter border */
    
    /* Text colors */
    --text: #0F1111;               /* Primary text - almost black */
    --text-muted: #565959;         /* Secondary text */
    --text-light: #767676;         /* Lighter text for metadata */

    --radius-sm:    6px;
    --radius-md:    10px;
    --radius-lg:    16px;
    

    --shadow-card:  0 1px 2px rgba(38, 34, 32, 0.04), 0 6px 20px rgba(38, 34, 32, 0.05);

    --font-display: "Shippori Mincho", serif;
    --font-ui:      "Zen Kaku Gothic New", "Hiragino Sans", sans-serif;
    --font-mono:    "JetBrains Mono", monospace;

    /* Aliases so the rest of this file (written against the email/grammar
       page's token names) actually resolves. Without these, --paper,
       --ink, --line, --pine, etc. are undefined everywhere below. */
    --paper:        var(--bg-muted);
    --paper-panel:  var(--bg);
    --ink:          var(--text);
    --ink-soft:     var(--text-muted);
    --ink-faint:    var(--text-light);
    --line:         var(--border);
    --line-soft:    var(--border-light);
    --pine:         #1E3A34;
    --pine-soft:    #DCE7E2;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--paper);
    color: var(--ink);
    font-family: var(--font-ui);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* Subtle genkouyoushi (manuscript-paper grid) texture on the base page */
body {
    background-image:
        linear-gradient(var(--line-soft) 1px, transparent 1px),
        linear-gradient(90deg, var(--line-soft) 1px, transparent 1px);
    background-size: 28px 28px;
    background-position: -1px -1px;
    background-attachment: fixed;
}

button, input, select, textarea {
    font-family: inherit;
    font-size: inherit;
    color: inherit;
}

/* ===================================================================
   Layout shell
   =================================================================== */
.app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app-header {
    padding: 18px 28px;
    border-bottom: 1px solid var(--line);
    background: rgba(255, 169, 41, 0.88);
    backdrop-filter: blur(6px);
    position: sticky;
    top: 0;
    z-index: 20;
}

.brand {
    display: flex;
    align-items: center;
    gap: 14px;
}

.hanko {
    flex: none;
    width: 40px;
    height: 40px;
    border-radius: 4px;
    border: 2px solid var(--hanko);
    color: var(--hanko);
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-display);
    font-size: 20px;
    font-weight: 700;
    transform: rotate(-2deg);
}

.brand-text h1 {
    margin: 0;
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 20px;
    letter-spacing: 0.01em;
    color: var(--ink);
}

.brand-sub {
    margin: 2px 0 0;
    font-size: 12px;
    color: var(--ink-soft);
    letter-spacing: 0.02em;
}

.page {
    flex: 1;
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 24px;
    padding: 24px 28px 40px;
    align-items: start;
}

/* ===================================================================
   Sidebar
   =================================================================== */
.sidebar {
    background: var(--paper-panel);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    position: sticky;
    top: 96px;
    max-height: calc(100vh - 120px);
    overflow: hidden;
}

.sidebar-scroll {
    padding: 22px;
    overflow-y: auto;
    max-height: calc(100vh - 120px);
}

.field-group {
    margin-bottom: 20px;
    padding-bottom: 20px;
    border-bottom: 1px dashed var(--line);
}

.field-group:last-of-type {
    border-bottom: none;
}

.eyebrow {
    display: inline-block;
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--hanko-dark);
    margin-bottom: 10px;
}

.panel-header .eyebrow {
    margin-bottom: 0;
}

label {
    display: block;
    font-size: 12px;
    font-weight: 500;
    color: var(--ink-soft);
    margin: 12px 0 6px;
}

label:first-of-type {
    margin-top: 0;
}

input[type="text"],
select,
textarea {
    width: 100%;
    padding: 9px 11px;
    background: var(--paper);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    outline: none;
    transition: border-color 0.15s ease, background 0.15s ease;
}

input[type="text"]:focus,
select:focus,
textarea:focus {
    border-color: var(--pine);
    background: var(--paper-panel);
}

textarea {
    min-height: 84px;
    resize: vertical;
    line-height: 1.5;
}

/* ===================================================================
   Buttons
   =================================================================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 16px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    font-size: 13px;
    font-weight: 500;
    letter-spacing: 0.01em;
    cursor: pointer;
    transition: transform 0.1s ease, background 0.15s ease, border-color 0.15s ease;
}

.btn:active {
    transform: translateY(1px);
}

.btn-primary {
    background: var(--hanko);
    border-color: var(--hanko);
    color: #FBF3EE;
}

.btn-primary:hover {
    background: var(--hanko-dark);
    border-color: var(--hanko-dark);
}

.btn-generate {
    width: 100%;
    padding: 12px 16px;
    font-size: 14px;
    margin-top: 4px;
}

.btn-ghost {
    background: transparent;
    border-color: var(--line);
    color: var(--ink);
}

.btn-ghost:hover {
    border-color: var(--pine);
    color: var(--pine);
    background: var(--pine-soft);
}

.btn-small {
    padding: 5px 10px;
    font-size: 12px;
}

/* ===================================================================
   Loading
   =================================================================== */
.loading {
    margin-top: 18px;
    padding: 16px;
    border-radius: var(--radius-md);
    background: var(--hanko-soft);
    text-align: center;
}

.loading h3 {
    margin: 10px 0 2px;
    font-size: 13px;
    font-weight: 500;
    color: var(--hanko-dark);
}

.loading p {
    margin: 0;
    font-size: 12px;
    color: var(--ink-soft);
}

.spinner {
    width: 22px;
    height: 22px;
    margin: 0 auto;
    border: 2px solid var(--hanko-dark);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
    .spinner { animation: none; }
}

/* ===================================================================
   Main content
   =================================================================== */
.main-content {
    display: flex;
    flex-direction: column;
    gap: 20px;
    min-width: 0;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

/* History strip */
.history-panel {
    background: var(--paper-panel);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    padding: 18px;
}

.history-strip {
    display: flex;
    gap: 10px;
    overflow-x: auto;
    padding-bottom: 4px;
}

.history-strip:empty::before {
    content: "Generated materials will appear here.";
    font-size: 12px;
    color: var(--ink-faint);
    padding: 10px 2px;
}

.history-item {
    flex: none;
    width: 190px;
    padding: 12px 13px;
    background: var(--paper);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: border-color 0.15s ease, background 0.15s ease;
}

.history-item:hover {
    border-color: var(--hanko);
    background: var(--hanko-soft);
}

.history-item.active {
    background: var(--pine-soft);
    border-color: var(--pine);
}

.history-title {
    font-weight: 700;
    font-size: 14px;
    margin-bottom: 4px;
}

.history-meta {
    font-size: 12px;
    color: var(--ink-soft);
    margin-bottom: 6px;
}

.history-time {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--ink-faint);
}

/* ===================================================================
   Workspace: preview + resizer + questions
   =================================================================== */
.workspace {
    display: flex;
    align-items: stretch;
    gap: 0;
    min-height: 420px;
}

.preview-panel,
.questions-panel {
    background: var(--paper-panel);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    padding: 16px;
    min-width: 0;
}

.preview-panel {
    flex: 0 0 62%;
    display: flex;
    flex-direction: column;
}

.questions-panel {
    flex: 1 1 auto;
    min-width: 240px;
    overflow-y: auto;
}

/* Draggable divider between the two panels */
.panel-resizer {
    flex: none;
    width: 18px;
    margin: 0 -4px;
    position: relative;
    cursor: col-resize;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
}

.panel-resizer::before {
    content: "";
    width: 4px;
    height: 44px;
    border-radius: 3px;
    background: var(--line);
    transition: background 0.15s ease;
}

.panel-resizer:hover::before,
.panel-resizer:focus-visible::before {
    background: var(--hanko);
}

.panel-resizer:focus-visible {
    outline: none;
}

.preview-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 12px;
}

#mail_client {
    max-width: 140px;
}

.toolbar-actions {
    display: flex;
    gap: 8px;
}

.preview {
    flex: 1;
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    overflow: hidden;
    background: repeating-linear-gradient(
        45deg,
        #FBF8F2,
        #FBF8F2 10px,
        #F6F1E7 10px,
        #F6F1E7 20px
    );
    min-height: 360px;
}

#preview-frame {
    width: 100%;
    height: 100%;
    min-height: 360px;
    border: none;
    display: block;
    background: #fff;
}

.questions-output:empty::before {
    content: "Comprehension questions will appear here once generated.";
    font-size: 12px;
    color: var(--ink-faint);
}

.questions-loading {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    margin-bottom: 14px;
    background: var(--hanko-soft);
    border-radius: var(--radius-md);
    font-size: 13px;
    color: var(--hanko-dark);
}

.questions-loading .spinner {
    width: 16px;
    height: 16px;
    margin: 0;
    border-width: 2px;
}

.question-set {
    margin-bottom: 22px;
    padding-bottom: 18px;
    border-bottom: 1px dashed var(--line);
}

.question-set:last-of-type {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.question-set-header {
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--hanko-dark);
    margin-bottom: 10px;
}

.question-card {
    background: var(--paper);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    padding: 12px 14px;
    margin-bottom: 10px;
}

.question-card h4 {
    margin: 0 0 6px;
    font-size: 13px;
    color: var(--ink);
}

.question-card .answer-toggle {
    margin-top: 6px;
    padding: 5px 10px;
    font-size: 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--paper-panel);
    color: var(--pine);
    cursor: pointer;
}

.question-card .answer-toggle:hover {
    border-color: var(--pine);
    background: var(--pine-soft);
}

.question-card .answer {
    margin-top: 8px;
    font-size: 13px;
    color: var(--ink-soft);
}

/* ===================================================================
   Responsive
   =================================================================== */
@media (max-width: 980px) {
    .page {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: static;
        max-height: none;
    }

    .sidebar-scroll {
        max-height: none;
        overflow: visible;
    }

    .workspace {
        flex-direction: column;
    }

    .preview-panel {
        flex-basis: auto !important;
    }

    .panel-resizer {
        display: none;
    }

    .questions-panel {
        min-width: 0;
    }
}

@media (max-width: 560px) {
    .app-header {
        padding: 14px 16px;
    }

    .page {
        padding: 16px;
    }
}

/* ===================================================================
   Accessibility: visible focus ring
   =================================================================== */
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--pine);
    outline-offset: 2px;
}

/* ===================================================================
   Audio modal (reviews)
   =================================================================== */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 17, 17, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease;
}

.modal-backdrop.open {
    opacity: 1;
    pointer-events: auto;
}

.modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--paper-panel);
    border-radius: var(--radius-lg);
    box-shadow: 0 20px 60px rgba(15, 17, 17, 0.25);
    width: 100%;
    max-width: 520px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

.modal-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid var(--line);
    cursor: move;
    user-select: none;
}

.modal-header h2 {
    margin: 4px 0 0;
    font-family: var(--font-display);
    font-size: 18px;
    font-weight: 700;
    color: var(--ink);
}

.modal-close {
    background: transparent;
    border: none;
    font-size: 22px;
    line-height: 1;
    color: var(--ink-soft);
    cursor: pointer;
    padding: 4px;
}

.modal-close:hover {
    color: var(--hanko);
}

.modal-body {
    padding: 20px 24px 24px;
    overflow-y: auto;
}

.modal-subtitle {
    font-size: 13px;
    color: var(--ink-soft);
    margin: 0 0 16px;
}

.modal-actions {
    display: flex;
    gap: 10px;
    margin-bottom: 18px;
}

.audio-output:empty::before {
    content: "";
}

.audio-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--line-soft);
}

.audio-row:last-child {
    border-bottom: none;
}

.audio-row .audio-name {
    flex: none;
    width: 120px;
    font-weight: 700;
    font-size: 13px;
    color: var(--ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.audio-row audio {
    flex: 1;
    height: 34px;
}

.audio-row .audio-missing {
    flex: 1;
    font-size: 12px;
    color: var(--ink-faint);
    font-style: italic;
}

/* ===================================================================
   Vocabulary table (reused across vocab/grammar modals)
   =================================================================== */
.question-set {
    margin-bottom: 22px;
    padding-bottom: 18px;
    border-bottom: 1px dashed var(--line);
}

.question-set:last-of-type {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.question-set-header {
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--hanko-dark);
    margin-bottom: 10px;
}

.vocab-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 8px;
}

.vocab-table tr {
    border-bottom: 1px solid var(--line-soft);
}

.vocab-table tr:last-child {
    border-bottom: none;
}

.vocab-table td {
    padding: 8px 6px;
    font-size: 13px;
    vertical-align: top;
}

.vocab-word {
    font-weight: 700;
    white-space: nowrap;
}

.vocab-type {
    color: var(--ink-soft);
    font-size: 12px;
    font-style: italic;
    white-space: nowrap;
}

.vocab-translation {
    color: var(--ink);
}

.grammar-card {
    background: var(--paper);
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    padding: 12px 14px;
    margin-bottom: 10px;
}

.grammar-card h4 {
    margin: 0 0 8px;
    font-size: 14px;
    color: var(--ink);
}

.grammar-card p {
    margin: 0 0 6px;
    font-size: 13px;
    line-height: 1.5;
}

.grammar-card p:last-child {
    margin-bottom: 0;
}

.grammar-example {
    font-family: var(--font-mono);
    color: var(--ink);
    background: var(--paper-panel);
    border-radius: var(--radius-sm);
    padding: 6px 9px;
}

.grammar-example-translation {
    color: var(--ink-soft);
    font-style: italic;
}

.grammar-explanation {
    color: var(--ink);
}

.grammar-conjugation {
    color: var(--pine);
}
