:root {
  --bg: #f4f1ea;
  --card: #ffffff;
  --ink: #2b2b2b;
  --muted: #7a7568;
  --accent: #1f5aa8;
  --accent-dark: #164378;
  --ok: #2e7d32;
  --err: #c62828;
  --tile-bg: #eef2f7;
  --tile-border: #c7d2df;
  --bar-bg: #ffffff;
  --bar-border: #e3ddcf;
  --topbar-h: 56px;
  --tabbar-h: 64px;
  --screen-pad-top: 72px;
  --screen-pad-bottom: 96px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

button, input {
  font-family: inherit;
}

/* ---------- App shell ---------- */

.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 56px;
  background: var(--bar-bg);
  border-bottom: 1px solid var(--bar-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 10;
}
.topbar-title {
  font-weight: 700;
  font-size: 16px;
}
.account-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--tile-bg);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.account-btn:hover { background: #dde7f2; }

.screen {
  max-width: 520px;
  margin: 0 auto;
  padding: var(--screen-pad-top) 20px calc(var(--screen-pad-bottom) + var(--safe-bottom));
  min-height: 100vh;
}

.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bar-bg);
  border-top: 1px solid var(--bar-border);
  display: flex;
  z-index: 10;
}
.tab-btn {
  flex: 1;
  border: none;
  background: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
}
.tab-btn .tab-icon { font-size: 20px; }
.tab-btn.active { color: var(--accent); }

/* ---------- Dashboard ---------- */

.stat-row {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
}
.stat-card {
  flex: 1;
  background: var(--card);
  border-radius: 14px;
  padding: 18px 12px;
  text-align: center;
  box-shadow: 0 4px 16px rgba(0,0,0,0.06);
}
.stat-icon { font-size: 22px; margin-bottom: 6px; }
.stat-value { font-size: 26px; font-weight: 700; }
.stat-label { font-size: 12px; color: var(--muted); margin-top: 2px; }

.catalog-list {
  background: var(--card);
  border-radius: 14px;
  padding: 4px 16px;
  margin-bottom: 24px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.06);
}
.catalog-row {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  font-size: 14px;
}
.catalog-row + .catalog-row { border-top: 1px solid var(--tile-border); }
.catalog-count { font-weight: 700; color: var(--accent); }

.cta-wrap { text-align: center; }
.cta-btn {
  width: 100%;
  padding: 18px;
  font-size: 17px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 14px;
  cursor: pointer;
}
.cta-btn:hover { background: var(--accent-dark); }
.all-done { color: var(--muted); font-size: 14px; }
.all-done p { margin: 0 0 8px; }
.add-words-link { color: var(--accent); font-weight: 600; text-decoration: none; }
.add-words-link:hover { text-decoration: underline; }

/* ---------- Trainer session (shared with the letter-scramble widget) ---------- */

/* .session is a flex column so the interactive letter area can be pinned
   to the bottom (thumb reach) on mobile via margin-top:auto below, while
   #trainerMount stays display:contents so its children (translation,
   answer slot, pool, feedback, reset) act as direct siblings of
   .session-top / .session-controls for that same flex layout. */
.session {
  display: flex;
  flex-direction: column;
}
#trainerMount { display: contents; }

.session-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
  flex-wrap: wrap;
  gap: 8px;
}
.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.toggle-row input {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--accent);
}
.progress {
  font-size: 13px;
  color: var(--muted);
}

.translation-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 28px;
}
.translation {
  text-align: center;
  font-size: 30px;
  font-weight: 700;
  overflow-wrap: break-word;
  word-break: break-word;
}

.answer-slot {
  min-height: 56px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px;
  border-bottom: 2px solid var(--tile-border);
  padding-bottom: 14px;
  margin-bottom: 24px;
}
.answer-hint { color: var(--muted); font-size: 14px; }

