/* ── Variables ─────────────────────────────────────────────── */
:root {
  --bg:              #12100d;
  --sidebar-bg:      #1a1713;
  --card-bg:         #211e17;
  --card-back-bg:    #1a2118;
  --surface:         #272320;
  --surface-2:       #302c28;
  --border:          #332f28;
  --border-2:        #403b33;
  --accent:          #c8912a;
  --accent-hover:    #daa838;
  --accent-muted:    rgba(200, 145, 42, 0.13);
  --text:            #ede4d3;
  --text-muted:      #7a6e5a;
  --text-dim:        #403c34;
  --danger:          #c45a40;
  --danger-hover:    #d96a50;
  --success:         #5a8e58;
  --radius:          6px;
  --radius-lg:       14px;
  --shadow:          0 4px 30px rgba(0,0,0,0.55);
  --shadow-card:     0 12px 60px rgba(0,0,0,0.65);
  --t:               0.18s ease;
  --font-ui:         'IBM Plex Mono', monospace;
  --font-card:       'Cormorant Garamond', Georgia, serif;
}

[data-theme="light"] {
  --bg:              #f0ead8;
  --sidebar-bg:      #e8e0cc;
  --card-bg:         #fffcf5;
  --card-back-bg:    #f5f0e5;
  --surface:         #ddd8c8;
  --surface-2:       #d0c9b8;
  --border:          #ccc4b0;
  --border-2:        #bfb8a8;
  --accent:          #a06c18;
  --accent-hover:    #b87c22;
  --accent-muted:    rgba(160, 108, 24, 0.10);
  --text:            #1c1710;
  --text-muted:      #6a5e48;
  --text-dim:        #b0a890;
  --shadow:          0 4px 20px rgba(0,0,0,0.12);
  --shadow-card:     0 12px 40px rgba(0,0,0,0.18);
}

/* ── Reset ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }

body {
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  display: flex;
  transition: background var(--t), color var(--t);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* ── Sidebar ────────────────────────────────────────────────── */
#sidebar {
  width: 232px;
  min-width: 232px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  transition: background var(--t);
}

/* Only the tree list area scrolls — header and footer stay pinned */
#sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 16px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-user {
  padding: 6px 16px 4px;
  flex-shrink: 0;
}

.sidebar-username {
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
}

.app-title {
  font-family: var(--font-card);
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--text);
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 1rem;
  padding: 4px 7px;
  border-radius: var(--radius);
  line-height: 1;
  transition: color var(--t), background var(--t);
}
.icon-btn:hover { color: var(--text); background: var(--surface); }

.sidebar-section { padding: 10px 0 2px; }

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 16px 5px;
}

.section-label {
  font-size: 0.62rem;
  letter-spacing: 0.13em;
  color: var(--text-muted);
  user-select: none;
}

.add-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 1.05rem;
  line-height: 1;
  width: 18px; height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: color var(--t), background var(--t);
}
.add-btn:hover { color: var(--accent); background: var(--accent-muted); }

.tree-list { padding: 0 8px 4px; }

.tree-item {
  display: flex;
  align-items: center;
  padding: 6px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  gap: 4px;
  font-size: 0.76rem;
  color: var(--text-muted);
  transition: color var(--t), background var(--t);
  min-height: 30px;
}
.tree-item:hover { color: var(--text); background: var(--surface); }
.tree-item.active { color: var(--accent); background: var(--accent-muted); }

.tree-item-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-item-count { font-size: 0.6rem; color: var(--text-dim); flex-shrink: 0; }
.tree-item.active .tree-item-count { color: var(--accent); opacity: 0.7; }

.tree-item-actions { display: none; gap: 1px; flex-shrink: 0; }
.tree-item:hover .tree-item-actions,
.tree-item.active .tree-item-actions { display: flex; }

.tree-action-btn {
  background: none; border: none; cursor: pointer;
  color: var(--text-dim); font-size: 0.72rem;
  padding: 2px 4px; border-radius: 3px;
  transition: color var(--t), background var(--t); line-height: 1;
}
.tree-action-btn:hover { color: var(--text-muted); background: var(--surface-2); }
.tree-action-btn.danger:hover { color: var(--danger); }

.sidebar-footer {
  padding: 12px 8px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex-shrink: 0;
}

