:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --ink: #1d2233;
  --muted: #6b7083;
  --line: #e4e1d8;
  --accent: #3446c9;
  --accent-soft: #e8ebfb;
  --good: #1f8a5b;
  --good-soft: #e3f4ec;
  --bad: #c2413b;
  --bad-soft: #fbe9e7;
  --warn: #b7791f;
  --warn-soft: #fbf1df;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 24, 40, .06), 0 6px 20px rgba(20, 24, 40, .06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161f;
    --surface: #1d2030;
    --ink: #eceef6;
    --muted: #9ca1b6;
    --line: #2e3246;
    --accent: #8796ff;
    --accent-soft: #262c4d;
    --good: #4cc38a;
    --good-soft: #193427;
    --bad: #ff7a72;
    --bad-soft: #3a1f1f;
    --warn: #f0b75a;
    --warn-soft: #3a2f1a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); line-height: 1.5; min-height: 100vh; display: flex; flex-direction: column; }
main { width: 100%; max-width: 980px; margin: 0 auto; padding: 24px 20px 48px; flex: 1; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 12px; }
h1 { font-size: clamp(1.6rem, 3vw, 2.2rem); letter-spacing: -.02em; }
h2 { font-size: 1.4rem; letter-spacing: -.01em; }
h3 { font-size: 1.05rem; margin-top: 28px; }
code { background: var(--accent-soft); padding: 1px 6px; border-radius: 6px; font-size: .9em; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 20px; padding: 10px 20px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); }
.brand-mark { background: var(--accent); color: #fff; font-weight: 800; font-size: .85rem; padding: 6px 8px; border-radius: 9px; }
.brand-text { font-weight: 700; display: flex; flex-direction: column; line-height: 1.1; }
.brand-text small { font-weight: 500; color: var(--muted); font-size: .75rem; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs a { text-decoration: none; color: var(--muted); padding: 7px 12px; border-radius: 999px; font-size: .92rem; font-weight: 500; }
.tabs a:hover { color: var(--ink); background: var(--surface); }
.tabs a.active { color: var(--accent); background: var(--accent-soft); }

/* Buttons */
.btn { appearance: none; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; padding: 10px 18px; border-radius: 10px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; transition: transform .08s, background .15s, border-color .15s; }
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.good { background: var(--good-soft); border-color: transparent; color: var(--good); }
.btn.bad { background: var(--bad-soft); border-color: transparent; color: var(--bad); }
.btn.good:hover { border-color: var(--good); }
.btn.bad:hover { border-color: var(--bad); }
.btn.ghost { background: transparent; }
.btn.bad-outline { background: transparent; color: var(--bad); border-color: var(--bad-soft); }
.btn.small { padding: 5px 10px; font-size: .85rem; }
.btn.icon { padding: 10px 12px; }
kbd { font: inherit; font-size: .72rem; opacity: .65; border: 1px solid currentColor; border-radius: 4px; padding: 0 4px; }
.badge { background: rgba(255, 255, 255, .25); padding: 1px 8px; border-radius: 999px; font-size: .85rem; }

/* Start */
.hero { display: grid; grid-template-columns: 1.3fr 1fr; gap: 24px; align-items: center; background: var(--surface); border-radius: var(--radius); padding: 28px; box-shadow: var(--shadow); margin-bottom: 32px; }
.lead { color: var(--muted); font-size: 1.05rem; margin: 0 0 20px; }
.hero-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.hero-stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.hero-stats.wide { grid-template-columns: repeat(4, 1fr); margin: 18px 0 24px; }
.stat { background: var(--bg); border-radius: 12px; padding: 14px; }
.stat b { display: block; font-size: 1.6rem; letter-spacing: -.02em; }
.stat span { color: var(--muted); font-size: .85rem; }
.section-title { margin-top: 8px; }
.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.cat-card { background: var(--surface); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); cursor: pointer; border: 1px solid transparent; text-align: left; font: inherit; color: inherit; }
.cat-card:hover { border-color: var(--accent); }
.cat-card .name { font-weight: 650; }
.cat-card .meta { color: var(--muted); font-size: .82rem; margin: 2px 0 10px; }
.mini-bar { height: 6px; background: var(--bg); border-radius: 99px; overflow: hidden; }
.mini-bar > div { height: 100%; background: var(--good); border-radius: 99px; }

/* Setup */
.setup { background: var(--surface); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow); }
.cat-picker { margin: 14px 0 18px; }
.picker-tools { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.picker-group { margin-bottom: 10px; }
.picker-group > .group-label { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span { display: inline-block; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); cursor: pointer; font-size: .9rem; user-select: none; }
.chip input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.chip small { opacity: .7; }
.options { display: flex; flex-wrap: wrap; gap: 16px; margin-bottom: 20px; }
.options label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); font-weight: 600; }
select, input[type=text] { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; }
select:focus, input[type=text]:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

