:root {
  --bg: #f6f1e7; --card: #fffdf8; --ink: #3a3122; --muted: #8a7d63; --line: #e6dccb;
  --brand: #6b5a2e; --brand-2: #8a7436; --wheat: #d9a441;
  --available: #4f9a52; --busy: #a39e93; --requested: #e0a226; --active: #3f7cbf; --overdue: #c9553f; --mine: #8e5bd0;
  --danger: #b5402c; --radius: 14px;
  color-scheme: light;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Naskh Arabic", Tahoma, sans-serif;
  font-size: 16px; line-height: 1.5;
}
#app { height: 100%; display: flex; flex-direction: column; }
button, input, select, textarea { font: inherit; color: inherit; }
h2, h3 { margin: .2em 0 .4em; }
h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.err { color: var(--danger); min-height: 1.4em; margin: .3em 0; }
.warn { color: #8a4b0e; background: #fff1d6; border-radius: 10px; padding: .5em .7em; }
.row { display: flex; align-items: center; }
.gap { gap: .5rem; }
.wrap { flex-wrap: wrap; }

/* ---------- gate (setup / join / unlock) */
.gate { flex: 1; display: flex; align-items: center; justify-content: center; padding: 20px; }
.gate-card { width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--line); border-radius: 20px; padding: 26px 22px; text-align: center; box-shadow: 0 6px 24px rgba(80, 60, 20, .08); }
.gate-card .lbl { text-align: right; }
.logo { font-size: 46px; line-height: 1; }
.brand-big { font-size: 30px; font-weight: 800; color: var(--brand); margin-bottom: 8px; }

