:root {
  --bg: #efeae1;
  --paper: #fffdf8;
  --ink: #26221c;
  --accent: #1f5f8b;
  --accent-dark: #17496b;
  --accent-text: #1f5f8b;
  --accent2: #b3541e;
  --gold: #ffd696;
  --border: #ddd5c5;
  --tooltip-bg: #2b2721;
  --tooltip-ink: #f5f1e8;
  --panel-bg: #faf7f0;
  --brand-grad: linear-gradient(135deg, #16324a 0%, #1f5f8b 70%, #27729f 100%);
  --heb-font: "SBL Hebrew", "Ezra SIL", "David", "Frank Ruehl", "Noto Sans Hebrew", serif;

  --toolbar-grad: linear-gradient(#fffefb, #f6f2e9);
  --statusbar-grad: linear-gradient(#fbf8f2, #f3eee3);
  --btn-ink: #4a4232;
  --btn-hover-bg: #efe7d7;
  --btn-active-bg: #e7ddc9;
  --select-bg: #fff;
  --select-hover-border: #c4baa4;
  --toggle-track: #cfc6b2;
  --muted: #6f6656;
  --muted2: #857b66;
  --heading-ink: #1d3a52;
  --quote-ink: #555;
  --selection: #cfe3f2;
  --hl: #fdeec9;
  --editor-glow: rgba(31,95,139,.045);
  --card-bg: #fff;
  --card-h4-bg: #f2f6f9;
  --card-h4-border: #e2e9ef;
  --root-badge-bg: #eef4f8;
  --root-badge-border: #c8dce8;
  --ety-bg: #fbf6ea;
  --err: #a33;
}

:root[data-theme="dark"] {
  --bg: #191712;
  --paper: #23201a;
  --ink: #e6dfd1;
  --accent: #2f7cb0;
  --accent-dark: #3e8dc2;
  --accent-text: #6db3e8;
  --border: #3b352a;
  --panel-bg: #201d18;

  --toolbar-grad: linear-gradient(#262319, #1e1b15);
  --statusbar-grad: linear-gradient(#211e18, #1b1914);
  --btn-ink: #c9c0ac;
  --btn-hover-bg: #322e24;
  --btn-active-bg: #3a3529;
  --select-bg: #2a2720;
  --select-hover-border: #55503f;
  --toggle-track: #4a4437;
  --muted: #9a917c;
  --muted2: #8d8470;
  --heading-ink: #9fc3de;
  --quote-ink: #b0a893;
  --selection: #2c4a63;
  --hl: #5a4a1e;
  --editor-glow: rgba(109,179,232,.05);
  --card-bg: #2a2720;
  --card-h4-bg: #253039;
  --card-h4-border: #32424f;
  --root-badge-bg: #223240;
  --root-badge-border: #35516a;
  --ety-bg: #2e2a1f;
  --err: #e08080;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg); color: var(--ink);
}

/* Toolbar */
#tb-brand { padding-left: 2px; }
#brand-logo { border-radius: 5px; display: block; }
#toolbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 7px 12px;
  background: var(--toolbar-grad);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.tb-group { display: flex; align-items: center; gap: 2px; padding: 0 7px; border-right: 1px solid var(--border); }
.tb-group:last-child { border-right: none; }
.tb-spacer { flex: 1; }

#toolbar button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-width: 32px; height: 31px; padding: 0 8px;
  border: 1px solid transparent; border-radius: 7px;
  background: transparent; color: var(--btn-ink); font-size: 14px; cursor: pointer;
  transition: background .12s, border-color .12s, transform .06s;
}
#toolbar button:hover { background: var(--btn-hover-bg); border-color: var(--border); color: var(--ink); }
#toolbar button:active { transform: translateY(1px); background: var(--btn-active-bg); }
#toolbar button svg {
  width: 17px; height: 17px; fill: none;
  stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round;
}
#toolbar button svg .dot { fill: currentColor; stroke: none; }
#toolbar button svg .thin { stroke-width: 1.3; }
#toolbar button svg .hl-bar { stroke: var(--accent2); stroke-width: 2.6; }
.txtbtn { font-size: 15px !important; font-family: Georgia, serif; }
.dirbtn { font-size: 12.5px !important; font-weight: 600; letter-spacing: .02em; }
.dirbtn svg { width: 14px !important; height: 14px !important; }

/* Theme toggle: show one icon per theme */
#btn-theme .icon-sun { display: none; }
:root[data-theme="dark"] #btn-theme .icon-sun { display: block; }
:root[data-theme="dark"] #btn-theme .icon-moon { display: none; }

#toolbar button.accent {
  background: var(--accent); color: #fff; font-weight: 600;
  padding: 0 14px; border-radius: 16px;
  box-shadow: 0 2px 6px rgba(31,95,139,.35);
}
#toolbar button.accent:hover { background: var(--accent-dark); border-color: transparent; color: #fff; }

#toolbar select {
  height: 31px; padding: 0 6px; max-width: 150px;
  border: 1px solid var(--border); border-radius: 7px;
  background: var(--select-bg); color: var(--ink); font-size: 13px; cursor: pointer;
}
#toolbar select:hover { border-color: var(--select-hover-border); }

/* Hover-lookup toggle switch */
.toggle { font-size: 12.5px; display: flex; gap: 7px; align-items: center; cursor: pointer; color: var(--muted); user-select: none; }
.toggle input { display: none; }
.toggle-track {
  width: 32px; height: 18px; border-radius: 9px;
  background: var(--toggle-track); position: relative; transition: background .15s;
}
.toggle-thumb {
  position: absolute; top: 2px; left: 2px; width: 14px; height: 14px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3);
  transition: left .15s;
}
.toggle input:checked + .toggle-track { background: var(--accent); }
.toggle input:checked + .toggle-track .toggle-thumb { left: 16px; }