/* Session */
.session-bar { display: flex; align-items: center; gap: 14px; margin-bottom: 20px; }
.progress { flex: 1; height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); transition: width .25s; }

/* Flashcard */
.card-wrap { perspective: 1400px; }
.flashcard { position: relative; height: 320px; transform-style: preserve-3d; transition: transform .5s cubic-bezier(.3, .7, .2, 1); cursor: pointer; outline: none; }
.flashcard.flipped { transform: rotateY(180deg); }
.flashcard:focus-visible .face { outline: 2px solid var(--accent); }
.face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; background: var(--surface); border-radius: 20px; box-shadow: var(--shadow); display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 28px; text-align: center; }
.face.back { transform: rotateY(180deg); background: linear-gradient(180deg, var(--surface), var(--accent-soft)); }
.card-cat { position: absolute; top: 16px; left: 18px; font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.question .card-cat { position: static; display: block; margin-bottom: 6px; }
.card-text { font-size: clamp(1.4rem, 3.6vw, 2.1rem); font-weight: 700; letter-spacing: -.01em; max-width: 680px; }
.card-sub { margin-top: 12px; color: var(--muted); font-size: 1rem; max-width: 600px; }
.hint { position: absolute; bottom: 14px; font-size: .78rem; color: var(--muted); }
.card-actions { display: flex; justify-content: center; gap: 12px; margin-top: 20px; }
.card-actions .btn.good, .card-actions .btn.bad { min-width: 150px; justify-content: center; }

/* Questions */
.question { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; }
.q-label { color: var(--muted); margin: 0 0 6px; font-size: .92rem; }
.q-text { font-size: clamp(1.3rem, 3vw, 1.8rem); font-weight: 700; margin-bottom: 22px; }
.answers { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.answer { text-align: left; font: inherit; padding: 14px 16px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); cursor: pointer; display: flex; gap: 12px; align-items: flex-start; }
.answer:hover:not(:disabled) { border-color: var(--accent); }
.answer .key { font-weight: 700; color: var(--accent); min-width: 18px; }
.answer.correct { background: var(--good-soft); border-color: var(--good); }
.answer.wrong { background: var(--bad-soft); border-color: var(--bad); }
.answer:disabled { cursor: default; }
.feedback { min-height: 24px; margin: 16px 0 10px; font-weight: 600; }
.feedback.ok { color: var(--good); }
.feedback.err { color: var(--bad); }
.feedback.warn { color: var(--warn); }
.feedback .expected { display: block; font-weight: 500; color: var(--ink); margin-top: 4px; }
#w-form { display: flex; gap: 10px; }
#w-form input { flex: 1; font-size: 1.1rem; padding: 12px 14px; }
.self-grade { display: flex; gap: 10px; margin-bottom: 10px; }

/* Results */
.result { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; text-align: center; }
.score-ring { font-size: 3rem; font-weight: 800; letter-spacing: -.03em; margin: 6px 0; }
.result .actions { display: flex; justify-content: center; gap: 10px; margin: 18px 0; flex-wrap: wrap; }
.mistakes { text-align: left; margin-top: 18px; border-top: 1px solid var(--line); padding-top: 14px; }
.mistakes li { margin: 6px 0; }
.mistakes .your { color: var(--bad); text-decoration: line-through; margin-left: 6px; }

/* Stats */
.stats-table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.stats-table th, .stats-table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--line); font-size: .93rem; }
.stats-table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.stats-table tr:last-child td { border-bottom: none; }
.stats-table .bar-col { width: 30%; }
.right { text-align: right !important; }
.history { list-style: none; padding: 0; margin: 0; }
.history li { background: var(--surface); padding: 10px 14px; border-radius: 10px; margin-bottom: 6px; display: flex; justify-content: space-between; gap: 10px; font-size: .92rem; }
.danger-zone { margin-top: 28px; display: flex; gap: 10px; flex-wrap: wrap; }
.file-btn input { display: none; }

