:root {
  --bg: #f5f4ef;
  --surface: #ffffff;
  --surface-2: #f1efe8;
  --border: #e4e1d8;
  --text: #1d1c1a;
  --muted: #6d6a62;
  --accent: #c15f3c;
  --accent-hover: #a94f2f;
  --on-accent: #ffffff;
  --green: #23744a;
  --green-bg: #e2f1e7;
  --amber: #96600a;
  --amber-bg: #fbefd6;
  --red: #b0352b;
  --red-bg: #fbe3df;
  --grey: #6d6a62;
  --grey-bg: #ecebe5;
  --wa: #1f8f4e;
  --bubble: #d9fdd3;
  --bubble-text: #111b21;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 6px 20px rgb(0 0 0 / 0.05);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171614;
    --surface: #211f1c;
    --surface-2: #2a2824;
    --border: #36332e;
    --text: #edebe5;
    --muted: #a39f95;
    --accent: #e07b55;
    --accent-hover: #ea9070;
    --on-accent: #1b0f0a;
    --green: #72cf98;
    --green-bg: #1c3326;
    --amber: #f0b955;
    --amber-bg: #372b14;
    --red: #f2897e;
    --red-bg: #3b201c;
    --grey: #a39f95;
    --grey-bg: #2c2a26;
    --wa: #4fd08a;
    --bubble: #144d37;
    --bubble-text: #e9edef;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; } /* beats display rules on .field, .btn, etc. */

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "SF Arabic", "Geeza Pro", "Segoe UI", Tahoma, sans-serif;
}

.app { max-width: 1180px; margin: 0 auto; padding: 24px 20px 64px; }

