@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Arabic:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap');

/* Layout: top bar + tab strip; each tab is a full-height work area. Editor = canvas + side panel. */
:root {
  --bg: #f3f5f7;
  --panel: #ffffff;
  --ink: #17212b;
  --muted: #5d6b79;
  --line: #d6dde4;
  --accent: #1f5f8b;
  --accent-ink: #ffffff;
  --accent-soft: #e3eef6;
  --warn: #b25b00;
  --bad: #b3261e;
  --good: #2e7d4f;
  --canvas: #fbfcfd;
  --cad: #9aa6b2;
  --sans: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, Arial, sans-serif;
  --mono: "IBM Plex Mono", Consolas, "Courier New", monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11161c; --panel: #19212a; --ink: #e5ebf0; --muted: #98a6b3; --line: #2c3845;
    --accent: #5aa3d6; --accent-ink: #0c1117; --accent-soft: #1d3446; --warn: #f0a24a; --bad: #ff7a70; --good: #6fcf97;
    --canvas: #0e1318; --cad: #4f5d6a; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #11161c; --panel: #19212a; --ink: #e5ebf0; --muted: #98a6b3; --line: #2c3845;
  --accent: #5aa3d6; --accent-ink: #0c1117; --accent-soft: #1d3446; --warn: #f0a24a; --bad: #ff7a70; --good: #6fcf97;
  --canvas: #0e1318; --cad: #4f5d6a; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--sans); font-size: 14px; line-height: 1.5; }
button, input, select, textarea { font: inherit; color: inherit; }
.num, .mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* top bar */
.top { display: flex; align-items: center; gap: 16px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 16px; letter-spacing: .01em; }
.brand small { font-weight: 400; color: var(--muted); margin-inline-start: 8px; font-size: 12px; }
.proj { margin-inline-start: auto; display: flex; gap: 8px; align-items: center; }
.tabs { display: flex; gap: 2px; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 10px 14px; border-bottom: 2px solid transparent; cursor: pointer; color: var(--muted); white-space: nowrap; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.tabs button:disabled { opacity: .4; cursor: default; }
main { padding: 16px; }
section[hidden] { display: none !important; }

/* generic */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 14px 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.stack { display: grid; gap: 14px; }
h2 { font-size: 15px; margin: 0 0 10px; }
h3 { font-size: 13px; margin: 0 0 6px; color: var(--muted); font-weight: 600; }
p.hint { color: var(--muted); margin: 4px 0 10px; font-size: 13px; max-width: 80ch; }
.btn { background: var(--panel); border: 1px solid var(--line); border-radius: 5px; padding: 6px 12px; cursor: pointer; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 2px 8px; font-size: 12px; }
.btn.on { background: var(--accent-soft); border-color: var(--accent); }
.btn:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input, select, textarea { background: var(--panel); border: 1px solid var(--line); border-radius: 4px; padding: 4px 7px; }
input.sm { width: 7em; }
label { font-size: 12px; color: var(--muted); }
.pill { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; background: var(--accent-soft); }
.pill.ready { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); }
.pill.error { background: color-mix(in srgb, var(--bad) 18%, transparent); color: var(--bad); }
.pill.busy { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.swatch { display: inline-block; width: 12px; height: 12px; border: 1px solid var(--line); border-radius: 2px; vertical-align: -1px; }

/* tables */
.tbl-wrap { overflow: auto; max-height: calc(100vh - 230px); border: 1px solid var(--line); border-radius: 6px; background: var(--panel); }
table.t { border-collapse: collapse; width: 100%; font-size: 12.5px; }
table.t th { position: sticky; top: 0; background: var(--accent); color: var(--accent-ink); text-align: start; padding: 6px 8px; font-weight: 600; z-index: 1; white-space: nowrap; }
table.t td { border-top: 1px solid var(--line); padding: 4px 8px; vertical-align: top; }
table.t td.num { text-align: end; white-space: nowrap; }
table.t tr.muted td { color: var(--muted); }
table.t tr.sel td { background: var(--accent-soft); }
table.t input, table.t select { width: 100%; min-width: 5em; padding: 2px 5px; }
table.t .ltr { direction: ltr; text-align: left; }
.calc { font-family: var(--mono); font-size: 11.5px; direction: ltr; text-align: left; max-width: 60ch; }

/* drop zone */
.drop { border: 2px dashed var(--line); border-radius: 8px; padding: 22px; text-align: center; color: var(--muted); cursor: pointer; }
.drop.over { border-color: var(--accent); background: var(--accent-soft); }

/* editor */
.editor { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 12px; height: calc(100vh - 150px); min-height: 480px; }
.canvas-box { position: relative; background: var(--canvas); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; direction: ltr; }
.canvas-box canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.toolbar { position: absolute; top: 8px; left: 8px; display: flex; gap: 4px; flex-wrap: wrap; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 4px; direction: rtl; max-width: calc(100% - 16px); }
.coords { position: absolute; bottom: 6px; left: 8px; font: 11px var(--mono); color: var(--muted); background: var(--panel); padding: 1px 6px; border-radius: 3px; }
.tip { position: absolute; pointer-events: none; background: var(--ink); color: var(--panel); font-size: 12px; padding: 3px 7px; border-radius: 4px; white-space: nowrap; direction: ltr; }
.side { overflow: auto; display: grid; gap: 12px; align-content: start; min-width: 0; }
.zone-item { border: 1px solid var(--line); border-radius: 5px; padding: 6px 8px; cursor: pointer; display: grid; gap: 4px; }
.zone-item.on { border-color: var(--accent); background: var(--accent-soft); }
.zone-item .head { display: flex; gap: 6px; align-items: center; }
.rect-list { font: 11.5px var(--mono); direction: ltr; display: grid; gap: 2px; }
.rect-list div { display: flex; gap: 4px; align-items: center; }
.rect-list input { width: 5.6em; font: 11px var(--mono); padding: 1px 3px; }

/* results */
.summary { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 8px; }
.summary .tile { border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; background: var(--panel); }
.summary .tile b { font-family: var(--mono); font-size: 18px; display: block; direction: ltr; text-align: right; }
.qa li { margin-bottom: 3px; }

/* login */
body.login { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 28px; display: grid; gap: 10px; width: min(360px, 100%); }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0 0 8px; color: var(--muted); }
.login-card button { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 5px; padding: 8px; font-weight: 600; cursor: pointer; }
.err { color: var(--bad); }
.toast { position: fixed; bottom: 16px; inset-inline-start: 16px; background: var(--ink); color: var(--panel); padding: 8px 14px; border-radius: 6px; z-index: 50; }

@media (max-width: 900px) {
  .editor { grid-template-columns: 1fr; height: auto; }
  .canvas-box { height: 60vh; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