.letter-btn {
  min-width: 44px;
  height: 48px;
  padding: 0 10px;
  font-size: 22px;
  background: var(--tile-bg);
  border: 2px solid var(--tile-border);
  border-radius: 10px;
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease, opacity 0.15s ease;
}
.letter-btn:hover:not(:disabled) { background: #dde7f2; transform: translateY(-2px); }
.letter-btn:active:not(:disabled) { transform: translateY(0); }
.letter-btn:disabled { opacity: 0.25; cursor: default; }

.pool {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  min-height: 58px;
  margin-bottom: 22px;
}

.feedback {
  text-align: center;
  min-height: 30px;
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 14px;
}
.feedback.ok { color: var(--ok); }
.feedback.err { color: var(--err); }

.controls, .session-controls {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 4px;
}

button.action {
  font-size: 15px;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  transition: background 0.15s ease, opacity 0.15s ease;
}
button.action:disabled { opacity: 0.4; cursor: default; }
.btn-reset { background: var(--tile-bg); color: var(--ink); }
.btn-reset:hover { background: #dde7f2; }
.btn-next, .btn-add, .btn-save { background: var(--accent); color: #fff; }
.btn-next:hover, .btn-add:hover, .btn-save:hover { background: var(--accent-dark); }
.btn-next:disabled { opacity: 0.4; cursor: default; }
.btn-cancel { background: var(--tile-bg); color: var(--ink); }
.btn-cancel:hover { background: #dde7f2; }

.session-complete { text-align: center; }
.session-complete h2 { color: var(--accent); margin-bottom: 8px; }

/* Mobile: pin the letter pool / answer area near the thumb zone at the
   bottom of the screen instead of wherever normal document flow leaves it.
   .session stretches to fill the viewport between the top/tab bars, and
   margin-top:auto on .answer-slot pushes it (plus everything after it —
   pool, feedback, reset, next) down to the bottom as one cluster, while
   the translation/hint stays pinned at the top. */
@media (max-width: 700px) {
  .session {
    min-height: calc(100vh - var(--screen-pad-top) - var(--screen-pad-bottom) - var(--safe-bottom));
    min-height: calc(100dvh - var(--screen-pad-top) - var(--screen-pad-bottom) - var(--safe-bottom));
  }
  .session-top { margin-bottom: 14px; }
  .translation-row { margin-bottom: 16px; }
  .answer-slot {
    margin-top: auto;
    margin-bottom: 16px;
    gap: 6px;
  }
  .pool { margin-bottom: 16px; gap: 6px; }
  .feedback { margin-bottom: 10px; }
  .letter-btn { padding: 0 8px; }
}

/* ---------- Words screen ---------- */

.add-form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}
.add-form input {
  flex: 1 1 120px;
  min-width: 100px;
  padding: 10px 12px;
  font-size: 14px;
  border-radius: 8px;
  border: 1px solid var(--tile-border);
  background: var(--tile-bg);
  color: var(--ink);
}
.add-form input.input-error {
  border-color: var(--err);
}
.add-msg {
  min-height: 18px;
  font-size: 13px;
  color: var(--ok);
  margin-bottom: 18px;
}
.add-msg.err { color: var(--err); }
.add-msg a { color: inherit; font-weight: 700; text-decoration: underline; }

.search-input {
  width: 100%;
  padding: 10px 14px;
  font-size: 14px;
  border-radius: 10px;
  border: 1px solid var(--tile-border);
  background: var(--card);
  color: var(--ink);
  margin-bottom: 12px;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
}
.chip {
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  border-radius: 999px;
  border: 1px solid var(--tile-border);
  background: var(--tile-bg);
  color: var(--muted);
  cursor: pointer;
}
.chip:hover { background: #dde7f2; }
.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.words-list { display: flex; flex-direction: column; gap: 8px; }
.word-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--card);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
  flex-wrap: wrap;
}
.word-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1 1 auto;
  min-width: 0;
}
.word-greek {
  font-size: 14px;
  font-weight: 700;
  overflow-wrap: break-word;
  word-break: break-word;
}
.word-translation {
  font-size: 12px;
  color: var(--muted);
  overflow-wrap: break-word;
  word-break: break-word;
}
.word-status {
  font-size: 11px;
  color: var(--muted);
  opacity: 0.85;
}
.word-actions { display: flex; gap: 6px; flex: 0 0 auto; }
.icon-btn {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  padding: 0;
  font-size: 15px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}

.menu-wrap { position: relative; }
.menu-dropdown {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  min-width: 150px;
  background: var(--card);
  border: 1px solid var(--tile-border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.15);
  overflow: hidden;
  z-index: 20;
}
.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 14px;
  font-size: 14px;
  background: none;
  border: none;
  color: var(--ink);
  cursor: pointer;
}
.menu-item:hover { background: var(--tile-bg); }
.menu-item.danger { color: var(--err); }
.word-row input.edit-greek,
.word-row input.edit-translation {
  flex: 1 1 100px;
  min-width: 90px;
  padding: 8px 10px;
  font-size: 14px;
  border-radius: 8px;
  border: 1px solid var(--tile-border);
  background: var(--tile-bg);
  color: var(--ink);
}
.empty-msg { text-align: center; color: var(--muted); font-size: 14px; padding: 24px 0; }

