:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #f0f2f6;
    --surface-3: #e8ecf2;
    --border: #e2e6ee;
    --border-light: #eef1f6;
    --text: #141c2b;
    --muted: #64748b;
    --accent: #4f6ef7;
    --accent-hover: #3d5ce8;
    --accent-soft: #eef1fe;
    --accent-muted: #c7d2fe;
    --good: #16a34a;
    --good-bg: #ecfdf3;
    --warn: #d97706;
    --warn-bg: #fffbeb;
    --bad: #dc2626;
    --bad-bg: #fef2f2;
    --radius: 14px;
    --radius-sm: 10px;
    --radius-lg: 18px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow: 0 2px 8px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow-lg: 0 8px 30px rgba(15, 23, 42, 0.08);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
    --toolbar-h: 48px;
    --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
    height: 100%;
    height: -webkit-fill-available;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    min-height: 100%;
}

/* Chat: lock to viewport; only .messages scrolls. */
body.body--chat {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    height: -webkit-fill-available;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    overscroll-behavior: none;
    background: var(--bg);
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }

/* ---- Side menu ---- */
.menu-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.35);
    z-index: 200;
    opacity: 0;
    transition: opacity 0.2s;
}
.menu-backdrop.open { display: block; opacity: 1; }
.menu-drawer {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(300px, 88vw);
    background: var(--surface);
    z-index: 201;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    padding: calc(var(--safe-top) + 16px) 16px calc(16px + var(--safe-bottom));
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.menu-drawer.open { transform: translateX(0); }
.menu-brand {
    font-weight: 700;
    font-size: 16px;
    letter-spacing: -0.02em;
    margin-bottom: 8px;
    padding: 0 4px;
}
.menu-section { margin-bottom: 8px; }
.menu-section-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
    margin-bottom: 8px;
    padding: 0 4px;
}
.menu-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin-top: auto;
    padding-top: 16px;
    border-top: 1px solid var(--border-light);
}
.menu-nav a {
    display: block;
    padding: 12px 10px;
    border-radius: var(--radius-sm);
    color: var(--text);
    font-weight: 500;
    font-size: 15px;
    text-decoration: none;
}
.menu-nav a:hover { background: var(--surface-2); color: var(--accent); }
.menu-logout { margin: 0; padding-top: 4px; }
.menu-logout-btn {
    width: 100%;
    text-align: left;
    padding: 12px 10px;
    border: none;
    background: none;
    font: inherit;
    font-size: 15px;
    font-weight: 500;
    color: var(--muted);
    cursor: pointer;
    border-radius: var(--radius-sm);
}
.menu-logout-btn:hover { background: var(--bad-bg); color: var(--bad); }

.page-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
    padding-top: var(--safe-top);
}
.page-toolbar-text h1 {
    margin: 0;
    font-size: 20px;
    font-weight: 700;
    letter-spacing: -0.02em;
}
.page-toolbar-text p {
    margin: 2px 0 0;
    font-size: 13px;
    color: var(--muted);
}

/* ---- Layout ---- */
.main {
    max-width: 1080px;
    margin: 0 auto;
    padding: 20px 16px 32px;
}
.main--chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    width: 100%;
    max-width: none !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden;
    background: var(--bg);
}
.main--auth {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
}