.sidebar-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 10px;
  font-family: var(--font-ui);
  font-size: 0.7rem;
  color: var(--text-muted);
  cursor: pointer;
  text-align: left;
  transition: all var(--t);
  width: 100%;
}
.sidebar-btn:hover { color: var(--text); border-color: var(--border-2); background: var(--surface); }
.sidebar-btn.accent { border-color: var(--accent); color: var(--accent); }
.sidebar-btn.accent:hover { background: var(--accent-muted); border-color: var(--accent-hover); }

/* ── Main area ──────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 28px 24px;
  height: 100vh;
  overflow: hidden;
}

.progress-bar-container {
  width: 100%;
  max-width: 640px;
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 22px;
}
.progress-label { font-size: 0.67rem; color: var(--text-muted); white-space: nowrap; min-width: 110px; }
.progress-track { flex: 1; height: 2px; background: var(--border); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); border-radius: 2px; transition: width 0.45s cubic-bezier(0.4,0,0.2,1); }

/* ── Card flip scene ────────────────────────────────────────── */
#card-scene {
  width: 100%;
  max-width: 640px;
  height: 320px;
  perspective: 1400px;
  cursor: pointer;
  margin-bottom: 22px;
  flex-shrink: 0;
}

#card-inner {
  width: 100%; height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.55s cubic-bezier(0.4,0,0.2,1);
}
#card-inner.flipped { transform: rotateY(180deg); }

.card-face {
  position: absolute; inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--radius-lg);
  padding: 22px 28px 18px;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-card);
  overflow: hidden;
  transition: background var(--t), border-color var(--t);
}
.card-face.front { background: var(--card-bg); }
.card-face.back  { background: var(--card-back-bg); border-color: var(--border-2); transform: rotateY(180deg); }

.card-badge {
  font-size: 0.58rem; letter-spacing: 0.14em;
  color: var(--text-muted); margin-bottom: 8px; user-select: none; flex-shrink: 0;
}
.back-badge { color: var(--success); opacity: 0.7; }

.card-text {
  font-family: var(--font-card);
  font-size: 1.55rem;
  line-height: 1.45;
  color: var(--text);
  flex-shrink: 0;
  text-align: center;
  word-break: break-word;
  white-space: pre-wrap;
}

/* ── Per-side media row inside card ─────────────────────────── */
.card-media-row {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  flex: 1;
  min-height: 0;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.card-media-row:empty { display: none; }

.card-media-row img {
  max-height: 150px;
  max-width: 48%;
  object-fit: contain;
  border-radius: 6px;
  flex-shrink: 0;
}
.card-media-row video {
  max-height: 150px;
  max-width: 48%;
  border-radius: 6px;
  background: #000;
  outline: none;
  flex-shrink: 0;
}

/* shrink text when media present so everything fits */
.card-face.has-media .card-text { font-size: 1.1rem; margin-bottom: 4px; }

/* ── Card controls ──────────────────────────────────────────── */
.card-controls { display: flex; gap: 10px; margin-bottom: 14px; }

.ctrl-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 22px;
  font-family: var(--font-ui);
  font-size: 0.72rem;
  color: var(--text-muted);
  cursor: pointer;
  min-width: 100px;
  transition: all var(--t);
}
.ctrl-btn:hover { color: var(--text); border-color: var(--border-2); }
.ctrl-btn.primary {
  background: var(--accent); border-color: var(--accent);
  color: #12100d; font-weight: 500;
}
.ctrl-btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.hint-bar { display: flex; gap: 22px; font-size: 0.62rem; color: var(--text-dim); user-select: none; }

/* ── Memorise overlay ───────────────────────────────────────── */
#memorise-overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.2s ease;
  overflow-y: auto;
}
#memorise-overlay.hidden { display: none; }

#memorise-panel {
  width: 100%;
  max-width: 700px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 28px 24px 32px;
  gap: 0;
}

.mem-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
}
.mem-title { font-family: var(--font-card); font-size: 1.15rem; color: var(--text-muted); }

.mem-progress-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}
.mem-progress-label { font-size: 0.67rem; color: var(--text-muted); white-space: nowrap; min-width: 80px; }
.mem-score-badges { display: flex; gap: 8px; flex-shrink: 0; }
.score-badge { font-size: 0.62rem; padding: 3px 9px; border-radius: 20px; border: 1px solid; }
.score-badge.correct { color: var(--success); border-color: var(--success); }
.score-badge.wrong   { color: var(--danger);  border-color: var(--danger);  }