/* Main layout */
#main { flex: 1; display: flex; min-height: 0; }
#editor-wrap {
  flex: 1; overflow-y: auto; display: flex; justify-content: center;
  padding: 30px 16px;
  background:
    radial-gradient(ellipse at top, var(--editor-glow), transparent 60%),
    var(--bg);
}
#editor {
  width: 100%; max-width: 820px; min-height: 950px;
  background: var(--paper);
  border: 1px solid var(--border); border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08), 0 10px 34px rgba(0,0,0,.13);
  padding: 68px 76px; outline: none;
  font-size: 17px; line-height: 1.75;
}
#editor p, #editor h1, #editor h2, #editor h3, #editor blockquote { margin: 0 0 .55em; }
#editor h1, #editor h2, #editor h3 { font-family: Georgia, "Times New Roman", serif; color: var(--heading-ink); }
#editor blockquote { border-inline-start: 3px solid var(--accent); padding-inline-start: 12px; color: var(--quote-ink); }
#editor [dir="rtl"], #editor *:dir(rtl) { font-family: var(--heb-font); font-size: 1.12em; }
#editor ::selection { background: var(--selection); }
.hword { background: var(--hl); border-radius: 3px; }

/* Tooltip */
#tooltip {
  position: fixed; z-index: 50; max-width: 380px;
  background: var(--tooltip-bg); color: var(--tooltip-ink);
  border-radius: 10px; padding: 10px 13px; font-size: 13.5px; line-height: 1.5;
  border: 1px solid rgba(255,214,150,.25);
  box-shadow: 0 6px 22px rgba(0,0,0,.4); pointer-events: none;
}
#tooltip .tt-word { font-weight: 700; font-size: 15px; color: var(--gold); }
#tooltip .tt-word.he { font-family: var(--heb-font); font-size: 17px; direction: rtl; }
#tooltip .tt-translit { color: #d9c79a; font-style: italic; margin-inline-start: 6px; font-size: 12.5px; }
#tooltip .tt-pos { color: #9fc1d9; font-size: 12px; margin-inline-start: 6px; }
#tooltip .tt-def { margin-top: 3px; }
#tooltip .tt-src { margin-top: 6px; padding-top: 5px; border-top: 1px solid rgba(255,255,255,.12); font-size: 10.5px; color: #a89f8d; }
#tooltip .tt-loading { color: #cbc2b0; font-style: italic; }

/* Floating explain button */
#floatbtn {
  position: fixed; z-index: 49;
  background: var(--accent); color: #fff; border: none; border-radius: 20px;
  padding: 6px 14px; font-size: 13px; font-weight: 600; cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,.28);
}
#floatbtn:hover { background: var(--accent-dark); }

/* Side panel */
#panel {
  width: 420px; min-width: 300px; border-left: 1px solid var(--border);
  background: var(--panel-bg); display: flex; flex-direction: column;
}
#panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; font-weight: 700; color: #fff;
  background: var(--brand-grad);
  border-bottom: 2px solid var(--accent2);
}
#panel-close { border: none; background: rgba(255,255,255,.12); color: #fff; cursor: pointer; font-size: 13px; width: 24px; height: 24px; border-radius: 50%; }
#panel-close:hover { background: rgba(255,255,255,.25); }
#panel-body { flex: 1; overflow-y: auto; padding: 14px 16px; font-size: 14px; line-height: 1.6; }