.flash-wrap {
    position: fixed;
    top: env(safe-area-inset-top, 0px);
    left: 0;
    right: 0;
    z-index: 150;
    max-width: 1080px;
    margin: 0 auto;
    padding: 8px 16px 0;
    pointer-events: none;
}
.flash-wrap .flash { pointer-events: auto; }
.flash {
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    margin-bottom: 8px;
    background: var(--accent-soft);
    border: 1px solid var(--border);
    font-size: 14px;
}
.flash-error { background: var(--bad-bg); border-color: #fecaca; color: #991b1b; }
.flash-success { background: var(--good-bg); border-color: #bbf7d0; color: #166534; }

/* ---- Typography ---- */
.page-header { margin-bottom: 24px; }
.page-header h1 {
    margin: 0 0 6px;
    font-size: 26px;
    font-weight: 700;
    letter-spacing: -0.03em;
    line-height: 1.2;
}
.page-header p {
    margin: 0;
    color: var(--muted);
    font-size: 14px;
}
.section-label {
    display: block;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
    margin-bottom: 12px;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---- Cards ---- */
.card {
    background: var(--surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    padding: 20px;
    margin-bottom: 16px;
}
.card h2 {
    margin: 0 0 4px;
    font-size: 17px;
    font-weight: 600;
    letter-spacing: -0.02em;
}
.card-desc {
    margin: 0 0 18px;
    color: var(--muted);
    font-size: 14px;
    line-height: 1.5;
}

/* ---- Forms ---- */
label {
    display: block;
    font-weight: 500;
    font-size: 14px;
    margin-bottom: 6px;
    color: var(--text);
}
input[type=text],
input[type=password],
input[type=email],
select,
textarea {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font: inherit;
    font-size: 16px; /* prevents iOS zoom */
    color: var(--text);
    transition: border-color 0.15s, box-shadow 0.15s;
    -webkit-appearance: none;
    appearance: none;
}
select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent-muted);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.field { margin-bottom: 18px; }
.field-hint {
    margin-top: 6px;
    font-size: 13px;
    color: var(--muted);
    line-height: 1.4;
}

/* ---- Buttons ---- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 12px 18px;
    border-radius: var(--radius-sm);
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
    min-height: 44px;
    touch-action: manipulation;
}
.btn:hover { background: var(--accent-hover); text-decoration: none; }
.btn:active { transform: scale(0.98); }
.btn-block { width: 100%; }
.btn-ghost {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--surface-3); }
.btn-danger { background: var(--bad-bg); color: var(--bad); border: 1px solid #fecaca; }
.btn-danger:hover { background: #fee2e2; }
.btn-sm { padding: 8px 12px; font-size: 13px; min-height: 36px; }
.btn-icon {
    width: 40px;
    height: 40px;
    padding: 0;
    min-height: 40px;
    border-radius: 10px;
}

/* ---- Auth ---- */
.auth-wrap { width: 100%; max-width: 400px; }
.auth-wrap .card { padding: 28px 24px; }
.auth-wrap h1 {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.03em;
    margin: 0 0 6px;
}
.auth-subtitle {
    color: var(--muted);
    font-size: 14px;
    margin: 0 0 24px;
}
.auth-alt { text-align: center; margin-top: 20px; font-size: 14px; }

/* ---- Settings ---- */
.settings-stack { display: flex; flex-direction: column; gap: 0; }
.settings-card-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}
.settings-card-header h2 { margin: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.upload-zone {
    position: relative;
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 28px 20px;
    text-align: center;
    background: var(--surface-2);
    transition: border-color 0.15s, background 0.15s;
    cursor: pointer;
}
.upload-zone:hover,
.upload-zone:focus-within {
    border-color: var(--accent-muted);
    background: var(--accent-soft);
}
.upload-zone input[type=file] {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
    width: 100%;
    height: 100%;
}
.upload-icon {
    width: 40px;
    height: 40px;
    margin: 0 auto 10px;
    background: var(--surface);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-sm);
}
.upload-icon svg { width: 20px; height: 20px; stroke: var(--accent); fill: none; stroke-width: 2; }
.upload-title { font-weight: 600; font-size: 14px; margin-bottom: 4px; }
.upload-hint { font-size: 13px; color: var(--muted); }
.upload-filename {
    margin-top: 10px;
    font-size: 13px;
    color: var(--accent);
    font-weight: 500;
}

.lesson-list { display: flex; flex-direction: column; gap: 10px; }
.lesson-card {
    display: block;
    padding: 16px;
    background: var(--surface-2);
    border: 1px solid var(--border-light);
    border-radius: var(--radius);
    transition: border-color 0.15s, box-shadow 0.15s;
}
.lesson-card:hover {
    border-color: var(--accent-muted);
    box-shadow: var(--shadow-sm);
}
.lesson-card-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 8px;
}
.lesson-card-title {
    font-weight: 600;
    font-size: 15px;
    color: var(--text);
    line-height: 1.3;
}
.lesson-card-title:hover { color: var(--accent); text-decoration: none; }
.lesson-card-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
    font-size: 13px;
    color: var(--muted);
}
.lesson-card-topics { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 12px; }
.lesson-card-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.lesson-card-actions form { margin: 0; }
.lesson-card-error {
    margin-top: 8px;
    padding: 8px 10px;
    background: var(--bad-bg);
    border-radius: 8px;
    font-size: 12px;
    color: #991b1b;
    line-height: 1.4;
}
.lesson-count {
    font-size: 13px;
    color: var(--muted);
    font-weight: 500;
    white-space: nowrap;
}
.empty-state {
    text-align: center;
    padding: 32px 16px;
    color: var(--muted);
    font-size: 14px;
}