#mem-card-area {
  width: 100%;
  max-width: 640px;
  min-height: 180px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px 28px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  margin-bottom: 22px;
  box-shadow: var(--shadow-card);
  transition: background var(--t), border-color var(--t);
  overflow: hidden;
}
#mem-card-area.back-showing { background: var(--card-back-bg); border-color: var(--border-2); }
#mem-card-area.round-end    { background: var(--surface); border-color: var(--border-2); }

.mem-badge {
  font-size: 0.58rem; letter-spacing: 0.14em;
  color: var(--text-muted); margin-bottom: 12px; align-self: flex-start; user-select: none;
}
.mem-badge.back { color: var(--success); opacity: 0.7; }

.mem-card-text {
  font-family: var(--font-card);
  font-size: 1.55rem;
  line-height: 1.45;
  color: var(--text);
  word-break: break-word;
  white-space: pre-wrap;
  text-align: center;
  width: 100%;
}

/* media row inside memorise card */
#mem-card-area .card-media-row { max-height: 180px; }
#mem-card-area .card-media-row img,
#mem-card-area .card-media-row video { max-height: 175px; }

#mem-controls { display: flex; gap: 10px; margin-bottom: 14px; }

.mem-judge {
  display: flex;
  gap: 16px;
  margin-bottom: 14px;
  animation: slideUp 0.18s ease;
}
.mem-judge.hidden { display: none; }

.judge-btn {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 32px;
  font-family: var(--font-ui); font-size: 0.78rem; font-weight: 500;
  border-radius: var(--radius); cursor: pointer; border: 2px solid;
  background: none; transition: all 0.15s ease;
  min-width: 130px; justify-content: center;
}
.judge-btn .judge-key {
  font-size: 0.65rem; opacity: 0.7;
  border: 1px solid currentColor; border-radius: 3px; padding: 1px 5px;
}
.wrong-btn { border-color: var(--danger); color: var(--danger); }
.wrong-btn:hover { background: var(--danger); color: #fff; }
.right-btn { border-color: var(--success); color: var(--success); }
.right-btn:hover { background: var(--success); color: #fff; }

.mem-hint-bar {
  display: flex; gap: 22px;
  font-size: 0.62rem; color: var(--text-dim); user-select: none;
}

/* ── Modal ──────────────────────────────────────────────────── */
#modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.72);
  backdrop-filter: blur(5px);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  animation: fadeIn 0.14s ease;
}
#modal-overlay.hidden { display: none; }

#modal-box {
  background: var(--sidebar-bg);
  border: 1px solid var(--border-2);
  border-radius: var(--radius-lg);
  min-width: 420px; max-width: 620px; width: 92%;
  max-height: 88vh;
  display: flex; flex-direction: column;
  box-shadow: var(--shadow);
  animation: slideUp 0.18s ease;
  overflow: hidden;
  transition: background var(--t);
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h2 { font-family: var(--font-card); font-size: 1.15rem; font-weight: 500; color: var(--text); }

#modal-body { padding: 20px; overflow-y: auto; flex: 1; }

/* ── Card editor form ───────────────────────────────────────── */
.card-editor { display: flex; flex-direction: column; gap: 0; }

.editor-side {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  background: var(--surface);
  transition: border-color var(--t);
}
.editor-side:last-of-type { margin-bottom: 0; }
.editor-side-label {
  font-size: 0.62rem; letter-spacing: 0.1em;
  color: var(--accent); margin-bottom: 10px; display: block;
}

.media-slots {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
}

.media-slot-label {
  font-size: 0.6rem; letter-spacing: 0.08em;
  color: var(--text-muted); margin-bottom: 5px; display: block;
}

/* ── Upload zone ────────────────────────────────────────────── */
.upload-zone {
  border: 1px dashed var(--border-2);
  border-radius: var(--radius);
  padding: 10px 8px;
  text-align: center;
  font-size: 0.66rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: border-color var(--t), background var(--t);
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1.4;
}
.upload-zone:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-muted); }
.upload-zone.has-file { border-style: solid; border-color: var(--border-2); padding: 0; overflow: hidden; }