.card {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; margin-bottom: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.card h4 {
  margin: -12px -14px 10px; padding: 7px 14px;
  font-size: 11.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--accent-text);
  background: var(--card-h4-bg); border-bottom: 1px solid var(--card-h4-border);
  border-radius: 10px 10px 0 0;
}
.card .hw { font-size: 21px; font-weight: 700; }
.card .hw.he { font-family: var(--heb-font); font-size: 26px; direction: rtl; }
.card .meta { color: var(--muted); font-size: 12.5px; margin: 2px 0 6px; }
.card .root-badge {
  display: inline-block; background: var(--root-badge-bg); border: 1px solid var(--root-badge-border); color: var(--accent-text);
  font-family: var(--heb-font); font-size: 18px; border-radius: 6px; padding: 1px 10px; margin: 3px 0;
  direction: rtl;
}
.card ol { margin: 4px 0 4px 20px; padding: 0; }
.card ol li { margin-bottom: 3px; }
.card .ety { background: var(--ety-bg); border-inline-start: 3px solid var(--accent2); padding: 7px 10px; border-radius: 4px; margin-top: 7px; font-size: 13px; }
.card .ety b { color: var(--accent2); }
.card .attribution { margin-top: 7px; font-size: 10.5px; color: var(--muted2); }
.card .trans-text { font-size: 15px; }
.panel-loading { color: var(--muted2); font-style: italic; padding: 8px 2px; }
.panel-note { font-size: 12px; color: var(--muted2); margin-top: 4px; }
.err { color: var(--err); font-size: 13px; }

/* Status bar */
#statusbar {
  display: flex; gap: 14px; align-items: center;
  padding: 5px 14px; font-size: 12px; color: var(--muted);
  background: var(--statusbar-grad);
  border-top: 1px solid var(--border);
}
#st-file { font-weight: 600; color: var(--btn-ink); }
#st-dirty { color: var(--accent2); font-size: 14px; }
#st-lang { color: var(--accent-text); font-weight: 600; }

/* Help modal */
#help-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center;
}
#help-modal {
  width: 580px; max-width: 92vw; max-height: 84vh; overflow-y: auto;
  background: var(--paper); color: var(--ink);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 18px 60px rgba(0,0,0,.45);
}
#help-header {
  position: sticky; top: 0;
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 16px; font-weight: 700; color: #fff;
  background: var(--brand-grad);
}
#help-close { border: none; background: rgba(255,255,255,.15); color: #fff; cursor: pointer; font-size: 13px; width: 24px; height: 24px; border-radius: 50%; }
#help-close:hover { background: rgba(255,255,255,.3); }
#help-body { padding: 6px 18px 16px; font-size: 13.5px; line-height: 1.55; }
#help-body h4 {
  margin: 14px 0 4px; font-size: 11.5px; text-transform: uppercase;
  letter-spacing: .07em; color: var(--accent-text);
}
#help-body table { width: 100%; border-collapse: collapse; }
#help-body td { padding: 5px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
#help-body td:first-child { white-space: nowrap; color: var(--muted); }
#help-body kbd {
  background: var(--btn-hover-bg); border: 1px solid var(--border);
  border-radius: 4px; padding: 0 5px; font-family: inherit; font-size: 12px; color: var(--ink);
}

/* Pronounce buttons + transliteration/letter cards */
.speak {
  display: inline-flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; margin-inline-start: 8px;
  border: 1px solid var(--border); border-radius: 50%;
  background: var(--card-h4-bg); cursor: pointer; font-size: 13px;
  vertical-align: middle;
}
.speak:hover { background: var(--btn-hover-bg); }
.translit-big { font-size: 19px; font-style: italic; }
.letter-big { font-family: var(--heb-font); font-size: 46px; line-height: 1.15; }

.hidden { display: none !important; }

/* ---------------- Mobile (phones / small tablets) ---------------- */
@media (max-width: 700px) {
  /* Toolbar: one scrollable row instead of wrapping into many */
  #toolbar {
    flex-wrap: nowrap; overflow-x: auto; gap: 4px; padding: 6px 8px;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  #toolbar::-webkit-scrollbar { display: none; }
  .tb-group { padding: 0 4px; flex-shrink: 0; }
  .toggle { white-space: nowrap; }
  /* iOS zooms into controls with font-size < 16px — avoid that */
  #toolbar select { font-size: 16px; max-width: 116px; }

  #editor-wrap { padding: 10px 6px; }
  #editor {
    padding: 20px 16px; min-height: 70vh;
    font-size: 16px; border-radius: 4px;
  }

  /* Explain panel becomes a bottom sheet */
  #panel {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    width: 100%; min-width: 0; height: 62vh; z-index: 60;
    border-left: none; border-radius: 14px 14px 0 0; overflow: hidden;
    box-shadow: 0 -8px 30px rgba(0,0,0,.35);
  }
  #panel-body { padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px)); }

  #tooltip { max-width: 92vw; }
  #help-modal { max-width: 96vw; max-height: 88vh; }
  #statusbar { padding-bottom: calc(5px + env(safe-area-inset-bottom, 0px)); }
}