/* Phrases */
.phrase-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.phrase-box { background: var(--surface); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.phrase-box h3 { margin-top: 0; font-size: .95rem; color: var(--accent); }
.phrase-box ul { margin: 0; padding-left: 18px; }
.phrase-box li { margin: 5px 0; cursor: pointer; }
.phrase-box li:hover { color: var(--accent); }

/* Custom words */
.add-form { display: flex; gap: 10px; flex-wrap: wrap; margin: 16px 0 20px; }
.add-form input { flex: 1; min-width: 200px; }
.flash-msg { padding: 10px 14px; border-radius: 10px; margin-bottom: 12px; font-weight: 600; }
.flash-msg.ok { background: var(--good-soft); color: var(--good); }
.flash-msg.err { background: var(--bad-soft); color: var(--bad); }
.empty { color: var(--muted); }

.foot { text-align: center; color: var(--muted); font-size: .8rem; padding: 20px; }

@media (max-width: 720px) {
  .hero { grid-template-columns: 1fr; padding: 20px; }
  .hero-stats.wide { grid-template-columns: repeat(2, 1fr); }
  .answers { grid-template-columns: 1fr; }
  .flashcard { height: 280px; }
  .card-actions .btn.good, .card-actions .btn.bad { min-width: 0; flex: 1; }
  .stats-table .bar-col, .stats-table td:nth-child(5) { display: none; }
  .topbar { gap: 8px; }
  kbd { display: none; }
}

/* Konto użytkownika */
.user-box { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.user-box form { margin: 0; }
.user-name { font-weight: 600; font-size: .9rem; }
.sync-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--line); display: inline-block; }
.sync-dot.ok { background: var(--good); }
.sync-dot.pending { background: var(--warn); }
.sync-dot.err { background: var(--bad); }
.ranking tr.me td { background: var(--accent-soft); font-weight: 600; }

/* Logowanie */
.auth-page main.auth-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.auth-card { background: var(--surface); border-radius: 20px; box-shadow: var(--shadow); padding: 28px; width: 100%; max-width: 400px; }
.auth-brand { margin-bottom: 20px; }
.auth-switch { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border-radius: 12px; padding: 4px; margin-bottom: 18px; }
.auth-switch a { text-align: center; padding: 8px; border-radius: 9px; text-decoration: none; color: var(--muted); font-weight: 600; font-size: .92rem; }
.auth-switch a.active { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label { display: flex; flex-direction: column; gap: 5px; font-size: .88rem; font-weight: 600; }
.auth-form label .muted { font-weight: 400; }
.auth-form input[type=text], .auth-form input[type=password] { font-size: 1rem; padding: 11px 13px; }
input[type=password] { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; }
input[type=password]:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.auth-form label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; }
.auth-form label.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.btn.block { width: 100%; justify-content: center; padding: 12px; }
.small-note { font-size: .82rem; margin: 16px 0 0; text-align: center; }

@media (max-width: 720px) {
  .user-box { margin-left: 0; width: 100%; justify-content: flex-end; order: -1; position: absolute; top: 12px; right: 16px; width: auto; }
  .topbar { position: sticky; }
  .user-name { max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
