/* AI Study Buddy — design system v2 */
:root {
    --brand: #6d28d9;
    --brand-dark: #5b21b6;
    --brand-soft: #ede9fe;
    --accent: #059669;
    --accent-soft: #d1fae5;
    --ink: #1e1b2e;
    --muted: #6b7280;
    --line: #e5e7eb;
    --bg: #f4f2fb;
    --card: #ffffff;
    --danger: #dc2626;
    --danger-soft: #fee2e2;
    --warn-soft: #fef9c3;
    --radius: 12px;
    --radius-s: 8px;
    --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px;
    --font: 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif;
    --maxw: 960px;
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
    font-family: var(--font);
    background: var(--bg);
    margin: 0;
    padding: 0;
    min-height: 100vh;
    color: var(--ink);
    font-size: clamp(0.92rem, 0.85rem + 0.3vw, 1rem);
    line-height: 1.55;
    padding-bottom: env(safe-area-inset-bottom);
}
img, pre, code, table { max-width: 100%; }
pre, code, p, h1, h2, h3 { overflow-wrap: anywhere; word-wrap: break-word; }

.skip-link { position: absolute; left: -999px; top: 0; background: var(--brand); color: #fff; padding: 8px 12px; z-index: 99; border-radius: 0 0 8px 0; }
.skip-link:focus { left: 0; }

/* ---------- Navbar ---------- */
.navbar { position: sticky; top: 0; z-index: 50; background: rgba(255,255,255,.9); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.nav-inner { max-width: var(--maxw); margin: 0 auto; padding: 10px 16px; padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); display: flex; align-items: center; gap: var(--s3); }
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-weight: 700; font-size: 1.02rem; margin-right: auto; white-space: nowrap; }
.brand-icon { background: var(--brand-soft); border-radius: 8px; padding: 2px 6px; }
.nav-links { display: flex; gap: 4px; }
.nav-link { text-decoration: none; color: var(--muted); padding: 8px 12px; border-radius: 8px; font-size: 0.92rem; white-space: nowrap; }
.nav-link:hover, .nav-link.active { background: var(--brand-soft); color: var(--brand-dark); }
.nav-right { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.health { display: flex; align-items: center; gap: 6px; font-size: 0.78rem; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; white-space: nowrap; }
.health-dot { width: 8px; height: 8px; border-radius: 50%; background: #f59e0b; flex-shrink: 0; }
.health.ok .health-dot { background: #059669; }
.health.down .health-dot { background: var(--danger); }
.nav-toggle { display: none; flex: 0 0 auto; width: auto; padding: 8px 12px; font-size: 1.1rem; }

/* ---------- Layout ---------- */
.container { max-width: var(--maxw); width: 100%; margin: 0 auto; padding: var(--s5) var(--s4) var(--s6); padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: var(--s4); box-shadow: 0 1px 2px rgba(20,10,60,.05); }
.card + .card, .card + .actions-grid, .actions-grid + .status-bar { margin-top: var(--s4); }

/* ---------- Hero ---------- */
.hero { text-align: center; padding: var(--s5) var(--s2) var(--s4); }
.eyebrow { display: inline-block; font-size: 0.75rem; letter-spacing: .12em; text-transform: uppercase; color: var(--brand-dark); background: var(--brand-soft); border-radius: 999px; padding: 4px 12px; margin: 0 0 var(--s3); font-weight: 700; }
.hero h1 { margin: 0 0 var(--s2); font-size: clamp(1.5rem, 4.5vw, 2.3rem); line-height: 1.15; letter-spacing: -0.01em; }
.lede { color: var(--muted); margin: 0 auto var(--s4); max-width: 60ch; font-size: clamp(0.92rem, 1vw + .8rem, 1.02rem); }
.stat-chips { display: flex; gap: var(--s2); justify-content: center; flex-wrap: wrap; margin-bottom: var(--s4); }
.chip { background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
.chip strong { color: var(--ink); }
.hero-cta { display: flex; gap: var(--s2); justify-content: center; flex-wrap: wrap; }
.cta-primary { background: var(--brand); color: #fff; text-decoration: none; padding: 12px 20px; border-radius: 10px; font-weight: 600; }
.cta-primary:hover { background: var(--brand-dark); }
.cta-ghost { border: 1px solid var(--line); background: var(--card); color: var(--ink); text-decoration: none; padding: 12px 20px; border-radius: 10px; }

/* ---------- Steps ---------- */
.how { margin-top: var(--s4); }
.how h2, .trust { scroll-margin-top: 70px; }
.how h2 { font-size: clamp(1.1rem, 2.5vw, 1.35rem); margin: 0 0 var(--s3); }
.steps { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--s3); }
.steps li { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: var(--s3); display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.step-n { font-size: 0.72rem; font-weight: 800; color: var(--brand-dark); background: var(--brand-soft); align-self: flex-start; border-radius: 6px; padding: 2px 8px; }
.steps strong { font-size: 0.95rem; }
.steps span:last-child { color: var(--muted); font-size: 0.85rem; }

/* ---------- Studio ---------- */
#studio { scroll-margin-top: 70px; margin-top: var(--s4); display: flex; flex-direction: column; gap: var(--s4); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: var(--s2); margin-bottom: var(--s3); flex-wrap: wrap; }
.card-head h2 { margin: 0; font-size: 1.1rem; }
.muted { color: var(--muted); } .small { font-size: 0.82rem; }
.visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
textarea { width: 100%; min-height: 160px; height: 160px; padding: 12px; border: 2px solid var(--line); border-radius: var(--radius-s); resize: vertical; font: inherit; color: inherit; background: #fafaff; transition: border-color .2s; }
textarea:focus { outline: none; border-color: var(--brand); }
textarea.dragover { border-color: var(--brand); background: var(--brand-soft); }
.input-meta { display: flex; gap: var(--s3); flex-wrap: wrap; margin-top: var(--s2); font-size: 0.8rem; color: var(--muted); }
#charCount.over-limit { color: var(--danger); font-weight: 700; }
.file-row { display: flex; gap: var(--s2); margin-top: var(--s3); flex-wrap: wrap; align-items: center; }
.file-status { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; font-size: 0.82rem; color: var(--muted); flex: 1 1 140px; min-width: 0; }
.drop-hint { margin-top: 8px; border: 1px dashed var(--brand); background: var(--brand-soft); color: var(--brand-dark); border-radius: 8px; padding: 10px; text-align: center; font-size: 0.88rem; }

/* ---------- Buttons / action cards ---------- */
button { font: inherit; }
.actions-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s3); }
.action-card { display: flex; flex-direction: column; gap: 4px; text-align: left; padding: var(--s4); background: var(--brand); color: #fff; border: 2px solid var(--brand); border-radius: var(--radius); cursor: pointer; transition: transform .08s, background .2s; min-width: 0; }
.action-card:last-child { background: #047857; border-color: #047857; }
.action-card:hover:not(:disabled) { filter: brightness(.94); }
.action-card:disabled { opacity: .55; cursor: not-allowed; }
.action-icon { font-size: 1.4rem; }
.action-title { font-weight: 700; font-size: 1rem; }
.action-desc { font-size: 0.85rem; opacity: .9; }
.btn-group { display: none; }
.ghost-btn { flex: 0 0 auto; padding: 8px 12px; font-size: 0.85rem; background: #f3f4f6; color: var(--ink); border: 1px solid var(--line); border-radius: 8px; cursor: pointer; white-space: nowrap; }
.ghost-btn:hover:not(:disabled) { background: #e5e7eb; }
.ghost-btn:disabled { opacity: .5; cursor: not-allowed; }
.ghost-btn.small { padding: 5px 10px; font-size: 0.78rem; }
.filter-btn.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.action-row { display: flex; gap: var(--s2); flex-wrap: wrap; align-items: center; justify-content: flex-end; }
.action-row.wrap, .section-head.wrap { flex-wrap: wrap; }

/* ---------- Status / error ---------- */
.status-bar { display: flex; align-items: center; gap: 10px; padding: 12px; background: var(--brand-soft); border: 1px solid #c4b5fd; border-radius: var(--radius-s); color: var(--brand-dark); font-size: 0.92rem; }
.mini-spinner { width: 16px; height: 16px; border: 3px solid #c4b5fd; border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; flex-shrink: 0; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { padding: 12px; background: var(--danger-soft); border: 1px solid #fca5a5; border-left: 5px solid var(--danger); border-radius: 6px; color: #7f1d1d; font-size: 0.92rem; overflow-wrap: anywhere; }

/* ---------- Output ---------- */
.output-section { scroll-margin-top: 70px; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: var(--s2); margin-bottom: var(--s3); flex-wrap: wrap; }
.section-head h2 { margin: 0; font-size: 1.1rem; }
.output-box { padding: 15px; background: #f8fafc; border: 1px solid var(--line); border-left: 5px solid var(--brand); border-radius: 8px; line-height: 1.6; min-width: 0; }
.output-box.raw { border-left-color: #d97706; }
.output-box h1, .output-box h2, .output-box h3, .output-box h4 { margin: .7em 0 .35em; }
.output-box ul, .output-box ol { margin: .3em 0; padding-left: 1.4em; }
.output-box li { margin: .15em 0; }
.output-box p { margin: .4em 0; }
.output-box code { background: #e5e7eb; padding: 1px 5px; border-radius: 4px; font-size: .88em; overflow-wrap: anywhere; }
.output-box pre { background: #111827; color: #e5e7eb; padding: 10px; border-radius: 8px; overflow-x: auto; }
.output-box pre code { background: none; padding: 0; color: inherit; }
.no-cards { padding: 12px; margin-bottom: 12px; background: var(--warn-soft); border: 1px solid #fde68a; border-radius: 8px; color: #713f12; font-size: .9rem; }
.raw-details { margin-top: var(--s3); }
.raw-details summary { cursor: pointer; color: var(--muted); font-size: .88rem; padding: 6px 0; }

/* ---------- Flashcards ---------- */
.progress { height: 8px; background: #e5e7eb; border-radius: 999px; overflow: hidden; margin-bottom: var(--s3); }
.progress-bar { height: 100%; width: 0; background: var(--brand); transition: width .25s; }
.card-viewer { margin-top: 10px; }
.flashcard { perspective: 1200px; min-height: 230px; cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent; }
.flashcard:focus-visible { outline: 3px solid var(--brand); outline-offset: 3px; }
.flashcard-inner { position: relative; width: 100%; min-height: 230px; transition: transform .5s; transform-style: preserve-3d; display: grid; }
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.flashcard-face { grid-area: 1/1; backface-visibility: hidden; -webkit-backface-visibility: hidden; display: flex; align-items: center; justify-content: center; padding: 20px; border-radius: 12px; text-align: center; font-size: clamp(0.95rem, 2vw + .8rem, 1.05rem); overflow-y: auto; overflow-wrap: anywhere; min-height: 230px; max-height: 340px; }
.flashcard-face.front { background: var(--brand); color: #fff; }
.flashcard-face.back { background: #059669; color: #fff; transform: rotateY(180deg); }
.flip-hint { text-align: center; color: var(--muted); font-size: .78rem; margin: 8px 0 0; }
.card-controls { display: flex; justify-content: center; align-items: center; gap: 16px; margin-top: 12px; flex-wrap: wrap; }
.card-controls button { padding: 8px 18px; font-size: .9rem; background: var(--brand); color: #fff; border: none; border-radius: 8px; cursor: pointer; }
.card-controls button:hover:not(:disabled) { background: var(--brand-dark); }
.card-controls button:disabled { opacity: .5; cursor: not-allowed; }
#cardPosition { font-size: .9rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--s3); margin-top: var(--s3); }
.mini-card { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #fff; min-width: 0; }
.mini-card .q { background: var(--brand-soft); padding: 10px; font-size: .85rem; font-weight: 600; overflow-wrap: anywhere; }
.mini-card .a { padding: 10px; font-size: .85rem; color: var(--muted); overflow-wrap: anywhere; }

/* ---------- History ---------- */
#historySearch { padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; font-size: .85rem; min-width: 140px; flex: 1 1 140px; max-width: 220px; }
.filter-row { display: flex; gap: 6px; flex-wrap: wrap; }
.history-list { list-style: none; padding: 0; margin: 0; max-height: 300px; overflow-y: auto; display: flex; flex-direction: column; gap: var(--s2); }
.history-item { display: flex; align-items: center; gap: 10px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: #fff; min-width: 0; }
.history-item:hover { background: #fafaff; }
.badge { flex-shrink: 0; padding: 3px 8px; border-radius: 999px; font-size: .72rem; font-weight: 700; color: #fff; }
.badge-summary { background: var(--brand); } .badge-cards { background: #059669; }
.history-text { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .88rem; }
.history-time { flex-shrink: 0; font-size: .72rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.history-del { flex-shrink: 0; border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 3px 8px; cursor: pointer; font-size: .78rem; }
.history-del:hover { background: var(--danger-soft); }

/* ---------- Trust / footer ---------- */
.trust { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--s3); margin-top: var(--s4); }
.trust-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: var(--s3); display: flex; flex-direction: column; gap: 4px; font-size: .88rem; min-width: 0; }
.trust-card span { color: var(--muted); font-size: .83rem; }
footer { margin-top: var(--s5); text-align: center; }
footer p { color: var(--muted); font-size: .8rem; margin: 0; overflow-wrap: anywhere; }

/* ---------- Responsive ---------- */
@media (max-width: 768px) {
    .steps { grid-template-columns: 1fr 1fr; }
    .trust { grid-template-columns: 1fr; }
    .nav-links { display: none; position: absolute; top: 100%; left: 0; right: 0; background: #fff; border-bottom: 1px solid var(--line); flex-direction: column; padding: 8px 16px 12px; }
    .nav-links.open { display: flex; }
    .nav-toggle { display: block; }
    .navbar { position: sticky; }
    #historySearch { max-width: none; }
}
@media (max-width: 480px) {
    .container { padding: var(--s4) var(--s3) var(--s5); }
    .card { padding: var(--s3); }
    .actions-grid { grid-template-columns: 1fr; }
    .steps { grid-template-columns: 1fr; }
    .section-head { align-items: flex-start; flex-direction: column; }
    .action-row { justify-content: flex-start; }
    .flashcard, .flashcard-inner, .flashcard-face { min-height: 200px; }
    .health #healthText { display: none; }
    .hero { padding-top: var(--s4); }
}
@media (min-width: 1024px) { .container { padding-left: 24px; padding-right: 24px; } }
@media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    .flashcard-inner { transition: none; }
    .mini-spinner { animation: none; }
}