/* ---- Pills & status ---- */
.pill {
    display: inline-block;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 500;
    background: var(--surface);
    color: var(--muted);
    border: 1px solid var(--border);
}
.pill.good { background: var(--good-bg); color: var(--good); border-color: #bbf7d0; }
.pill.warn { background: var(--warn-bg); color: var(--warn); border-color: #fde68a; }
.pill.bad { background: var(--bad-bg); color: var(--bad); border-color: #fecaca; }

.status {
    font-size: 11px;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 999px;
    white-space: nowrap;
    flex-shrink: 0;
}
.status.ready { background: var(--good-bg); color: var(--good); }
.status.processing, .status.pending { background: var(--warn-bg); color: var(--warn); }
.status.failed { background: var(--bad-bg); color: var(--bad); }

.row-between {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

/* ---- Chat ---- */
.conv-list { display: flex; flex-direction: column; gap: 2px; }
.conv-item {
    display: block;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 14px;
    line-height: 1.3;
    transition: background 0.12s;
}
.conv-item:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.conv-item.active {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 600;
}

.chat-panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    width: 100%;
    max-width: none;
    background: var(--bg);
}
.chat-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    padding: 10px 12px;
    padding-top: max(10px, env(safe-area-inset-top, 0px));
    background: var(--surface);
    border-bottom: 1px solid var(--border-light);
}
.chat-toolbar-title {
    font-weight: 600;
    font-size: 14px;
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.messages {
    flex: 1;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
}
.msg {
    flex: 0 0 auto;
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 18px;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    font-size: 15px;
    line-height: 1.45;
    box-shadow: var(--shadow-sm);
}
.msg.user {
    align-self: flex-end;
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 6px;
}
.msg.assistant {
    align-self: flex-start;
    background: var(--surface);
    border: 1px solid var(--border-light);
    border-bottom-left-radius: 6px;
}
.msg-text {
    margin: 0;
    white-space: pre-wrap;
}
.msg-turn {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    width: 100%;
    flex: 0 0 auto;
}

/* ---- Interactive widgets (separate cards, not inside text bubbles) ---- */
.widget-stack {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    min-width: 0;
    flex: 0 0 auto;
}
.widget {
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 14px;
    box-shadow: var(--shadow-sm);
}
.widget-type {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--accent);
    margin-bottom: 8px;
}
.widget-prompt {
    font-weight: 600;
    font-size: 15px;
    line-height: 1.45;
    margin-bottom: 12px;
}
.widget-georgian {
    font-size: 22px;
    font-weight: 600;
    margin-bottom: 4px;
    line-height: 1.3;
}
.widget-translit {
    font-size: 13px;
    color: var(--muted);
    margin-bottom: 12px;
}
.widget-blank {
    font-size: 17px;
    line-height: 1.5;
    margin-bottom: 12px;
}
.widget-blank-gap {
    display: inline-block;
    min-width: 60px;
    border-bottom: 2px solid var(--accent-muted);
    margin: 0 4px;
    color: var(--accent);
    font-weight: 600;
}
.widget-meta {
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 10px;
}
.widget-options {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.widget-opt {
    display: block;
    width: 100%;
    text-align: left;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font: inherit;
    font-size: 15px;
    color: var(--text);
    cursor: pointer;
    transition: border-color 0.12s, background 0.12s, transform 0.1s;
    min-height: 44px;
    touch-action: manipulation;
}
.widget-opt:hover:not(:disabled) {
    border-color: var(--accent-muted);
    background: var(--accent-soft);
}
.widget-opt:active:not(:disabled) { transform: scale(0.98); }
.widget-opt:disabled { cursor: default; opacity: 0.85; }
.widget-opt.correct {
    border-color: #86efac;
    background: var(--good-bg);
    color: var(--good);
    font-weight: 600;
}
.widget-opt.wrong {
    border-color: #fca5a5;
    background: var(--bad-bg);
    color: var(--bad);
}
.widget-opt.missed {
    border-color: #86efac;
    background: var(--good-bg);
    color: var(--good);
    font-weight: 600;
    opacity: 1;
}
.widget-row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.widget-row .widget-opt { flex: 1; min-width: 100px; text-align: center; }
.widget-feedback {
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    line-height: 1.45;
}
.widget-feedback.ok { background: var(--good-bg); color: #166534; border: 1px solid #bbf7d0; }
.widget-feedback.no { background: var(--bad-bg); color: #991b1b; border: 1px solid #fecaca; }
.widget-feedback.neutral { background: var(--accent-soft); color: var(--text); border: 1px solid var(--border); }

.widget-flashcard {
    text-align: center;
    padding: 20px 16px;
}
.widget-flashcard-front {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 8px;
}
.widget-flashcard-back {
    font-size: 22px;
    font-weight: 700;
    color: var(--accent);
    margin: 12px 0;
    padding: 12px;
    background: var(--surface);
    border-radius: var(--radius-sm);
}
.widget-rating {
    display: flex;
    gap: 6px;
    justify-content: center;
}
.widget-rating button {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--surface);
    font-weight: 600;
    font-size: 15px;
    cursor: pointer;
    transition: all 0.12s;
}
.widget-rating button:hover:not(:disabled) {
    border-color: var(--accent);
    background: var(--accent-soft);
    color: var(--accent);
}
.widget-rating button:disabled { opacity: 0.6; }
.composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    flex-shrink: 0;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    background: var(--surface);
    border-top: 1px solid var(--border-light);
}
.composer textarea {
    flex: 1;
    resize: none;
    min-height: 44px;
    max-height: 120px;
    padding: 11px 14px;
    border-radius: 22px;
    line-height: 1.4;
}
.composer textarea::placeholder { color: transparent; }
.composer .btn {
    border-radius: 22px;
    padding: 11px 18px;
    flex-shrink: 0;
}
.empty-hint {
    align-self: center;
    color: var(--muted);
    text-align: center;
    margin: 24px auto;
    max-width: 300px;
    font-size: 14px;
    line-height: 1.6;
    padding: 12px;
}
.messages > .flash {
    align-self: stretch;
    width: 100%;
}
.empty-hint p { margin: 0 0 8px; }

/* ---- Lesson detail ---- */
.chunk-card { margin-bottom: 12px; }
.chunk-text {
    white-space: pre-wrap;
    margin: 12px 0 0;
    font-size: 14px;
    line-height: 1.6;
}
.vocab-table-wrap { overflow-x: auto; margin-top: 12px; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border-light); }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }

/* ---- Responsive ---- */
@media (max-width: 768px) {
    .main { padding: 12px 16px calc(20px + var(--safe-bottom)); }
    .grid-2 { grid-template-columns: 1fr; }
    .card { padding: 18px 16px; border-radius: var(--radius); }
    .settings-card-header { flex-direction: column; align-items: stretch; }
    .settings-card-header .btn { align-self: flex-start; }
    .row-between--lesson-header { flex-direction: column; align-items: flex-start; }
    .msg { max-width: 90%; }
    .widget { padding: 10px; }
    .composer textarea { font-size: 16px; }
}