.upload-zone img,
.upload-zone video {
  max-width: 100%;
  max-height: 90px;
  display: block;
  border-radius: calc(var(--radius) - 1px);
}
.upload-zone video { background: #000; }

.slot-remove-btn {
  display: block;
  width: 100%;
  background: none;
  border: none;
  border-top: 1px solid var(--border);
  padding: 3px 0;
  font-family: var(--font-ui);
  font-size: 0.6rem;
  color: var(--danger);
  cursor: pointer;
  text-align: center;
  transition: background var(--t);
}
.slot-remove-btn:hover { background: rgba(196, 90, 64, 0.12); }

/* ── Forms ──────────────────────────────────────────────────── */
.form-group { margin-bottom: 14px; }
.form-group:last-child { margin-bottom: 0; }

.form-label {
  display: block; font-size: 0.62rem; letter-spacing: 0.1em;
  color: var(--text-muted); margin-bottom: 6px;
}

.form-input, .form-textarea {
  width: 100%; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px;
  font-family: var(--font-card); font-size: 1.02rem;
  color: var(--text); outline: none;
  transition: border-color var(--t), background var(--t); resize: none;
}
.form-input:focus, .form-textarea:focus { border-color: var(--accent); }
.form-textarea { min-height: 72px; line-height: 1.5; }

.modal-footer {
  display: flex; gap: 8px; justify-content: flex-end;
  padding-top: 16px; margin-top: 4px;
  border-top: 1px solid var(--border);
}

/* ── Buttons ────────────────────────────────────────────────── */
.btn {
  padding: 8px 18px; border-radius: var(--radius);
  font-family: var(--font-ui); font-size: 0.7rem;
  cursor: pointer; transition: all var(--t);
  border: 1px solid transparent; white-space: nowrap;
}
.btn-secondary { background: none; border-color: var(--border); color: var(--text-muted); }
.btn-secondary:hover { color: var(--text); border-color: var(--border-2); background: var(--surface); }
.btn-primary { background: var(--accent); color: #12100d; border-color: var(--accent); font-weight: 500; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { background: none; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { padding: 4px 11px; font-size: 0.65rem; }

/* ── Manage list ────────────────────────────────────────────── */
.manage-list { display: flex; flex-direction: column; gap: 5px; margin-bottom: 4px; }

.manage-empty {
  font-size: 0.78rem; color: var(--text-muted); text-align: center;
  padding: 24px 0 8px; font-family: var(--font-card); font-style: italic;
}

.manage-item {
  display: flex; align-items: center;
  padding: 9px 12px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border);
  gap: 10px; transition: border-color var(--t);
}
.manage-item:hover { border-color: var(--border-2); }
.manage-item-info { flex: 1; min-width: 0; }
.manage-item-name {
  font-family: var(--font-card); font-size: 1rem; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.manage-item-sub {
  font-size: 0.65rem; color: var(--text-muted); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.manage-item-meta { font-size: 0.64rem; color: var(--text-muted); flex-shrink: 0; }
.manage-item-actions { display: flex; gap: 5px; flex-shrink: 0; }

.manage-add-row {
  display: flex; gap: 8px; align-items: center;
  padding-top: 14px; margin-top: 12px;
  border-top: 1px solid var(--border);
}
.manage-add-row .form-input { flex: 1; font-size: 0.88rem; padding: 8px 10px; }

/* ── Confirm dialog ─────────────────────────────────────────── */
.confirm-text { font-family: var(--font-card); font-size: 1rem; line-height: 1.6; color: var(--text); }
.confirm-text strong { font-weight: 600; }

/* ── Toast ──────────────────────────────────────────────────── */
#toast-container {
  position: fixed; bottom: 22px; right: 22px;
  display: flex; flex-direction: column; gap: 7px;
  z-index: 200; pointer-events: none;
}
.toast {
  background: var(--surface-2); border: 1px solid var(--border-2);
  border-radius: var(--radius); padding: 9px 15px;
  font-size: 0.7rem; color: var(--text); box-shadow: var(--shadow);
  animation: toastIn 0.2s ease; max-width: 280px; word-break: break-word;
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error   { border-left: 3px solid var(--danger);  }

/* ── Animations ─────────────────────────────────────────────── */
@keyframes fadeIn  { from { opacity: 0; }                     to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes toastIn { from { transform: translateX(14px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ── Scrollbar ──────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* ── Responsive ─────────────────────────────────────────────── */
@media (max-width: 680px) {
  #sidebar { width: 180px; min-width: 180px; }
  #card-scene { height: 260px; }
  .card-text { font-size: 1.2rem; }
  .ctrl-btn { min-width: 80px; padding: 8px 12px; }
  .media-slots { grid-template-columns: 1fr; }
  .modal-box { min-width: 92vw; }
}
