/* Derja Translator. Phone first; logical properties so the Arabic (RTL) layout mirrors itself. */

:root {
  --bg: #f8f4ee;
  --surface: #ffffff;
  --surface-2: #f2ece3;
  --text: #1f1b17;
  --muted: #675f56;
  --line: #e4dbcf;
  --accent: #b3122e;
  --accent-ink: #ffffff;
  --accent-soft: #fbe5e8;
  --teal: #0b6e76;
  --teal-soft: #dff1f2;
  --good: #1e7a3c;
  --good-soft: #e1f3e6;
  --bad: #b3261e;
  --bad-soft: #fbe4e2;
  --shadow: 0 1px 2px rgba(40, 25, 10, .06), 0 4px 14px rgba(40, 25, 10, .06);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, sans-serif;
  --font-ar: "Noto Naskh Arabic", "Noto Sans Arabic", "Segoe UI", Tahoma, "Geeza Pro", "Arabic Typesetting", sans-serif;
  --nav-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15120f;
    --surface: #1f1b17;
    --surface-2: #2a251f;
    --text: #f3ede5;
    --muted: #b3a99c;
    --line: #3a332b;
    --accent: #ff5d73;
    --accent-ink: #1a0508;
    --accent-soft: #3d1a20;
    --teal: #5fc7cf;
    --teal-soft: #173538;
    --good: #6fd08f;
    --good-soft: #17301f;
    --bad: #ff8a80;
    --bad-soft: #3a1b19;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
}
html[lang="ar"] body { font-family: "Segoe UI", Tahoma, system-ui, "Noto Sans Arabic", var(--font); }
[lang="aeb-Arab"], .ar { font-family: var(--font-ar); }

