:root {
  --bg: #0f172a;
  --panel: #111827;
  --muted: #94a3b8;
  --text: #e5e7eb;
  --accent: #3b82f6;
  --accent-2: #10b981;
  --danger: #ef4444;
  --warn: #f59e0b;
  --border: #1f2937;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  background: var(--bg);
  color: var(--text);
}

/* Prefer mobile viewport units to reduce URL bar jump */
html, body { height: 100%; min-height: 100dvh; }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: 0.3px; }
.controls { display: flex; align-items: center; gap: 10px; }
.icon-button {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--border);
  background: #0b1220;
  color: var(--text);
  border-radius: 10px;
  padding: 6px 10px;
  cursor: pointer;
}
.gear-button { margin-right: 0; }
.gear-button.fab {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  width: 48px;
  height: 48px;
  border-radius: 9999px;
  display: inline-grid;
  place-items: center;
  z-index: 1100;
  box-shadow: 0 6px 16px rgba(0,0,0,0.35);
}
/* Make the gear icon subdued/monochrome-feeling */
.gear-button .gear {
  font-size: 18px;
  line-height: 1;
  display: inline-block;
  filter: grayscale(1) saturate(0);
  opacity: 0.6;
  transform: translateY(1px);
}
@media (hover:hover) {
  .gear-button:hover .gear { opacity: 0.85; }
}
.build-tag {
  position: fixed;
  left: max(16px, env(safe-area-inset-left));
  bottom: max(16px, env(safe-area-inset-bottom));
  padding: 4px 12px;
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  color: var(--muted);
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid var(--border);
  border-radius: 9999px;
  z-index: 1200;
  pointer-events: none;
}
.controls select {
  background: #0b1220;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 8px;
  border-radius: 6px;
}
.summary { display: flex; gap: 12px; color: var(--muted); font-size: 0.9rem; }

.tabs {
  display: flex;
  gap: 8px;
  padding: 8px 16px;
  background: #0b1220;
  border-bottom: 1px solid var(--border);
}
.tab-button {
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.tab-button.active { background: var(--panel); border-color: #2b3445; }

.tab-contents { padding: 16px; }
.tab-content { display: none; }
.tab-content.active { display: block; }

.flashcard {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative; /* for corner badge placement */
  min-height: 40vh;
  border: 1px dashed var(--border);
  border-radius: 12px;
  background: #0b1220;
  margin-bottom: 16px;
}
.card-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 8px;
}
.card-text {
  font-size: clamp(2.75rem, 20vw, 9rem);
  font-weight: 700;
  text-align: center;
  letter-spacing: 0.5px;
}
.card-phrase {
  font-size: clamp(0.9rem, 2.5vw, 1.2rem);
  line-height: 1.35;
  color: var(--muted);
  max-width: min(90%, 900px);
}
.card-meta {
  position: absolute;
  left: 10px;
  bottom: 10px;
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--muted);
  background: #1f2937;
  border: 1px solid var(--border);
  padding: 2px 8px;
  border-radius: 9999px; /* pill badge */
}
.card-motion {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 0.8rem;
  line-height: 1.35;
  color: var(--muted);
  background: #1f2937;
  border: 1px solid var(--border);
  padding: 2px 8px;
  border-radius: 9999px; /* pill badge */
  max-width: 50%;
}

