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

:root {
  --bg: #F7F6F2;
  --surface: #FFFFFF;
  --surface2: #F0EFE9;
  --border: #E0DED7;
  --border-strong: #C8C6BE;
  --text: #1A1917;
  --text-muted: #6B6963;
  --text-hint: #9C9A94;
  --accent: #1A1917;
  --accent-light: #F0EFE9;
  --green: #3B6D11;
  --green-bg: #EAF3DE;
  --red: #A32D2D;
  --red-bg: #FCEBEB;
  --amber: #854F0B;
  --amber-bg: #FAEEDA;
  --radius: 10px;
  --radius-lg: 14px;
  --card-h: 300px;
  --transition: 0.18s ease;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1A1917;
    --surface: #232220;
    --surface2: #2C2C2A;
    --border: #3A3936;
    --border-strong: #4E4D49;
    --text: #F0EEE8;
    --text-muted: #A8A59E;
    --text-hint: #6E6C67;
    --accent: #F0EEE8;
    --accent-light: #2C2C2A;
    --green: #C0DD97;
    --green-bg: #173404;
    --red: #F09595;
    --red-bg: #501313;
    --amber: #FAC775;
    --amber-bg: #412402;
  }
}

body {
  font-family: 'Geist Mono', monospace;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.6;
}

/* ——— Layout ——— */
.app  { display: flex; height: 100vh; overflow: hidden; }
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