h1, h2 { line-height: 1.25; margin: 1.2em 0 .5em; }
h1 { font-size: 1.3rem; margin-top: .4em; }
h2 { font-size: 1.05rem; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--teal); outline-offset: 2px; border-radius: 6px; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { border-radius: 9px; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { font-size: 1.1rem; letter-spacing: .01em; }
.brand-text span { font-size: .78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.langs { display: flex; background: var(--surface-2); border-radius: 999px; padding: 3px; flex: none; }
.langs button {
  border: 0; background: transparent; padding: 6px 10px; border-radius: 999px; cursor: pointer;
  font-size: .85rem; font-weight: 600; color: var(--muted); min-width: 40px; min-height: 34px;
}
.langs button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.langs button[lang="ar"] { font-family: var(--font-ar); }

/* ---------- tabs: bottom bar on phones ---------- */
.tabs {
  position: fixed; inset-inline: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .75rem; font-weight: 600;
}
.tabs a[aria-current="page"] { color: var(--accent); }
.tabs a[aria-current="page"] svg { stroke-width: 2.2; }

main { max-width: 760px; margin: 0 auto; padding: 14px 16px 24px; outline: none; }
.loading { color: var(--muted); text-align: center; padding: 40px 0; }

/* ---------- inputs ---------- */
.field-label { display: block; font-weight: 600; font-size: .9rem; margin: 4px 0 6px; color: var(--muted); }
textarea, input[type="search"], select {
  width: 100%; border: 1.5px solid var(--line); background: var(--surface); border-radius: var(--radius);
  padding: 12px 14px; font-size: 1.05rem; resize: vertical;
}
textarea:focus, input[type="search"]:focus, select:focus { border-color: var(--teal); outline: none; box-shadow: 0 0 0 3px var(--teal-soft); }
.searchbox { position: relative; }
.searchbox textarea { min-height: 64px; }
/* the clear button sits on the page's end side, whatever the typed text's direction */
html[dir="ltr"] .searchbox textarea { padding-right: 48px; padding-left: 14px; }
html[dir="rtl"] .searchbox textarea { padding-left: 48px; padding-right: 14px; }
.icon-btn {
  position: absolute; inset-inline-end: 8px; top: 8px;
  width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--surface-2); color: var(--muted);
  display: grid; place-items: center; cursor: pointer;
}
.icon-btn svg { width: 18px; height: 18px; }

.meta { font-size: .85rem; color: var(--muted); margin: 8px 2px; min-height: 1.3em; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: .78rem; font-weight: 600; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.badge.teal { background: var(--teal-soft); color: var(--teal); }
.badge.red { background: var(--accent-soft); color: var(--accent); }

.examples { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 6px 0 4px; color: var(--muted); font-size: .9rem; }
.chip {
  border: 1.5px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 6px 12px; cursor: pointer; font-size: .9rem; min-height: 36px;
}
.chip:hover { border-color: var(--teal); }
.chip[lang="aeb-Arab"] { font-size: 1rem; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1.5px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 10px 16px; cursor: pointer; font-weight: 600; min-height: 44px;
}
.btn.small { min-height: 36px; padding: 6px 12px; font-size: .88rem; }
.btn.small svg { width: 18px; height: 18px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.good { background: var(--good); color: #fff; border-color: var(--good); }
.btn.wide { width: 100%; margin-top: 14px; }
.btn.link { background: none; border: 0; color: var(--muted); text-decoration: underline; width: 100%; margin-top: 8px; font-weight: 500; }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- results / entry cards ---------- */
.results { display: grid; gap: 12px; margin-top: 10px; }
.notice {
  border-radius: 12px; padding: 10px 14px; font-size: .9rem;
  background: var(--teal-soft); color: var(--text); border-inline-start: 4px solid var(--teal);
}
.notice.warn { background: var(--accent-soft); border-color: var(--accent); }
.notice strong { display: block; margin-bottom: 2px; }

.entry {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.entry-top { display: flex; justify-content: space-between; gap: 8px; align-items: center; margin-bottom: 6px; }
.derja { display: grid; gap: 2px; }
.line { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.line .ar { font-size: 1.6rem; line-height: 1.5; font-weight: 600; }
.line .lat { font-size: 1.15rem; font-weight: 600; color: var(--teal); }
.copy {
  flex: none; border: 1px solid var(--line); background: var(--surface-2); color: var(--muted);
  border-radius: 10px; width: 40px; height: 40px; display: grid; place-items: center; cursor: pointer;
}
.copy svg { width: 18px; height: 18px; }
.copy:hover { color: var(--text); }
.also { font-size: .85rem; color: var(--muted); }
.also bdi { color: var(--text); }
.means { margin: 10px 0 0; display: grid; gap: 4px; }
.means div { display: grid; grid-template-columns: 5.5em 1fr; gap: 8px; align-items: baseline; }
.means dt { font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
html[lang="ar"] .means dt { text-transform: none; letter-spacing: 0; font-size: .85rem; }
.means dd { margin: 0; font-size: 1rem; }
.ex { margin-top: 10px; padding: 8px 12px; background: var(--surface-2); border-radius: 10px; font-size: .92rem; }
.ex .ex-label { font-size: .75rem; color: var(--muted); font-weight: 600; }
.ex p { margin: 2px 0; }
.ex .ar { font-size: 1.1rem; }
.ex .lat { color: var(--teal); font-weight: 600; }
.note { margin: 8px 0 0; font-size: .85rem; color: var(--muted); }

/* word by word */
.words { display: flex; flex-wrap: wrap; gap: 8px; }
.word {
  display: flex; flex-direction: column; gap: 2px; min-width: 64px;
  border: 1.5px solid var(--line); background: var(--surface); border-radius: 12px; padding: 6px 10px;
}
.word .src { font-size: .8rem; color: var(--muted); }
.word .dst { font-weight: 600; }
.word .dst .ar { font-size: 1.05rem; }
.word.unknown { border-style: dashed; }
.word.unknown .dst, .word.skip .dst { color: var(--muted); font-weight: 400; font-size: .85rem; }
.word.close, .word.spelling { border-color: var(--teal); }

/* ---------- converter ---------- */
.lead { color: var(--muted); margin-top: 0; }
.cv-head { display: flex; justify-content: space-between; align-items: center; margin: 12px 0 6px; gap: 8px; }
.cv-out {
  display: block; min-height: 72px; padding: 14px 16px; border-radius: var(--radius);
  background: var(--surface); border: 1.5px dashed var(--line); font-size: 1.35rem; font-weight: 600;
  white-space: pre-wrap; word-break: break-word;
}
.cv-out[dir="rtl"] { font-family: var(--font-ar); font-size: 1.6rem; }
.hint { font-size: .85rem; color: var(--muted); }
.legend { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.legend li {
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 6px 12px;
}
.legend .k { font-weight: 700; color: var(--teal); direction: ltr; }
.legend .v { font-family: var(--font-ar); font-size: 1.3rem; }

/* ---------- browse ---------- */
.cats { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 4px; }
.cats .chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.cats .chip .n { opacity: .7; font-size: .8em; margin-inline-start: 4px; }
/* phones: browse categories in one row you can swipe */
@media (max-width: 759px) {
  #br-cats { flex-wrap: nowrap; overflow-x: auto; margin-inline: -16px; padding: 2px 16px 8px; scrollbar-width: none; }
  #br-cats::-webkit-scrollbar { display: none; }
  #br-cats .chip { flex: none; }
}

/* ---------- learn ---------- */
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { border: 0; background: transparent; padding: 10px; border-radius: 9px; font-weight: 600; color: var(--muted); cursor: pointer; min-height: 42px; }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 6px; text-align: center; display: flex; flex-direction: column; }
.stat .num { font-size: 1.5rem; font-weight: 700; line-height: 1.2; }
.stat .lbl { font-size: .72rem; color: var(--muted); font-weight: 600; }
.stat .sub { font-size: .68rem; color: var(--muted); }
.stat.streak .num { color: var(--accent); }
.boxes { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 12px; align-items: end; height: 64px; }
.boxes .bx { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 3px; }
.boxes .bar { width: 56%; background: var(--teal); border-radius: 6px 6px 3px 3px; min-height: 3px; opacity: .85; }
.boxes .bx span { font-size: .72rem; color: var(--muted); }
.msg { background: var(--accent-soft); padding: 10px 14px; border-radius: 12px; margin: 12px 0 0; }

.play-top { display: flex; justify-content: space-between; align-items: center; margin: 4px 0 12px; color: var(--muted); font-size: .9rem; }
.progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-bottom: 14px; }
.progress div { height: 100%; background: var(--accent); width: 0; transition: width .25s; }
.flashcard {
  width: 100%; min-height: 260px; border: 1px solid var(--line); border-radius: 20px; background: var(--surface);
  box-shadow: var(--shadow); padding: 22px 18px; cursor: pointer; text-align: center;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
}
.flashcard .big, .quiz-q .big { font-size: 1.9rem; font-weight: 700; line-height: 1.35; }
.flashcard .big.ar, .quiz-q .big.ar { font-size: 2.3rem; }
.flashcard .lat, .quiz-q .lat { color: var(--teal); font-weight: 600; font-size: 1.2rem; }
.flashcard .tap { color: var(--muted); font-size: .85rem; margin-top: 10px; }
.flashcard hr { width: 60%; border: 0; border-top: 1px dashed var(--line); margin: 10px 0; }
.flashcard .answer { font-size: 1.3rem; font-weight: 600; }
.flashcard .ex { text-align: start; width: 100%; }
.flashcard.flipped { animation: flip .28s ease; }
@keyframes flip { from { transform: rotateY(80deg); opacity: .3; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .flashcard.flipped { animation: none; } }
.fc-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.fc-actions .btn { min-height: 52px; font-size: 1rem; }
.again { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.knew { background: var(--good-soft); color: var(--good); border-color: transparent; }

.quiz-q { background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 22px 18px; text-align: center; box-shadow: var(--shadow); }
.quiz-q .ask { color: var(--muted); font-size: .9rem; margin: 0 0 8px; }
.options { display: grid; gap: 10px; margin-top: 14px; }
.option {
  text-align: start; border: 1.5px solid var(--line); background: var(--surface); border-radius: 14px;
  padding: 12px 16px; min-height: 52px; cursor: pointer; font-size: 1rem;
}
.option .ar { font-size: 1.2rem; }
.option .lat { color: var(--teal); font-weight: 600; }
.option.right { border-color: var(--good); background: var(--good-soft); }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.feedback { margin: 12px 2px 0; font-weight: 600; }
.feedback.right { color: var(--good); }
.feedback.wrong { color: var(--bad); }
.done { text-align: center; padding: 26px 16px; background: var(--surface); border-radius: 20px; border: 1px solid var(--line); }
.done .num { font-size: 2.6rem; font-weight: 800; color: var(--accent); }

/* ---------- footer / toast ---------- */
.foot { max-width: 760px; margin: 0 auto; padding: 8px 16px 24px; color: var(--muted); font-size: .8rem; text-align: center; }
.foot p { margin: 4px 0; }
.toast {
  position: fixed; inset-inline: 0; margin: 0 auto; width: max-content; max-width: 90vw;
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom, 0px)); z-index: 10;
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 999px; font-size: .9rem; font-weight: 600;
  opacity: 0; transform: translateY(8px); transition: opacity .2s, transform .2s; pointer-events: none;
}
.toast.show { opacity: 1; transform: none; }

/* ---------- desktop: tabs go under the header ---------- */
@media (min-width: 760px) {
  body { padding-bottom: 0; }
  .top { padding-inline: max(16px, calc((100vw - 760px) / 2)); }
  .tabs {
    position: sticky; top: 61px; bottom: auto; height: auto; padding: 8px 16px;
    display: flex; justify-content: center; gap: 6px; background: var(--bg); border-top: 0; border-bottom: 1px solid var(--line);
  }
  .tabs a { flex-direction: row; gap: 8px; padding: 8px 16px; border-radius: 999px; font-size: .92rem; }
  .tabs a[aria-current="page"] { background: var(--accent-soft); }
  .tabs a svg { width: 20px; height: 20px; }
  .toast { bottom: 24px; }
  main { padding-top: 22px; }
  .flashcard { min-height: 300px; }
}