/* ---------- inputs & buttons */
.lbl { display: block; font-size: .9rem; color: var(--muted); margin: .8em 0 .25em; }
.input { width: 100%; padding: .7em .8em; border: 1px solid var(--line); border-radius: 12px; background: #fff; outline: none; }
.input:focus { border-color: var(--brand-2); box-shadow: 0 0 0 3px rgba(138, 116, 54, .15); }
.input.small { width: auto; padding: .55em .6em; }
.input.pin { text-align: center; letter-spacing: .4em; font-size: 1.3rem; margin-bottom: .4em; direction: ltr; }
textarea.input { resize: vertical; }
.btn { border: 0; border-radius: 12px; padding: .7em 1.1em; font-weight: 700; cursor: pointer; background: #ece4d3; color: var(--ink); text-decoration: none; white-space: nowrap; display: inline-flex; align-items: center; justify-content: center; gap: .3em; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.danger { background: #f6dcd6; color: var(--danger); }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.small { padding: .45em .8em; font-size: .9rem; }
.btn.wide { width: 100%; margin-top: 1em; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: transparent; font-size: 1.25rem; width: 42px; height: 42px; border-radius: 12px; cursor: pointer; }
.icon-btn:active { background: rgba(0, 0, 0, .06); }

/* ---------- shell */
header.top { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px 6px 6px; padding-top: max(6px, env(safe-area-inset-top)); background: var(--card); border-bottom: 1px solid var(--line); }
.brand { display: flex; gap: .4em; align-items: center; font-size: 1.15rem; color: var(--brand); padding: 0 8px; }
.top-actions { display: flex; }
.banner { background: #fff1d6; color: #8a4b0e; padding: .5em 1em; font-size: .9rem; }
main { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }
main.full { overflow: hidden; position: relative; }
nav.tabs { display: flex; background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.tab { flex: 1; border: 0; background: none; padding: 7px 2px 6px; display: flex; flex-direction: column; align-items: center; gap: 1px; font-size: .75rem; color: var(--muted); position: relative; cursor: pointer; }
.tab .ic { font-size: 1.25rem; line-height: 1.2; filter: grayscale(1); opacity: .7; }
.tab.on { color: var(--brand); font-weight: 700; }
.tab.on .ic { filter: none; opacity: 1; }
.badge { position: absolute; top: 3px; left: calc(50% - 22px); background: var(--danger); color: #fff; border-radius: 10px; font-size: .7rem; min-width: 18px; padding: 0 5px; line-height: 18px; }

.page { padding: 12px 14px 24px; max-width: 760px; margin: 0 auto; }
.toolbar { display: flex; gap: .5rem; margin-bottom: .6rem; }
.chips { display: flex; gap: .4rem; overflow-x: auto; padding-bottom: .5rem; margin-bottom: .3rem; scrollbar-width: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); border-radius: 20px; padding: .3em .85em; font-size: .88rem; cursor: pointer; }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.chips .sep { flex: none; width: 1px; background: var(--line); margin: 0 .2rem; }
.rows { display: flex; flex-direction: column; gap: 6px; }
.row-item { display: flex; align-items: center; gap: .6rem; width: 100%; text-align: right; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: .65em .8em; cursor: pointer; }
.row-item.off { opacity: .55; }
.ri-main { flex: 1; min-width: 0; }
.ri-main b { font-size: 1.02rem; }
.chev { color: var(--muted); font-size: 1.4rem; }
.empty { text-align: center; padding: 40px 20px; }
.group h3 { margin-top: 1em; color: var(--brand-2); }
.group .row-item { margin-bottom: 6px; }

.pill { flex: none; font-size: .78rem; font-weight: 700; padding: .2em .65em; border-radius: 20px; background: #eee; white-space: nowrap; }
.pill.available { background: #dcefdc; color: #2f6b33; }
.pill.busy { background: #ebe8e1; color: #6f695c; }
.pill.requested { background: #fbedcc; color: #8a5d06; }
.pill.active { background: #dbe7f5; color: #24507e; }
.pill.overdue { background: #f6dcd6; color: #7d2e20; }
.pill.mine { background: #ece0fa; color: #5a2f94; }

.card { display: block; width: 100%; text-align: right; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 10px; }
button.card { cursor: pointer; }
.card.mine { border-right: 5px solid var(--mine); }
.card.requested { border-right: 5px solid var(--requested); }
.card.overdue { border-right: 5px solid var(--overdue); }
.card-top { display: flex; align-items: baseline; gap: .6rem; }
.big { font-size: 1.35rem; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 10px; }
.stat { background: #faf6ee; border-radius: 12px; padding: 8px; text-align: center; display: flex; flex-direction: column; }
.stat b { font-size: 1.4rem; }
.stat span { font-size: .8rem; color: var(--muted); }
.stat.available b { color: var(--available); } .stat.active b { color: var(--active); } .stat.overdue b { color: var(--overdue); } .stat.requested b { color: #a8740a; }
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th, .tbl td { padding: .35em .3em; border-bottom: 1px solid var(--line); text-align: center; }
.tbl td:first-child, .tbl th:first-child { text-align: right; }

.req { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: .6em .7em; margin-bottom: 8px; display: flex; align-items: center; gap: .5rem; justify-content: space-between; }
.req-info { border: 0; background: none; text-align: right; display: flex; flex-direction: column; cursor: pointer; padding: 0; flex: 1; }

/* ---------- map */
.map-wrap { position: absolute; inset: 0; }
svg.map { width: 100%; height: 100%; display: block; touch-action: none; background: #efe8d8; user-select: none; -webkit-user-select: none; }
.road { fill: none; stroke: #fffaf0; stroke-width: 8; stroke-linecap: round; stroke-linejoin: round; }
.bld { fill: #dccfb4; }
.t { fill-opacity: .45; stroke-width: 1.6; vector-effect: non-scaling-stroke; cursor: pointer; }
.t.available { fill: var(--available); stroke: #2f6b33; }
.t.busy { fill: var(--busy); stroke: #6f695c; }
.t.requested { fill: var(--requested); stroke: #8a5d06; }
.t.active { fill: var(--active); stroke: #24507e; }
.t.overdue { fill: var(--overdue); stroke: #7d2e20; }
.t.mine { fill: var(--mine); stroke: #4b2482; stroke-width: 3; fill-opacity: .55; }
.t.focus { fill: none; stroke: var(--brand); stroke-width: 3; }
.tl, .bn { font-weight: 800; fill: #2b2418; text-anchor: middle; dominant-baseline: central; paint-order: stroke; stroke: #fffaf0; stroke-linejoin: round; pointer-events: none; font-family: system-ui, sans-serif; }
.tb { fill: #c9b48c; stroke: #8f7a52; stroke-width: 1; vector-effect: non-scaling-stroke; }
.bn { fill: #5a2f94; }
.legend { position: absolute; top: 8px; right: 8px; left: 64px; display: flex; flex-wrap: wrap; gap: 4px; pointer-events: none; }
.lg { background: rgba(255, 253, 248, .92); border-radius: 14px; padding: 2px 9px; font-size: .78rem; display: inline-flex; align-items: center; gap: 5px; box-shadow: 0 1px 4px rgba(0, 0, 0, .08); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.available { background: var(--available); } .dot.busy { background: var(--busy); } .dot.requested { background: var(--requested); }
.dot.active { background: var(--active); } .dot.overdue { background: var(--overdue); } .dot.mine { background: var(--mine); }
.zoom { position: absolute; left: 8px; bottom: 12px; display: flex; flex-direction: column; gap: 6px; }
.zoom .icon-btn { background: rgba(255, 253, 248, .95); box-shadow: 0 1px 5px rgba(0, 0, 0, .15); font-weight: 700; }
.minimap { height: 280px; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); margin: 8px 0; }

/* ---------- sheets */
#sheet-root { position: fixed; inset: 0; pointer-events: none; z-index: 50; }
.sheet-wrap { position: absolute; inset: 0; pointer-events: auto; }
.sheet-bg { position: absolute; inset: 0; background: rgba(30, 24, 12, .35); opacity: 0; transition: opacity .2s; }
.sheet { position: absolute; left: 0; right: 0; bottom: 0; max-height: 92%; background: var(--card); border-radius: 20px 20px 0 0; transform: translateY(100%); transition: transform .2s ease-out; display: flex; flex-direction: column; max-width: 760px; margin: 0 auto; padding-bottom: env(safe-area-inset-bottom); }
.sheet-wrap.open .sheet { transform: none; }
.sheet-wrap.open .sheet-bg { opacity: 1; }
.sheet-head { display: flex; justify-content: flex-start; padding: 4px 6px 0; }
.sheet-body { overflow: auto; padding: 0 16px 18px; }
.t-head { display: flex; align-items: center; justify-content: space-between; gap: .6rem; }
.facts { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 8px 0; }
.fact { background: #faf6ee; border-radius: 10px; padding: 6px 9px; display: flex; flex-direction: column; font-size: .9rem; border: 0; text-align: right; }
.fact.link { cursor: pointer; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.blist summary { cursor: pointer; font-weight: 700; padding: .4em 0; }
.b-row { display: flex; gap: .7rem; padding: .3em .2em; border-bottom: 1px solid var(--line); }
.b-row b { min-width: 2.2em; color: var(--mine); }
.pick-list { max-height: 45vh; overflow: auto; margin: 6px 0; display: flex; flex-direction: column; gap: 4px; }
.pick { display: flex; justify-content: space-between; border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: .55em .7em; cursor: pointer; text-align: right; }
.pick.on { border-color: var(--brand); background: #f3ecdc; font-weight: 700; }
.qr-box { display: flex; justify-content: center; margin: 10px 0; }
svg.qr { width: min(280px, 80vw); height: auto; }
.confirm { padding-bottom: 6px; }

/* ---------- invitation scanner */
.scan-box { position: relative; margin: 10px auto; width: 100%; max-width: 320px; aspect-ratio: 1; border-radius: 16px; overflow: hidden; background: #222; }
.scan-video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-frame { position: absolute; inset: 18%; border: 3px solid rgba(255, 255, 255, .85); border-radius: 14px; box-shadow: 0 0 0 999px rgba(0, 0, 0, .25); }

/* ---------- image viewer */
.viewer { position: fixed; inset: 0; background: #111; z-index: 80; display: flex; flex-direction: column; }
.viewer-bar { color: #fff; padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top)); font-weight: 700; }
.viewer-scroll { flex: 1; overflow: auto; }
.viewer img.sat { display: block; max-width: none; width: 200%; }

/* ---------- toast */
#toast { position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%) translateY(20px); background: #2e2618; color: #fff; padding: .6em 1.1em; border-radius: 12px; opacity: 0; transition: .2s; pointer-events: none; z-index: 100; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%); }

@media (min-width: 900px) { .viewer img.sat { width: auto; max-width: 100%; margin: 0 auto; } }

@media print {
  body * { visibility: hidden; }
  .printable, .printable * { visibility: visible; }
  .printable { position: fixed; inset: 0; padding: 30px; background: #fff; }
  .noprint { display: none !important; }
}
