:root {
  --bg: #f4f8f8; --surface: #ffffff; --text: #12312f; --muted: #4f6b69;
  --accent: #0d6e6e; --accent-soft: #d8ecec; --border: #d5e3e3;
  --danger: #b3261e; --success: #1e7d32;
  --radius: 12px; --shadow: 0 1px 4px rgba(18,49,47,.08);
}
html[data-theme="dark"] {
  --bg: #0e1a19; --surface: #152726; --text: #e6f2f1; --muted: #9db8b6;
  --accent: #2fa3a3; --accent-soft: #16393b; --border: #294544;
  --shadow: 0 1px 4px rgba(0,0,0,.4);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: "Segoe UI", Tahoma, "Noto Kufi Arabic", sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; }
.app-header { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.header-inner { max-width: 1000px; margin: 0 auto; padding: 12px 16px; display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.app-title { font-size: 1.4rem; color: var(--accent); }
.app-title .en { font-size: .9rem; margin-inline-start: 8px; color: var(--muted); }
.header-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.container { max-width: 1000px; margin: 20px auto; padding: 0 16px; display: grid; gap: 20px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.card h2 { font-size: 1.1rem; margin-bottom: 14px; color: var(--accent); }
.notice { max-width: 1000px; margin: 16px auto 0; padding: 10px 16px; background: var(--accent-soft); border-radius: 10px; font-size: .9rem; }
.btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px; padding: 8px 14px; cursor: pointer; font-size: .95rem; font-family: inherit; min-height: 44px; }
.btn:hover { background: var(--accent-soft); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.1); background: var(--accent); }
.btn-ghost { border: none; padding: 8px 10px; font-size: .85rem; font-weight: 600; color: var(--muted); }
.btn-ghost:hover { color: var(--accent); background: none; }
.btn-danger { color: var(--danger); border: none; }
.btn-edit { color: var(--accent); border: none; }
.calc-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; align-items: end; margin-bottom: 10px; }
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 14px; }
.account-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; align-items: end; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
input, select { padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); font-size: 1rem; font-family: inherit; min-height: 44px; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: .95rem; }
.table th, .table td { padding: 10px 8px; text-align: start; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th { color: var(--muted); font-weight: 600; font-size: .85rem; cursor: pointer; user-select: none; }
.table th[data-sort]:hover { color: var(--accent); }
.table th.sorted-asc::after { content: " ▲"; }
.table th.sorted-desc::after { content: " ▼"; }
.muted { color: var(--muted); font-size: .85rem; margin-top: 8px; }
.positive { color: var(--success); }
.negative { color: var(--danger); }
.empty-state { text-align: center; color: var(--muted); padding: 18px 8px; font-style: italic; }
.app-footer { text-align: center; padding: 20px; }
.result-list { display: grid; gap: 10px; margin-top: 12px; }
.result-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); }
.result-row.best { border-color: var(--accent); background: var(--accent-soft); }
.result-row .who { font-weight: 600; }
.result-row .net { font-weight: 700; }
.result-row .badge { font-size: .75rem; background: var(--accent); color: #fff; padding: 2px 8px; border-radius: 20px; margin-inline-start: 8px; }
.result-row.disqualified { opacity: .55; }
.skip-link { position: absolute; inset-inline-start: 8px; top: -48px; z-index: 100; background: var(--accent); color: #fff; padding: 10px 16px; border-radius: 8px; transition: top .15s ease; text-decoration: none; }
.skip-link:focus { top: 8px; }
.toast { position: fixed; bottom: 18px; inset-inline-start: 50%; transform: translate(50%, 80px); background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 10px; display: flex; gap: 14px; align-items: center; box-shadow: 0 4px 16px rgba(0,0,0,.25); opacity: 0; transition: transform .2s ease, opacity .2s ease; pointer-events: none; z-index: 200; max-width: min(92vw, 480px); }
html[dir="ltr"] .toast { transform: translate(-50%, 80px); }
.toast.show { transform: translate(50%, 0); opacity: 1; pointer-events: auto; }
html[dir="ltr"] .toast.show { transform: translate(-50%, 0); }
.toast-action { background: none; border: none; color: var(--accent); font-weight: 700; cursor: pointer; font-size: .95rem; min-height: 44px; padding: 0 6px; }
.print-header { display: none; }
@media (max-width: 600px) {
  .card { padding: 14px; }
  .table { font-size: .85rem; }
}
@media (max-width: 420px) {
  .account-form, .calc-form, .filters { grid-template-columns: 1fr; }
}
@media print {
  @page { margin: 14mm; }
  .app-header, #form-calc, .filters, #card-add, .btn-danger, .btn-edit, .skip-link, .notice { display: none !important; }
  body { background: #fff; color: #000; }
  .card { border: 1px solid #999; box-shadow: none; break-inside: avoid; }
  .print-header { display: block; text-align: center; font-weight: 700; font-size: 1.2rem; margin: 0 0 12px; }
}

/* pass 6: notes wrap; pass 12: breakdown; pass 17: editing highlight */
.table td:nth-child(7) { white-space: normal; min-width: 140px; }
.netwrap { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
html[dir="rtl"] .netwrap { align-items: flex-start; }
.breakdown { font-size: .75rem; color: var(--muted); }
#card-add.editing { border-color: var(--accent); }

/* pass 7 fix: grid children must be allowed to shrink so .table-wrap scrolls internally */
.container > * { min-width: 0; }
