  :root {
    --bg: #16181d; --panel: #1e2128; --line: #2e323c; --txt: #e6e8ec;
    --dim: #9aa1ad; --ok: #35c46f; --bad: #ef5350; --skip: #c8a03c; --accent: #5b9dff;
  }
  /* בהיר מופיע פעמיים בכוונה: פעם לפי העדפת המערכת, ופעם לפי בחירה
     מפורשת. `:not([data-theme="dark"])` נחוץ כדי שבחירה בכהה תגבר
     על מערכת שנמצאת בבהיר — בלעדיו הכפתור לא עובד ביום. */
  @media (prefers-color-scheme: light) {
    :root:not([data-theme="dark"]) {
      --bg:#f7f8fa; --panel:#fff; --line:#dfe3ea; --txt:#1c1f26;
      --dim:#6b7280; --ok:#1a9e52; --bad:#d13c38; --skip:#a37a15; --accent:#2563eb; }
  }
  :root[data-theme="light"] {
    --bg:#f7f8fa; --panel:#fff; --line:#dfe3ea; --txt:#1c1f26;
    --dim:#6b7280; --ok:#1a9e52; --bad:#d13c38; --skip:#a37a15; --accent:#2563eb; }

  /* ── מחשבון צף ── */
  #calc-box.float {
    position: fixed; z-index: 60; width: min(560px, calc(100vw - 24px));
    background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
    padding: 10px 14px 4px; box-shadow: 0 10px 34px rgba(0,0,0,.32);
  }
  #calc-box.float h2 { margin-top: 4px; }
  .grip {
    cursor: grab; user-select: none; touch-action: none;
    color: var(--dim); font-size: 15px; padding: 0 6px; background: none;
    border: 0; font-family: inherit;
  }
  #calc-box.float .grip { cursor: grabbing; }
  .calc-head { display: flex; align-items: center; gap: 6px; }
  .calc-head h2 { margin: 26px 0 10px; flex: 1 1 auto; }
  #calc-box.float .calc-head h2 { margin: 4px 0 8px; }
  .pin { display: none; }
  #calc-box.float .pin { display: inline-block; }
  * { box-sizing: border-box; }
  /* סולם ריווח אחד לכל הדף — קודם כל מספר היה נבחר לכל מקום בנפרד */
  :root { --s1:4px; --s2:8px; --s3:14px; --s4:22px; --s5:34px; --r:10px; }
  /* `.bar { display:flex }` הוא כלל של המחבר, והוא גובר על
     `[hidden] { display:none }` של הדפדפן — בלי השורה הזאת שורת
     "בדוק" מוצגת גם כשהיא מוסתרת. */
  [hidden] { display: none !important; }
  body {
    margin: 0; padding: var(--s4) var(--s3) 140px; background: var(--bg); color: var(--txt);
    font: 15px/1.65 -apple-system, "Segoe UI", Roboto, "Noto Sans Hebrew", sans-serif;
    -webkit-text-size-adjust: 100%;
  }
  /* 780px היה צר מדי: שורת משוואה מבזבזת ~380px על תווית, שדות
     הפעולה/מקור, סימון ושני כפתורים — ולמתמטיקה נשארו 402px.
     משוואה עם שני שברים וסוגריים נחתכה. */
  .wrap { max-width: 1120px; margin: 0 auto; }
  h1 { font-size: 19px; margin: 0 0 4px; font-weight: 600;
       display: flex; align-items: center; gap: 10px; }
  .logo { width: 30px; height: 30px; flex: 0 0 auto; color: var(--accent); }
  button.act.theme { margin-inline-start: auto; padding: 5px 10px; font-size: 15px; line-height: 1; }
  .sub { color: var(--dim); font-size: 13px; margin-bottom: 22px; }
  h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .09em;
       color: var(--dim); margin: var(--s5) 0 var(--s2); font-weight: 700; }

  /* כל אזור הוא כרטיס. קודם השורות צפו על הרקע והגבול בין "המשוואות"
     ל"הפתרון" היה כותרת בלבד. */
  .card {
    background: color-mix(in srgb, var(--panel) 55%, transparent);
    border: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
    border-radius: var(--r); padding: var(--s3) var(--s3) var(--s2);
  }

  .row { display: flex; align-items: center; gap: var(--s2); margin-bottom: var(--s1); padding: 3px 0; }
  /* השורה שבמוקד מודגשת קלות — בתרגיל בן 15 שורות קשה לאתר איפה עומדים */
  .row:focus-within { background: color-mix(in srgb, var(--accent) 6%, transparent);
                      border-radius: var(--r); }
  /* התווית והשדות הקטנים מאבדים בולטות: המתמטיקה היא העיקר */
  .tag {
    flex: 0 0 auto; width: 40px; padding: 7px 2px; text-align: center;
    background: transparent; color: var(--dim); border: 1px solid var(--line);
    border-radius: var(--r); font: inherit; font-size: 13px; cursor: pointer;
  }
  .tag:hover { color: var(--txt); border-color: var(--accent); }
  math-field {
    /* הרוחב נקבע ב-fitWidth: `width:fit-content` **אינו עובד** כאן —
       נמדד שהוא נותן 110px גם ל-`x=1` וגם לביטוי בן 24 תווים, כי
       MathLive אינה מפיצה רוחב פנימי מה-shadow DOM החוצה. */
    flex: 0 1 auto; min-width: 0; max-width: 100%; direction: ltr; text-align: left;
    background: var(--panel); color: var(--txt);
    border: 1px solid var(--line); border-radius: 7px; padding: 7px 10px;
    font-size: 17px;
  }
  math-field:focus-within {
    border-color: var(--accent); outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent);
  }
  math-field::part(virtual-keyboard-toggle) { color: var(--dim); }
  .aux {
    flex: 0 0 auto; direction: ltr; text-align: left;
    background: transparent; color: var(--txt); border: 1px dashed var(--line);
    border-radius: var(--r); padding: 8px; font: inherit; font-size: 13px;
  }
  .aux:hover { border-style: solid; }
  .aux.op { width: 92px; }
  .aux.src { width: 78px; }
  .aux::placeholder { color: var(--dim); opacity: .7; }
  .aux:focus { border-color: var(--accent); outline: none; }
  .spacer { flex: 0 0 auto; width: 78px; }
  /* `position:fixed` ולא `absolute`: אלמנט absolute ב-left:-9999px
     בתוך מסמך RTL מרחיב את שטח הגלילה, והדף קיבל גלילה אופקית של
     22,000 פיקסלים. fixed אינו משתתף בחישוב הגלילה. */
  .ruler {
    position: fixed; left: 0; top: 0; visibility: hidden; pointer-events: none;
    white-space: nowrap; font-size: 17px; direction: ltr;
  }

  /* ── מסך רחב: הבלוק ממורכז ── */
  /* מאז שהשדות מתאימים את רוחבם לתוכן, השורות קצרות ונדבקו לימין
     בתוך מכל של 1120px. **מרכוז כל שורה בנפרד היה שובר את היישור**:
     לשורות יש רוחב שונה, והתווית, הסימון והכפתורים לא היו נופלים זה
     מתחת לזה. לכן הכרטיס עצמו מתכווץ לתוכנו וממורכז, והעמודות בתוכו
     נשארות מיושרות. min-width מייצב אותו כדי שלא יקפוץ בכל הקשה. */
  @media (min-width: 700px) {
    .card {
      width: fit-content; min-width: min(760px, 100%); max-width: 100%;
      margin-inline: auto;
    }
    .bar { justify-content: center; }
    h2, .sub, .ver, .done { text-align: center; }
    #next-out, .detail { max-width: min(760px, 100%); margin-inline: auto; }
  }

  /* מסך צר: המתמטיקה מקבלת שורה שלמה, השדות הקטנים יורדים מתחתיה */
  @media (max-width: 640px) {
    .row { flex-wrap: wrap; }
    math-field { flex: 1 1 100%; order: 1; }
    .tag { order: 0; }
    .mark { order: 0; margin-inline-start: auto; }
    .del { order: 0; }
    /* min-width:0 חובה — ל-input יש רוחב מינימלי מובנה שמכריח גלישה */
    .aux { order: 2; flex: 1 1 0; min-width: 0; }
    .aux.op, .aux.src { width: auto; }
    .spacer { display: none; }
    .note { padding-inline-start: 0; }
  }
  .mark {
    flex: 0 0 auto; width: 26px; text-align: center; font-size: 19px; font-weight: 700;
  }
  .mark.ok { color: var(--ok); } .mark.bad { color: var(--bad); }
  .mark.skip { color: var(--skip); } .mark.none { color: var(--dim); opacity: .45; }
  .mark.warn { color: var(--skip); }

  .note {
    margin: -3px 0 9px 0; padding-inline-start: 52px;
    font-size: 12.5px; color: var(--dim); min-height: 1px;
  }
  .note.bad { color: var(--bad); }
  .note.warn { color: var(--skip); }

  .bar { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
  button.act {
    background: var(--panel); color: var(--txt); border: 1px solid var(--line);
    border-radius: var(--r); padding: 9px 15px; font: inherit; font-size: 13px;
    cursor: pointer; transition: border-color .12s, color .12s;
  }
  button.act:hover { border-color: var(--accent); color: var(--accent); }

  .done {
    margin-top: 20px; padding: 13px 16px; border-radius: 9px;
    background: color-mix(in srgb, var(--ok) 14%, transparent);
    border: 1px solid color-mix(in srgb, var(--ok) 45%, transparent);
    color: var(--ok); font-weight: 600; display: none;
  }
  .done.show { display: block; }

  /* התוצאה מתכווצת לתוכנה במקום לשריין 170px קבועים — כך שדה הקלט
     מקבל את כל מה שנשאר, וגדל ככל שמקלידים. ריק = רוחב אפס. */
  .calc-out {
    flex: 0 1 auto; min-width: 0; white-space: nowrap; direction: ltr; text-align: left;
    font-size: 17px; font-weight: 600; color: var(--ok); padding-inline-start: 4px;
  }
  .calc-out.dim { color: var(--dim); font-weight: 400; font-size: 14px; }

  .del {
    flex: 0 0 auto; width: 30px; height: 30px; padding: 0; line-height: 1;
    background: transparent; color: var(--dim); border: 1px solid transparent;
    border-radius: 8px; font: inherit; font-size: 17px; cursor: pointer;
    opacity: .55; transition: opacity .12s;
  }
  /* הכפתורים מתעוררים רק כשעומדים על השורה — קודם שלושה סמלים
     התחרו על תשומת הלב מול המתמטיקה בכל שורה בו-זמנית */
  .row:hover .del, .row:focus-within .del { opacity: 1; }
  .del:hover { color: var(--bad); border-color: var(--bad); opacity: 1; }
  .del.paste:hover, .del.dup:hover { color: var(--accent); border-color: var(--accent); }
  /* במסך מגע אין ריחוף, ולכן הם גלויים תמיד וגדולים יותר */
  @media (hover: none) {
    .del { opacity: .8; width: 36px; height: 36px; }
  }

  /* ── הגדרות ── */
  .opts {
    background: var(--panel); border: 1px solid var(--line);
    border-radius: 9px; padding: 0 14px; margin-bottom: 20px;
  }
  .opts > summary {
    cursor: pointer; padding: 10px 0; font-size: 13px; color: var(--dim);
    list-style: none;
  }
  .opts > summary::-webkit-details-marker { display: none; }
  .opts > summary::before { content: "⚙ "; }
  .opts[open] > summary { border-bottom: 1px solid var(--line); margin-bottom: 10px; }
  .opt { margin-bottom: 12px; }
  .opt label { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; }
  .opt input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
  .help-line { color: var(--dim); font-size: 13.5px; margin-bottom: 9px; }
  .help-line strong { color: var(--txt); }
  .opt .why-hint {
    color: var(--dim); font-size: 12.5px; padding-inline-start: 24px; margin-top: 2px;
  }
  .opt.soon label { opacity: .5; cursor: default; }
  .opt.soon input { cursor: default; }

  /* ── "איפה?" ── */
  button.why {
    margin-inline-start: 8px; padding: 1px 8px; font: inherit; font-size: 12px;
    background: transparent; color: var(--accent); cursor: pointer;
    border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent); border-radius: 20px;
  }
  button.why:hover { background: color-mix(in srgb, var(--accent) 14%, transparent); }
  .detail {
    margin: 2px 0 12px; padding: 10px 13px;
    padding-inline-start: 52px; border-radius: 8px;
    background: color-mix(in srgb, var(--accent) 7%, transparent);
    border: 1px solid color-mix(in srgb, var(--accent) 22%, transparent);
    font-size: 13px;
  }
  @media (max-width: 640px) { .detail { padding-inline-start: 13px; } }
  #next-out { padding-inline-start: 13px; }
  #next-out .d-say { font-size: 15px; font-weight: 600; }
  #next-out .why-hint { padding: 0; margin-top: 6px; color: var(--dim); font-size: 12.5px; }
  #next-out .d-row.sol .d-lab { color: var(--ok); font-weight: 600; }
  #next-out .d-row.sol .d-tex { color: var(--ok); }
  .detail .d-row { display: flex; gap: 9px; align-items: baseline; margin: 3px 0; flex-wrap: wrap; }
  .detail .d-lab { color: var(--dim); flex: 0 0 auto; }
  .detail .d-tex { direction: ltr; unicode-bidi: isolate; font-size: 15px; }
  .detail .d-say { margin: 3px 0; }
  .detail .d-say.hit { color: var(--bad); font-weight: 600; }
  .detail .d-row.hit .d-lab { color: var(--bad); font-weight: 600; }
  .detail .d-row.hit .d-tex { color: var(--bad); }

  button.act.go { border-color: var(--accent); color: var(--accent); font-weight: 600; }
  .stale { align-self: center; font-size: 12.5px; color: var(--dim); }

  /* ── הסבר מפורק ── */
  button.caret {
    flex: 0 0 auto; margin-inline-end: 5px; padding: 0 3px; width: 17px;
    background: transparent; color: var(--dim); border: 0; border-radius: 4px;
    font: inherit; font-size: 11px; line-height: 1; cursor: pointer;
  }
  button.caret:hover { color: var(--accent); }
  .expl {
    margin: -2px 0 10px; padding-inline-start: 52px;
    font-size: 12.5px; color: var(--dim);
  }
  @media (max-width: 640px) { .expl { padding-inline-start: 0; } }
  .expl-head { color: var(--txt); opacity: .85; font-weight: 600; margin-top: 3px; }
  .expl-item { padding-inline-start: 14px; position: relative; line-height: 1.75; }
  .expl-item::before {
    content: "•"; position: absolute; inset-inline-start: 2px; opacity: .5;
  }
  .expl-item.bare { padding-inline-start: 0; }
  .expl-item.bare::before { content: none; }
  /* בידוד: מינוס צמוד למספר בתוך משפט עברי נדבק לצד השני */
  .ltr { direction: ltr; unicode-bidi: isolate; }

  .copy-msg { align-self: center; font-size: 13px; }
  .copy-msg.ok { color: var(--ok); } .copy-msg.bad { color: var(--bad); }
  @media (max-width: 640px) {
    .calc-out { flex: 1 1 100%; order: 3; min-width: 0; padding-inline-start: 0; }
  }
  .err { color: var(--bad); font-size: 13px; margin-top: 14px; white-space: pre-wrap; }

  .ver {
    margin-top: 34px; padding-top: 12px; border-top: 1px solid var(--line);
    color: var(--dim); font-size: 11.5px; opacity: .75;
  }
  .ver span { direction: ltr; unicode-bidi: isolate; }

  /* יומן אבחון — ?debug=1 */
  #dbg {
    direction: ltr; text-align: left; white-space: pre-wrap; margin-top: 12px;
    padding: 10px; max-height: 300px; overflow: auto;
    background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
    color: var(--dim); font: 11px/1.5 ui-monospace, monospace;
  }

  /* ספינר: מכסה עד שהמנוע ו-MathLive נטענו. נמחק מה-DOM, לא רק מוסתר. */
  #boot {
    position: fixed; inset: 0; background: var(--bg); z-index: 99;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  }
  #boot .ring {
    width: 34px; height: 34px; border-radius: 50%;
    border: 3px solid var(--line); border-top-color: var(--accent);
    animation: spin .8s linear infinite;
  }
  #boot .msg { color: var(--dim); font-size: 13px; }
  @keyframes spin { to { transform: rotate(360deg); } }
  @media (prefers-reduced-motion: reduce) { #boot .ring { animation-duration: 2.4s; } }
