:root {
  --wood: #d2b48c;
  --wood-dark: #b8976c;
  --metal: linear-gradient(160deg, #e8e8ec, #c6c6cc 45%, #9c9ca4 55%, #d5d5db);
  --metal-flat: #c6c6cc;
  --metal-dark: #7d7d85;
  --metal-shine: rgba(255, 255, 255, 0.55);
  --accent: #d94a38;
  --primary: #2d6e7e;
  --card: #faf6ee;
  --ink: #3a342c;
  --easy: #4caf6e;
  --medium: #d99e2b;
  --hard: #d94a38;
  --shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--wood);
}

body {
  display: flex;
  justify-content: center;
  padding-top: var(--safe-t);
}

/* ---- Tabletop ---- */
.tabletop {
  width: 100%;
  max-width: 480px;
  min-height: 100vh;
  padding: 14px 12px;
  background:
    radial-gradient(circle at 18% 12%, rgba(255, 255, 255, 0.12) 0 2px, transparent 3px),
    radial-gradient(circle at 78% 22%, rgba(0, 0, 0, 0.06) 0 2px, transparent 3px),
    radial-gradient(circle at 50% 80%, rgba(255, 255, 255, 0.10) 0 3px, transparent 4px),
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.03) 0 2px, transparent 2px 26px),
    linear-gradient(180deg, var(--wood) 0%, var(--wood-dark) 100%);
}

/* ---- The metal box ---- */
.box {
  position: relative;
  border: 6px solid transparent;
  border-image: var(--metal) 1;
  border-radius: 14px;
  background: var(--metal-flat);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 28px - var(--safe-t));
}