/* ---------- Loading / error states (words API) ---------- */

.state-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 12px;
  padding: 64px 24px;
}
.state-message { margin: 0; font-size: 15px; color: var(--ink); }
.state-error .state-message { color: var(--err); }
.state-hint { margin: -4px 0 0; font-size: 13px; color: var(--muted); }
.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--tile-border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.row-error { color: var(--err); font-size: 12px; margin-top: 2px; }
.word-row > .row-error, .selection-bar-inner > .row-error { flex-basis: 100%; }
.session-msg:not(:empty) { margin: -4px 0 12px; font-size: 13px; display: flex; align-items: center; gap: 8px; }

.selection-bar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  background: var(--card);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.selection-count { font-size: 13px; color: var(--muted); font-weight: 600; }
.selection-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-delete { background: var(--err); color: #fff; }
.btn-delete:hover:not(:disabled) { filter: brightness(0.9); }

.select-check {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 2px solid var(--tile-border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
}
.select-check.checked { background: var(--accent); border-color: var(--accent); }
.word-row.selectable { cursor: pointer; }
.word-row.selected { outline: 2px solid var(--accent); outline-offset: -2px; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: 20px;
}
.modal-card {
  background: var(--card);
  border-radius: 14px;
  padding: 20px;
  max-width: 320px;
  width: 100%;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  text-align: center;
}
.modal-card p { margin: 0 0 18px; font-size: 14px; color: var(--ink); }
.modal-actions { display: flex; gap: 10px; justify-content: center; }

/* ---------- Settings ---------- */

.settings-screen { text-align: center; }
.back-btn {
  align-self: flex-start;
  border: none;
  background: none;
  color: var(--accent);
  font-size: 14px;
  cursor: pointer;
  margin-bottom: 16px;
  padding: 0;
}
.settings-placeholder { color: var(--muted); font-size: 14px; }

.settings-section {
  margin-top: 32px;
  text-align: left;
}
.settings-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 10px;
}
.option-row {
  display: flex;
  gap: 8px;
}
.option-btn {
  flex: 1;
  padding: 10px 0;
  font-size: 15px;
  font-weight: 600;
  border-radius: 10px;
  border: 2px solid var(--tile-border);
  background: var(--tile-bg);
  color: var(--ink);
  cursor: pointer;
}
.option-btn:hover { background: #dde7f2; }
.option-btn.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

/* ---------- Dark mode ---------- */

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1c1f;
    --card: #26282c;
    --ink: #f0efe9;
    --muted: #9b9a94;
    --accent: #5b9bf0;
    --accent-dark: #7cb0f5;
    --ok: #6fcf78;
    --err: #f2726a;
    --tile-bg: #34363b;
    --tile-border: #4a4d53;
    --bar-bg: #202124;
    --bar-border: #37383c;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1c1f;
  --card: #26282c;
  --ink: #f0efe9;
  --muted: #9b9a94;
  --accent: #5b9bf0;
  --accent-dark: #7cb0f5;
  --ok: #6fcf78;
  --err: #f2726a;
  --tile-bg: #34363b;
  --tile-border: #4a4d53;
  --bar-bg: #202124;
  --bar-border: #37383c;
}
