:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --text: #24282b;
  --muted: #6b7175;
  --border: #e2ddd3;
  --accent: #4f7a6a;
  --accent-soft: #e3ece7;
  --accent-text: #ffffff;
  --danger: #b4534a;
  --shadow: 0 1px 2px rgba(30, 35, 40, .05), 0 8px 24px rgba(30, 35, 40, .07);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17191b;
    --surface: #212427;
    --surface-2: #2a2e31;
    --text: #e8e6e1;
    --muted: #9aa0a4;
    --border: #33373b;
    --accent: #7fb19d;
    --accent-soft: #263630;
    --accent-text: #10201a;
    --danger: #e08a80;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

.banner {
  background: var(--danger);
  color: #fff;
  padding: 10px 16px;
  text-align: center;
  font-size: 14px;
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 28px 18px;
}

.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 32px; padding: 0 6px; }
.brand h1 { font-size: 20px; }
.brand-mark {
  width: 22px; height: 16px; border-radius: 4px;
  background: var(--accent);
  box-shadow: 4px -4px 0 var(--accent-soft);
}

.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 6px; margin-bottom: 8px;
}
.sidebar-head h2 {
  font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted);
}

.set-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.set-list button {
  width: 100%;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 10px 12px;
  border: 0; border-radius: 10px;
  background: transparent; color: var(--text);
  font: inherit; text-align: left; cursor: pointer;
}
.set-list button:hover { background: var(--surface-2); }
.set-list button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.set-list .count {
  font-size: 12px; color: var(--muted); font-weight: 500;
  background: var(--surface-2); border-radius: 999px; padding: 1px 8px;
}
.set-list button.active .count { background: var(--surface); }
.set-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main { padding: 40px 48px 64px; max-width: 920px; width: 100%; }

/* Buttons */
.btn {
  font: inherit; font-weight: 500;
  border-radius: 10px; border: 1px solid transparent;
  padding: 9px 16px; cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
.btn-small { padding: 5px 10px; font-size: 14px; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { color: var(--danger); }
.btn:disabled { opacity: .4; cursor: default; }
.btn:focus-visible, .flashcard:focus-visible, .set-list button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

/* Set header */
.set-header {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap;
  margin-bottom: 28px;
}
.set-header h2 { font-size: 28px; }
.set-header p { margin: 4px 0 0; }
.set-actions { display: flex; gap: 8px; }

/* Flashcard */
.study { margin-bottom: 48px; }
.card-scene { perspective: 1400px; }

.flashcard {
  position: relative;
  display: block;
  width: 100%;
  height: 300px;
  padding: 0; border: 0; background: none;
  font: inherit; color: inherit; cursor: pointer;
  transform-style: preserve-3d;
  transition: transform .55s cubic-bezier(.2, .7, .2, 1);
}
.flashcard.flipped { transform: rotateY(180deg); }

.face {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 32px 40px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  text-align: center;
}
.face.back { transform: rotateY(180deg); background: var(--accent-soft); }
.face-label {
  position: absolute; top: 16px; left: 20px;
  font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted);
}
.face-text {
  font-size: 26px; line-height: 1.4; max-width: 34ch;
  white-space: pre-wrap; overflow-wrap: anywhere;
  overflow: auto; max-height: 100%;
}
.face.back .face-text { font-size: 20px; }

.hint { text-align: center; font-size: 13px; margin: 14px 0 10px; }
.study-nav { display: flex; justify-content: center; align-items: center; gap: 20px; }
.counter { min-width: 64px; text-align: center; font-variant-numeric: tabular-nums; color: var(--muted); }

/* Card list */
.cards-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.cards-head h3 { font-size: 18px; }

.card-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.card-list li {
  display: grid; grid-template-columns: 1fr 1.4fr auto; gap: 16px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px;
}
.card-list .q { font-weight: 600; overflow-wrap: anywhere; }
.card-list .a { color: var(--muted); font-size: 14px; overflow-wrap: anywhere; }
.card-list .row-actions { display: flex; gap: 6px; }

.empty-state { text-align: center; padding: 64px 16px; }
.empty-state.small { padding: 48px 16px; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }
.empty-state p { margin: 8px 0 0; }

/* Dialogs */
.dialog {
  border: 0; border-radius: var(--radius);
  padding: 0; width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.dialog::backdrop { background: rgba(20, 24, 28, .4); }
.dialog form { padding: 24px; display: grid; gap: 16px; }
.dialog h3 { font-size: 20px; }
.dialog label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.dialog textarea, .dialog input {
  font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; resize: vertical;
}
.dialog textarea:focus, .dialog input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* Responsive */
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); padding: 20px 16px; }
  .brand { margin-bottom: 16px; }
  .main { padding: 24px 16px 48px; }
  .flashcard { height: 240px; }
  .face { padding: 28px 20px; }
  .face-text { font-size: 21px; }
  .face.back .face-text { font-size: 17px; }
  .card-list li { grid-template-columns: 1fr; gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .flashcard { transition: none; }
}