/* ---- Hinged lid ---- */
.lid {
  position: relative;
  height: 74px;
  background: var(--metal);
  border-bottom: 6px solid var(--metal-dark);
  padding: 0 12px;
}
.lid::before {
  content: "";
  position: absolute;
  top: 10px;
  left: 0;
  right: 0;
  height: 34px;
  background: linear-gradient(160deg, rgba(255,255,255,0.5), rgba(255,255,255,0.02) 55%, rgba(0,0,0,0.12));
  transform: skewY(-6deg);
  pointer-events: none;
  border-bottom: 1px solid rgba(255,255,255,0.4);
}
.lid::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 60px;
  height: 10px;
  background: linear-gradient(180deg, rgba(255,255,255,0.4), rgba(0,0,0,0.25));
}
.hinge {
  position: absolute;
  top: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 110px;
  height: 16px;
  background: linear-gradient(180deg, #b8b8bf, #6f6f77);
  border-radius: 0 0 6px 6px;
  box-shadow: inset 0 -3px 4px rgba(0,0,0,0.35);
}

/* ---- Tokens ---- */
.tokens {
  position: relative;
  z-index: 2;
  display: flex;
  gap: 18px;
  justify-content: center;
  padding-top: 34px;
}
.token {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffffff, #c2c2c9 60%, #8a8a92);
  border: 2px solid #77777f;
  box-shadow: 0 3px 5px rgba(0,0,0,0.4), inset 0 1px 2px rgba(255,255,255,0.8);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.token-num {
  font-weight: 800;
  font-size: 20px;
  line-height: 1;
  color: #33333a;
  text-shadow: 0 1px 0 rgba(255,255,255,0.7);
}
.token-label {
  font-size: 8px;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: #55555c;
  text-transform: uppercase;
  margin-top: 2px;
}

/* ---- Content area ---- */
.content {
  flex: 1;
  background: var(--secondary, #f5f0e8);
  background-image: linear-gradient(180deg, #f5f0e8, #ece4d8);
  padding: 14px 14px 18px;
  min-height: 0;
  position: relative;
}
.tab-panel { animation: fadeIn 0.25s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---- Deck ---- */
.toolbar { position: sticky; top: 0; z-index: 5; padding-bottom: 10px; background: #f5f0e8; }
.toolbar input {
  width: 100%;
  padding: 12px 14px;
  border: 2px solid #d8cdb8;
  border-radius: 12px;
  font-size: 15px;
  background: #fffdf7;
  color: var(--ink);
}
.toolbar input:focus { outline: none; border-color: var(--primary); }

.lang-group { margin-bottom: 14px; }
.lang-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: #7a5b35;
}
.lang-header::before { content: "▮"; color: var(--primary); }
.lang-count { color: #a08a63; font-weight: 500; }

/* ---- Flashcard ---- */
.flip { perspective: 1000px; margin-bottom: 10px; }
.card {
  position: relative;
  width: 100%;
  min-height: 86px;
  transform-style: preserve-3d;
  transition: transform 0.5s ease;
  cursor: pointer;
}
.flip.flipped .card { transform: rotateY(180deg); }
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--card);
  border: 1px solid #e3d9c4;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.12);
  padding: 16px 16px 14px;
  min-height: 86px;
}
.face.front {
  border-top: 6px solid var(--easy);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.face.back {
  transform: rotateY(180deg);
  border-top: 6px solid #d8c39a;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.card.easy .face.front { border-top-color: var(--easy); }
.card.medium .face.front { border-top-color: var(--medium); }
.card.hard .face.front { border-top-color: var(--hard); }

.card-front { font-size: 17px; font-weight: 700; }
.card-lang {
  font-size: 11px;
  color: #a08a63;
  background: rgba(45,110,126,0.12);
  padding: 3px 8px;
  border-radius: 20px;
  white-space: nowrap;
}
.card-back { font-size: 16px; color: #4a443a; }
.card-meta { font-size: 11px; color: #a08a63; margin-top: 6px; }

/* paperclip on mastered cards */
.paperclip {
  position: absolute;
  top: 8px;
  right: 6px;
  width: 16px;
  height: 26px;
  border: 2px solid #c9ccd4;
  border-radius: 3px;
  background: linear-gradient(160deg, #f2f2f5, #b8bcc4);
  box-shadow: 0 1px 2px rgba(0,0,0,0.25);
  z-index: 3;
  pointer-events: none;
}
.paperclip::after {
  content: "";
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 12px;
  height: 10px;
  border: 2px solid #c9ccd4;
  border-top: none;
  border-radius: 0 0 8px 8px;
}

/* card actions */
.card-actions {
  position: absolute;
  right: 8px;
  bottom: 4px;
  display: flex;
  gap: 8px;
  z-index: 4;
}
.card-actions button {
  border: none;
  background: none;
  color: var(--primary);
  font-size: 13px;
  font-weight: 600;
  padding: 4px;
  cursor: pointer;
}
.card-actions button.del { color: var(--hard); }

/* ---- Review ---- */
.panel-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 14px; }
.panel-head h2 { font-size: 20px; color: var(--primary); }
.sub { font-size: 12px; color: #a08a63; }

.review-card .face { padding-bottom: 14px; }
.rating-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 12px;
}
.rating-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 2px solid #c6bb9f;
  background: #fff;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s, border-color 0.15s;
}
.rating-btn:hover { transform: scale(1.08); }
.rating-btn.active { border-color: var(--primary); background: var(--primary); color: #fff; }
.rating-note { text-align: center; font-size: 11px; color: #a08a63; margin-bottom: 4px; }

/* ---- Add form ---- */
#addForm h2 { font-size: 20px; color: var(--primary); margin-bottom: 12px; }
#addForm label { display: block; font-size: 13px; font-weight: 600; margin: 12px 0 5px; color: #5c4f3d; }
#addForm label em { color: var(--hard); font-style: normal; }
#addForm input[type="text"],
#addForm select {
  width: 100%;
  padding: 12px;
  border: 2px solid #d8cdb8;
  border-radius: 10px;
  font-size: 15px;
  background: #fffdf7;
  color: var(--ink);
}
#addForm input:focus, #addForm select:focus { outline: none; border-color: var(--primary); }

#difficulty {
  width: 100%;
  accent-color: var(--primary);
  height: 34px;
}
.diff-scale { display: flex; justify-content: space-between; font-size: 12px; color: #a08a63; padding: 0 2px; }
.difficultyLabel { color: var(--primary); font-weight: 700; }

.err { color: var(--hard); font-size: 12px; min-height: 16px; margin-top: 3px; }
.btn-row { display: flex; gap: 10px; margin-top: 18px; }
button.primary {
  flex: 1;
  padding: 14px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 3px 0 #1f515e;
}
button.primary:active { transform: translateY(2px); box-shadow: 0 1px 0 #1f515e; }
button.ghost {
  padding: 14px 18px;
  border: 2px solid #c6bb9f;
  border-radius: 12px;
  background: transparent;
  color: #6a5b42;
  font-weight: 600;
  cursor: pointer;
}
.msg { text-align: center; font-size: 13px; color: #4c8a52; min-height: 18px; margin-top: 10px; font-weight: 600; }

/* ---- Progress ---- */
.progress-wrap {
  background: #fffdf7;
  border: 1px solid #e3d9c4;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 20px;
}
.progress-meta { display: flex; justify-content: space-between; font-size: 13px; color: #5c4f3d; margin-bottom: 8px; font-weight: 600; }
.progress-track { height: 14px; background: #e7ddc8; border-radius: 20px; overflow: hidden; }
.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #4caf6e, #2d6e7e);
  border-radius: 20px;
  transition: width 0.6s ease;
}
.block-title { font-size: 15px; color: #7a5b35; margin: 16px 0 10px; }

/* recent cards list */
.recent-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #fffdf7;
  border: 1px solid #e3d9c4;
  border-left: 4px solid var(--easy);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.recent-item.medium { border-left-color: var(--medium); }
.recent-item.hard { border-left-color: var(--hard); }
.recent-front { font-weight: 600; flex: 1; }
.recent-lang { font-size: 11px; color: #a08a63; }
.recent-time { font-size: 11px; color: #a08a63; }

/* ---- Empty ---- */
.empty { text-align: center; color: #a08a63; padding: 26px 10px; font-size: 14px; }
.empty .sub { display: block; margin-top: 4px; }

/* ---- Tab bar ---- */
.tabbar {
  display: flex;
  background: linear-gradient(180deg, #cfcfd6, #a9a9b1);
  border-top: 3px solid #6f6f77;
  padding: 4px 4px calc(4px + var(--safe-b));
  position: relative;
}
.tab {
  flex: 1;
  border: none;
  background: linear-gradient(180deg, #9a9aa2, #787880);
  color: #3c3c42;
  border-radius: 8px 8px 0 0;
  padding: 9px 0 7px;
  margin: 3px 2px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.35), 0 1px 0 rgba(255,255,255,0.15);
  transition: transform 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
  position: relative;
}
.tab-ico { font-size: 17px; line-height: 1; }
.tab-lbl { font-size: 10px; font-weight: 700; letter-spacing: 0.4px; text-transform: uppercase; margin-top: 3px; }
.tab.active {
  background: linear-gradient(180deg, #ffffff, #dedede 70%, #c6c6cc);
  color: #2d2d33;
  transform: translateY(-8px);
  box-shadow: 0 5px 10px rgba(0,0,0,0.3), inset 0 1px 2px rgba(255,255,255,0.9);
}
.tab.active::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 10px;
  right: 10px;
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.9), transparent);
}

/* responsiveness */
@media (max-width: 360px) {
  .token { width: 48px; height: 48px; }
  .token-num { font-size: 18px; }
}