.practice-controls { display: grid; gap: 12px; }
.practice-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  align-items: center;
}
#btnMastered { grid-column: 1; justify-self: center; }
#btnNext { grid-column: 2; justify-self: center; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  min-width: 120px;
  font-size: 0.95rem;
  line-height: 1.15;
}
.btn.positive { background: var(--accent-2); border-color: #0d5a45; }
.btn.negative { background: var(--danger); border-color: #7f1d1d; }
.btn.secondary { background: #1f2937; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

.mode-toggle { display: flex; align-items: center; gap: 12px; justify-content: center; color: var(--muted); }
.session-stats { text-align: center; color: var(--muted); }

.overview-actions { display: flex; gap: 10px; margin-bottom: 14px; }
.grid-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.word-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  background: #0b1220;
}
.word-card.mastered { border-color: #14532d; background: rgba(16, 185, 129, 0.08); }
.word-title { font-weight: 700; margin-bottom: 8px; font-size: 1.1rem; }
.word-meta { font-size: 0.85rem; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; }
.word-meta span { background: #1f2937; border: 1px solid var(--border); padding: 2px 8px; border-radius: 9999px; }
.word-card.mastered .word-meta span { background: rgba(16, 185, 129, 0.15); border-color: #14532d; color: #bbf7d0; }
.word-actions { margin-top: 10px; }
/* Single full-width action for compact cards */
.grid-list .btn { width: 100%; min-width: 0; padding: 8px 10px; font-size: 0.9rem; }

.phrase-list { display: grid; gap: 6px; }
.phrase-item { color: var(--muted); }
.phrase-eligible { color: var(--accent); }

.admin-section { margin-bottom: 18px; }
.display-controls { display: flex; gap: 14px; align-items: center; color: var(--muted); }
.admin-stats, .lists-overview { display: grid; gap: 8px; }
.admin-tools { margin-top: 8px; }
.hidden { display: none; }
.admin-toggle-link {
  color: var(--muted);
  text-decoration: underline;
  cursor: pointer;
}
.row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: #0b1220;
}
.row .muted { color: var(--muted); }

.file-input { position: relative; display: inline-block; }
.file-input input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.file-input .btn { pointer-events: none; }

.app-footer {
  padding: 12px 16px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

@media (max-width: 600px) {
  .controls { flex-wrap: wrap; gap: 6px; }
  .summary { width: 100%; justify-content: flex-end; }
}

/* Mobile-first practice UX */
@media (max-width: 720px) {
  .tabs { display: none; }
  .app-footer { display: none; }
  .app-header .controls { display: none; }
  .settings-panel .controls { display: flex; }
  .mode-toggle { display: none; }
  .settings-panel #modeToggle { display: flex; align-items: center; justify-content: flex-start; gap: 12px; }

  .tab-contents { padding: 12px; }
  .flashcard { min-height: 60dvh; }
  .card-text { font-size: clamp(3rem, 22vw, 9.5rem); }
  .practice-buttons { grid-template-columns: 1fr; }
  .btn { width: 100%; min-height: 48px; }
}

/* Settings overlay */
.settings-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: end center;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(2px);
  z-index: 1000;
}
.settings-overlay.hidden { display: none; }
.settings-panel {
  width: 100%;
  max-width: 640px;
  background: var(--panel);
  color: var(--text);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  border: 1px solid var(--border);
  box-shadow: 0 -10px 30px rgba(0,0,0,0.4);
  margin: 0 8px;
}
.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--panel);
}
.settings-section { padding: 16px 20px; display: grid; gap: 14px; }
.settings-section + .settings-section { border-top: 1px solid var(--border); }
.nav-links { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.nav-links .btn { height: 44px; border-radius: 10px; }

/* Practice-first layout (hide chrome while practicing) */
.mode-practice .tabs { display: none; }
.mode-practice .app-footer { display: none; }
.mode-practice .app-header .controls { display: none; }
.mode-practice .app-header .brand { display: none; }
.mode-practice .mode-toggle { display: none; }
.settings-panel .controls { display: flex; }
.settings-panel #modeToggle { display: flex; align-items: center; justify-content: flex-start; gap: 12px; }

/* Practice layout: larger word, buttons lower, motion non-overlapping */
.mode-practice #tab-practice {
  min-height: calc(100dvh - 56px);
  display: flex;
  flex-direction: column;
}
.mode-practice #tab-practice .flashcard {
  flex: 1 1 auto;
  min-height: 0;
}
.mode-practice .practice-controls {
  margin-top: auto;
  padding-bottom: calc(max(env(safe-area-inset-bottom), 12px) + 56px);
}
.mode-practice .card-text {
  font-size: clamp(3.5rem, 26vw, 12rem);
}
.mode-practice .card-motion { position: absolute; top: 10px; right: 10px; max-width: 60%; margin-top: 0; }
.mode-practice .flashcard { padding-top: 40px; }

/* Support safe areas on iOS PWA */
@supports(padding: max(0px)) {
  .settings-panel { padding-bottom: max(env(safe-area-inset-bottom), 0px); }
  .app-header { padding-left: max(env(safe-area-inset-left), 16px); padding-right: max(env(safe-area-inset-right), 16px); }
}

/* Settings form rows */
.settings-panel #modeToggle {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 16px;
}
.settings-panel #modeToggle > label:first-child {
  color: var(--muted);
  font-weight: 600;
}
.settings-panel #headerControls {
  display: grid;
  grid-template-columns: auto minmax(140px, 1fr) auto;
  align-items: center;
  gap: 12px;
}
.settings-panel #headerControls label { color: var(--muted); font-weight: 600; }
.settings-panel .display-controls { display: flex; flex-wrap: wrap; gap: 12px 18px; }
.settings-panel .display-controls label { display: inline-flex; align-items: center; gap: 8px; }