/* ——— Sidebar ——— */
.sidebar {
  width: 240px;
  min-width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 18px 14px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.logo {
  font-family: 'Instrument Serif', serif;
  font-size: 20px;
  color: var(--text);
  letter-spacing: -0.3px;
}

.sidebar-footer {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ——— Deck list ——— */
.deck-list-wrap {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.deck-list-wrap::-webkit-scrollbar { width: 4px; }
.deck-list-wrap::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 2px; }

.deck-item {
  padding: 9px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
  transition: background var(--transition), border-color var(--transition);
  margin-bottom: 2px;
}

.deck-item:hover { background: var(--surface2); }
.deck-item.active { background: var(--accent-light); border-color: var(--border-strong); }

.deck-item-name {
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
}

.deck-item-meta {
  font-size: 11px;
  color: var(--text-hint);
}

/* ——— Top bar ——— */
.topbar {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.deck-title-wrap { display: flex; flex-direction: column; gap: 2px; min-width: 0; }

.topbar-tabs { display: flex; gap: 2px; }

.tab {
  padding: 6px 14px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  font-family: 'Geist Mono', monospace;
  font-size: 12px;
  cursor: pointer;
  transition: all var(--transition);
}

.tab:hover  { color: var(--text); background: var(--surface2); }
.tab.active { color: var(--text); background: var(--accent-light); border-color: var(--border); }

.topbar-right { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.sep { width: 1px; height: 20px; background: var(--border); flex-shrink: 0; }

/* ——— Buttons ——— */
.btn {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 11px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-family: 'Geist Mono', monospace;
  font-size: 12px;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
  width: 100%;
  text-align: left;
  white-space: nowrap;
}

.btn:hover  { background: var(--surface2); border-color: var(--border-strong); }
.btn:active { opacity: 0.8; }

.btn.primary { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.btn.primary:hover { opacity: 0.88; }

.btn.danger  { border-color: var(--red); color: var(--red); }
.btn.danger:hover { background: var(--red-bg); }

.btn svg {
  width: 14px; height: 14px; flex-shrink: 0;
  stroke: currentColor; fill: none; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

/* ——— Deck name input ——— */
.deck-name-input {
  background: transparent;
  border: none;
  font-family: 'Instrument Serif', serif;
  font-size: 19px;
  color: var(--text);
  letter-spacing: -0.3px;
  width: 220px;
  padding: 0;
  outline: none;
}
.deck-name-input::placeholder { color: var(--border-strong); }

.deck-meta { display: flex; gap: 6px; align-items: center; }

.badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  background: var(--surface2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

/* ——— No deck / empty states ——— */
.no-deck-view {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}

.empty-state .big-num {
  font-family: 'Instrument Serif', serif;
  font-size: 56px;
  color: var(--border-strong);
  line-height: 1;
  margin-bottom: 12px;
}

.empty-state p { font-size: 13px; margin-bottom: 16px; line-height: 1.7; }

/* ——— Study view ——— */
.study-view {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  gap: 24px;
}

.study-progress { display: flex; gap: 20px; align-items: center; }

.progress-label { font-size: 12px; color: var(--text-muted); }

.progress-bar-wrap {
  width: 180px; height: 4px;
  background: var(--border); border-radius: 2px; overflow: hidden;
}

.progress-bar-fill {
  height: 100%; background: var(--text);
  border-radius: 2px; transition: width 0.3s ease;
}

/* ——— Flip card ——— */
.flip-scene {
  width: 100%; max-width: 580px;
  height: var(--card-h);
  perspective: 1000px; cursor: pointer;
}

.flip-card {
  width: 100%; height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.4, 0, 0.2, 1);
}

.flip-card.flipped { transform: rotateY(180deg); }

.flip-face {
  position: absolute; inset: 0;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 32px 40px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--surface);
  text-align: center; user-select: none;
}

.flip-face.back { transform: rotateY(180deg); }

.face-label {
  font-size: 10px; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--text-hint);
  margin-bottom: 16px;
}

.face-content {
  font-family: 'Instrument Serif', serif;
  font-size: 26px; line-height: 1.4; color: var(--text);
}

.face-hint {
  position: absolute; bottom: 16px;
  font-size: 11px; color: var(--text-hint);
}

/* ——— Study controls ——— */
.study-controls { display: flex; gap: 10px; align-items: center; }

.ctrl-btn {
  display: flex; align-items: center; gap: 7px;
  padding: 10px 18px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: 'Geist Mono', monospace;
  font-size: 12px; cursor: pointer;
  transition: all var(--transition);
}

.ctrl-btn:hover { background: var(--surface2); border-color: var(--border-strong); }
.ctrl-btn.good  { border-color: var(--green); color: var(--green); }
.ctrl-btn.good:hover  { background: var(--green-bg); }
.ctrl-btn.bad   { border-color: var(--red); color: var(--red); }
.ctrl-btn.bad:hover   { background: var(--red-bg); }
.ctrl-btn.skip  { border-color: var(--amber); color: var(--amber); }
.ctrl-btn.skip:hover  { background: var(--amber-bg); }
.ctrl-btn svg {
  width: 14px; height: 14px; flex-shrink: 0;
  stroke: currentColor; fill: none; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}

.study-rate-badges { display: flex; gap: 8px; justify-content: center; }

.rate-badge {
  font-size: 11px; padding: 3px 10px;
  border-radius: 20px; border: 1px solid;
}

.rate-badge.known    { background: var(--green-bg); color: var(--green); border-color: var(--green); }
.rate-badge.learning { background: var(--amber-bg); color: var(--amber); border-color: var(--amber); }
.rate-badge.unknown  { background: var(--red-bg);   color: var(--red);   border-color: var(--red); }

.study-complete { text-align: center; max-width: 400px; }
.study-complete h2 { font-family: 'Instrument Serif', serif; font-size: 32px; margin-bottom: 10px; }
.study-complete p  { color: var(--text-muted); margin-bottom: 20px; }

/* ——— Edit view ——— */
.edit-view { flex: 1; overflow-y: auto; padding: 20px 24px; }

.edit-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px; max-width: 900px;
}

@media (max-width: 700px) {
  .edit-grid { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}

.card-editor {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px; display: flex;
  flex-direction: column; gap: 8px;
  position: relative;
}

.card-editor-header { display: flex; align-items: center; gap: 8px; }
.card-num { font-size: 11px; color: var(--text-hint); }

.card-editor textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 11px;
  font-family: 'Geist Mono', monospace;
  font-size: 12px; color: var(--text);
  resize: none; min-height: 65px;
  transition: border-color var(--transition);
  line-height: 1.5;
}

.card-editor textarea:focus { outline: none; border-color: var(--border-strong); }

.card-editor-labels { display: flex; gap: 6px; }

.field-label {
  font-size: 10px; letter-spacing: 0.07em;
  text-transform: uppercase; color: var(--text-hint);
  flex: 1; padding-bottom: 2px;
}

.del-btn {
  position: absolute; top: 10px; right: 10px;
  width: 24px; height: 24px; border-radius: 6px;
  border: 1px solid transparent; background: transparent;
  color: var(--text-hint); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all var(--transition);
}

.del-btn:hover { background: var(--red-bg); border-color: var(--red); color: var(--red); }
.del-btn svg {
  width: 13px; height: 13px; stroke: currentColor;
  fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

.add-card-btn {
  display: flex; align-items: center; justify-content: center;
  gap: 8px; padding: 14px; border-radius: var(--radius-lg);
  border: 1px dashed var(--border-strong); background: transparent;
  color: var(--text-muted); font-family: 'Geist Mono', monospace;
  font-size: 12px; cursor: pointer;
  transition: all var(--transition); width: 100%;
}

.add-card-btn:hover { background: var(--surface2); color: var(--text); }
.add-card-btn svg {
  width: 14px; height: 14px; stroke: currentColor;
  fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

/* ——— Stats view ——— */
.stats-view { flex: 1; overflow-y: auto; padding: 24px 28px; }

.stats-grid {
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 12px; max-width: 680px; margin-bottom: 24px;
}

.stat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 14px 16px;
}

.stat-label { font-size: 11px; color: var(--text-muted); margin-bottom: 6px; }
.stat-value { font-family: 'Instrument Serif', serif; font-size: 30px; color: var(--text); line-height: 1; }
.stat-sub   { font-size: 11px; color: var(--text-hint); margin-top: 4px; }

.section-title {
  font-size: 11px; letter-spacing: 0.07em;
  text-transform: uppercase; color: var(--text-hint); margin-bottom: 10px;
}

.donut-row {
  display: flex; align-items: center; gap: 28px; max-width: 380px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 18px 22px; margin-bottom: 20px;
}

.donut-labels { display: flex; flex-direction: column; gap: 8px; }
.donut-label-row { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-muted); }
.donut-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

/* ——— Keyboard hints ——— */
kbd {
  display: inline-block; background: var(--surface2);
  border: 1px solid var(--border); border-radius: 4px;
  padding: 1px 5px; font-size: 10px; color: var(--text-muted);
  font-family: 'Geist Mono', monospace;
}

.kb-hints { display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; }
.kb-hint-item { font-size: 11px; color: var(--text-hint); display: flex; align-items: center; gap: 5px; }

/* ——— Shuffle toggle ——— */
.toggle-wrap {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--text-muted);
  cursor: pointer; user-select: none;
}

.toggle-track {
  width: 32px; height: 18px; border-radius: 9px;
  background: var(--border-strong); position: relative;
  transition: background var(--transition); flex-shrink: 0;
}

.toggle-track.on { background: var(--text); }

.toggle-thumb {
  position: absolute; top: 2px; left: 2px;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--bg); transition: left var(--transition);
}

.toggle-track.on .toggle-thumb { left: 16px; }

/* ——— Modal ——— */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.35);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px 28px;
  max-width: 340px; width: 90%;
  display: flex; flex-direction: column; gap: 18px;
}

.modal-msg { font-size: 13px; color: var(--text); line-height: 1.6; }

.modal-btns { display: flex; gap: 8px; justify-content: flex-end; }
.modal-btns .btn { width: auto; }

/* ——— Toast ——— */
.toast {
  position: fixed; bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--text); color: var(--bg);
  font-size: 12px; padding: 9px 18px;
  border-radius: 20px; opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100; pointer-events: none;
}

.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ——— Misc ——— */
#fileInput { display: none; }