h1 { font-size: 22px; margin: 0; line-height: 1.25; }
h2 { font-size: 16px; margin: 0; }
h3 { font-size: 17px; margin: 0; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.strong { font-weight: 600; }
code {
  font: 12.5px ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 5px;
}

/* ---------- header & tabs ---------- */
.top { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 44px; height: 44px; border-radius: 12px;
  background: var(--accent); color: var(--on-accent);
  display: grid; place-items: center;
}

.tabs {
  display: flex; gap: 4px; padding: 4px;
  background: var(--surface-2); border-radius: 12px;
  margin-bottom: 20px; width: fit-content; max-width: 100%; overflow-x: auto;
}
.tabs button {
  border: 0; background: transparent; color: var(--muted);
  font: inherit; font-weight: 500; padding: 7px 16px; border-radius: 9px;
  cursor: pointer; white-space: nowrap;
}
.tabs button:hover { color: var(--text); }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- dashboard ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat {
  position: relative; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 18px; box-shadow: var(--shadow);
}
.stat::before { content: ""; position: absolute; inset-block: 0; inset-inline-start: 0; width: 4px; background: var(--tone); }
.stat.green { --tone: var(--green); }
.stat.amber { --tone: var(--amber); }
.stat.red { --tone: var(--red); }
.stat.grey { --tone: var(--border); }
.stat-label { color: var(--muted); font-size: 13px; }
.stat-value { font-size: 26px; font-weight: 700; line-height: 1.35; font-variant-numeric: tabular-nums; }
.stat-sub { color: var(--muted); font-size: 12.5px; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.panel-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.panel-head.spaced { margin-top: 22px; }
.section-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin: 24px 0 10px; }
.note {
  background: var(--amber-bg); color: var(--amber);
  border-radius: 12px; padding: 10px 14px; font-size: 13.5px; font-weight: 500;
}
.neg { color: var(--red); }
.ok { color: var(--green); }
.warn { color: var(--amber); }
.paid-hint { font-weight: 500; margin-top: -4px; min-height: 1.4em; }
.pay-summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.pay-summary > div { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.pay-summary span { display: block; color: var(--muted); font-size: 12.5px; }
.pay-summary strong { font-size: 18px; font-variant-numeric: tabular-nums; }
tr.current td { background: color-mix(in srgb, var(--accent) 9%, transparent); }
.profit-hint { color: var(--green); font-weight: 500; margin-top: -4px; }
.wa-preview {
  white-space: pre-wrap; line-height: 1.75;
  background: var(--bubble); color: var(--bubble-text);
  border-radius: 14px; border-start-start-radius: 4px; padding: 12px 14px;
}

.items { display: flex; flex-direction: column; }
.item {
  display: grid; grid-template-columns: 1fr auto auto; gap: 16px; align-items: center;
  padding: 12px 2px; border-top: 1px solid var(--border);
}
.item:first-child { border-top: 0; }
.item-title { font-weight: 600; }
.item-meta { text-align: center; min-width: 130px; }
.item-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- controls ---------- */
.toolbar { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar input[type="search"] { flex: 1; min-width: 220px; }
.toolbar select { width: auto; }

input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 11px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent);
  border-color: var(--accent);
}
textarea { resize: vertical; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-size: 14px; font-weight: 500; color: var(--text); text-decoration: none; white-space: nowrap;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 14px; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--red); border-color: color-mix(in srgb, var(--red) 35%, var(--border)); }
.btn.danger:hover { background: var(--red-bg); }
.btn.wa { color: var(--wa); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.sm { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.btn:disabled { opacity: 0.6; cursor: default; }

.link {
  border: 0; background: none; padding: 0; cursor: pointer;
  font: inherit; color: var(--accent); text-decoration: underline; text-underline-offset: 3px;
}

.badge { display: inline-block; font-size: 12.5px; font-weight: 600; padding: 2px 10px; border-radius: 999px; white-space: nowrap; }
.badge.green { color: var(--green); background: var(--green-bg); }
.badge.amber { color: var(--amber); background: var(--amber-bg); }
.badge.red { color: var(--red); background: var(--red-bg); }
.badge.grey { color: var(--grey); background: var(--grey-bg); }

.acct { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.acct + .acct { margin-top: 3px; }
.acct + .link { margin-top: 4px; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.dot.green { background: var(--green); }
.dot.amber { background: var(--amber); }
.dot.red { background: var(--red); }
.dot.grey { background: var(--grey); }

.chip {
  display: inline-flex; align-items: center; gap: 8px; margin-bottom: 12px;
  background: var(--surface-2); border-radius: 999px; padding-block: 4px; padding-inline: 12px 5px; font-size: 13.5px;
}
.chip button {
  border: 0; background: var(--border); color: var(--text); cursor: pointer;
  width: 22px; height: 22px; border-radius: 50%; font-size: 12px;
}

/* ---------- tables ---------- */
.table-wrap {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow-x: auto; box-shadow: var(--shadow);
}
.modal .table-wrap { box-shadow: none; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: start; font-size: 12.5px; font-weight: 600; color: var(--muted);
  padding: 10px 14px; background: var(--surface-2); white-space: nowrap;
}
td { padding: 11px 14px; border-top: 1px solid var(--border); vertical-align: middle; }
tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 55%, transparent); }
td.actions { white-space: nowrap; text-align: end; }
td.actions > * + * { margin-inline-start: 4px; }
td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.clamp { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty { text-align: center; padding: 28px 12px; color: var(--muted); }
.empty p { margin-bottom: 12px; }

/* ---------- dialog & forms ---------- */
dialog {
  border: 0; padding: 0; border-radius: 16px;
  background: var(--surface); color: var(--text);
  width: min(560px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.25);
}
dialog.wide { width: min(860px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(20 18 15 / 0.45); }
.modal header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px 0; }
.modal-body { padding: 14px 20px 18px; max-height: calc(100vh - 190px); overflow-y: auto; }
.modal-body > p { margin-bottom: 12px; }
.modal footer {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 12px 20px 16px; border-top: 1px solid var(--border);
}
.form-error { color: var(--red); font-size: 13.5px; font-weight: 500; }
.spacer { flex: 1; }
.icon-btn {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  width: 32px; height: 32px; border-radius: 8px; font-size: 16px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.full { grid-column: 1 / -1; }
fieldset.grid { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset[hidden] { display: none; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 13px; font-weight: 500; color: var(--muted); }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 500; }
.field:not(.full) > .check { padding-top: 28px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.form-actions { margin-top: 16px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

#toast {
  position: fixed; bottom: 24px; inset-inline: 0; margin: 0 auto; z-index: 100;
  width: fit-content; max-width: calc(100vw - 32px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px; font-weight: 500;
  opacity: 0; transform: translateY(10px); transition: opacity 0.2s, transform 0.2s; pointer-events: none;
}
#toast.show { opacity: 1; transform: none; }

/* ---------- login ---------- */
body.locked .app { display: none; }
body:not(.locked) #auth { display: none; }
#auth {
  min-height: 100vh; min-height: 100dvh;
  display: grid; place-items: center; padding: 24px 16px;
}
.auth-card {
  width: min(380px, 100%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  padding: 28px 24px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px; text-align: center;
}
.auth-card .logo { margin: 0 auto 2px; }
.auth-card .field { text-align: start; }
.auth-card .btn { padding: 11px 14px; font-size: 15px; }

/* ---------- reminders queue ---------- */
.queue { display: flex; flex-direction: column; }
.queue-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 2px; border-top: 1px solid var(--border);
}
.queue-row:first-child { border-top: 0; }
.queue-row.done > div:first-child { opacity: 0.55; }
.queue-action { display: flex; align-items: center; gap: 6px; flex: none; }

/* ---------- phones ---------- */
@media (max-width: 760px) {
  .app {
    padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right))
      max(40px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  }
  h1 { font-size: 19px; }
  .logo { width: 38px; height: 38px; }
  .top { margin-bottom: 14px; }
  .tabs { margin-bottom: 14px; }
  .stats { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat { padding: 12px 14px; }
  .stat-value { font-size: 21px; }
  .panel { padding: 14px; }
  .item { grid-template-columns: 1fr auto; gap: 10px; }
  .item-meta { min-width: 0; }
  .item-actions { grid-column: 1 / -1; justify-content: flex-start; }
  .grid { grid-template-columns: 1fr; }
  .field:not(.full) > .check { padding-top: 0; }
  input, select, textarea { font-size: 16px; } /* stops iPhone zooming into fields */
  .toolbar input[type="search"] { min-width: 0; flex-basis: 100%; }
  .toolbar select { flex: 1; }
  .stats > .stat:last-child:nth-child(odd) { grid-column: 1 / -1; }
  .item > div, .queue-row > div:first-child { min-width: 0; overflow-wrap: anywhere; }
  .btn.sm { padding: 6px 12px; font-size: 13.5px; }

  /* tables become one card per row */
  .table-wrap { background: transparent; border: 0; box-shadow: none; overflow: visible; }
  table, tbody { display: block; }
  thead { display: none; }
  tbody tr {
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 12px 14px; margin-bottom: 10px;
  }
  tbody tr:hover { background: var(--surface); }
  tr.current { border-color: var(--accent); }
  tr.current td { background: none; }
  td { display: block; padding: 0; border: 0; min-width: 0; overflow-wrap: anywhere; }
  td::before { content: attr(data-label); display: block; color: var(--muted); font-size: 12px; margin-bottom: 1px; }
  td:first-child { grid-column: 1 / -1; font-size: 16px; }
  td:first-child::before, td.actions::before, td[data-label=""]::before { display: none; }
  td.actions {
    grid-column: 1 / -1; text-align: start; white-space: normal;
    display: flex; flex-wrap: wrap; gap: 6px; padding-top: 10px; border-top: 1px solid var(--border);
  }
  td.actions > * + * { margin: 0; }
  .clamp { max-width: none; white-space: normal; }
  .acct { white-space: normal; flex-wrap: wrap; }

  dialog, dialog.wide { width: calc(100vw - 16px); max-height: calc(100dvh - 16px); }
  .modal-body { max-height: calc(100dvh - 170px); }
  .modal .table-wrap tbody tr { background: var(--surface-2); border-color: transparent; }
  .queue-row { align-items: flex-start; }
}
