:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e1e4e8;
  --text: #1c1f24;
  --muted: #646b75;
  --accent: #2f6fde;
  --accent-soft: #e6eefc;
  --accent-text: #ffffff;
  --ok: #1d8a4e;
  --ok-soft: #e3f4ea;
  --warn: #b7791f;
  --warn-soft: #fbf0dc;
  --bad: #c9372c;
  --bad-soft: #fbe6e4;
  --wa: #1fa855;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f24;
    --surface-2: #24282e;
    --border: #33383f;
    --text: #e8eaed;
    --muted: #9aa1ab;
    --accent: #6c9cf0;
    --accent-soft: #23324d;
    --accent-text: #0d1320;
    --ok: #4cc37f;
    --ok-soft: #1c3527;
    --warn: #e3ac55;
    --warn-soft: #3a2f1b;
    --bad: #f07067;
    --bad-soft: #3d2120;
    --wa: #3fcf78;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
h2 { font-size: 20px; margin: 0; }
h3 { font-size: 16px; margin: 0; }
h4 { margin: 0 0 8px; font-size: 14px; }
code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; font-size: 12px; }
small, .hint { color: var(--muted); }
.hint { margin: 0 0 12px; }
a { color: var(--accent); }

button, .btn {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
button:hover, .btn:hover { background: var(--surface-2); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover { filter: brightness(1.07); }
button.danger { color: var(--bad); }
button.small, .btn.small { padding: 4px 9px; font-size: 13px; }
button.icon { padding: 5px 11px; font-size: 18px; line-height: 1; }
.btn.wa { background: var(--wa); border-color: var(--wa); color: #fff; font-weight: 600; }
button:disabled { opacity: .5; cursor: default; }

input, select, textarea {
  font: inherit;
  width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=checkbox] { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
textarea { resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
label.mid { align-self: end; padding-bottom: 9px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.logo { color: var(--accent); font-size: 20px; }
nav { display: flex; gap: 4px; overflow-x: auto; flex: 1; }
.logout { margin-left: auto; }
@media (max-width: 640px) { .logout { position: absolute; top: 8px; right: 16px; } }
nav button { border: none; background: transparent; color: var(--muted); font-weight: 500; }
nav button.active { background: var(--accent-soft); color: var(--accent); }
.badge { background: var(--bad); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 6px; min-width: 18px; text-align: center; }

main { padding: 20px; max-width: 1400px; margin: 0 auto; }
.toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar h2 { flex: 1; min-width: 160px; }
.toolbar input[type=search] { width: 220px; }
.group { display: flex; gap: 4px; }
.seg button.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }

/* Calendar */
.cal-layout { display: grid; grid-template-columns: 1fr 320px; gap: 16px; align-items: start; }
.cal { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.cal-head { display: grid; grid-template-columns: repeat(7, 1fr); background: var(--surface-2); font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.cal-head div { padding: 8px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); grid-auto-rows: minmax(104px, auto); }
.day {
  border-top: 1px solid var(--border); border-left: 1px solid var(--border);
  padding: 4px; cursor: pointer; min-width: 0; display: flex; flex-direction: column; gap: 2px;
}
.day:nth-child(7n+1) { border-left: none; }
.day:hover { background: var(--surface-2); }
.day.other { background: var(--bg); }
.day.other .num { color: var(--muted); opacity: .6; }
.day.selected { box-shadow: inset 0 0 0 2px var(--accent); }
.num { font-size: 12px; font-weight: 600; padding: 2px 4px; align-self: flex-start; border-radius: 999px; }
.day.today .num { background: var(--accent); color: var(--accent-text); }
.chip {
  font-size: 12px; padding: 2px 6px; border-radius: 5px; background: var(--accent-soft); color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-left: 3px solid var(--accent);
}
.chip b { font-weight: 600; }
.chip.paid { border-left-color: var(--ok); }
.chip.overdue { border-left-color: var(--bad); background: var(--bad-soft); }
.chip.cancelled { opacity: .55; text-decoration: line-through; border-left-color: var(--muted); background: var(--surface-2); }
.more { font-size: 11px; color: var(--muted); padding-left: 4px; }

.day-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); position: sticky; top: 72px; }
.day-panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; gap: 8px; }
.appt-item { display: block; width: 100%; text-align: left; padding: 10px; border-radius: 8px; border: 1px solid var(--border); margin-bottom: 8px; background: var(--surface); flex-direction: column; align-items: stretch; white-space: normal; }
.appt-item .when { font-weight: 600; }
.appt-item .who { color: var(--muted); }
.tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; }
.empty { color: var(--muted); padding: 16px 0; text-align: center; }

.pill { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.info { background: var(--accent-soft); color: var(--accent); }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: none; }
td.num-col, th.num-col { text-align: right; font-variant-numeric: tabular-nums; }
td .row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.totals { display: flex; gap: 16px; color: var(--muted); }
.totals b { color: var(--text); font-variant-numeric: tabular-nums; }

/* Outbox */
.msg { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; box-shadow: var(--shadow); }
.msg-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.msg-head .to { font-weight: 600; }
.msg-head .time { color: var(--muted); font-size: 12px; margin-left: auto; }
.msg-subject { margin-top: 6px; font-weight: 500; }
.msg details { margin-top: 4px; }
.msg summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.msg pre { white-space: pre-wrap; font: inherit; background: var(--surface-2); padding: 10px; border-radius: 8px; margin: 6px 0 0; }
.msg-error { font-size: 13px; margin-top: 4px; color: var(--muted); }
.msg-error.bad { color: var(--bad); }
.msg-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

/* Settings */
.settings { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; }
.settings fieldset { padding: 16px; display: flex; flex-direction: column; gap: 12px; margin: 0; min-width: 0; }
.settings fieldset.wide { grid-column: 1 / -1; }
.settings legend { font-weight: 700; font-size: 15px; padding: 0 6px; display: flex; align-items: center; gap: 8px; }
.tpl-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.tpl-grid > div { display: flex; flex-direction: column; gap: 10px; }
.test-row { display: flex; gap: 8px; }

/* Dialogs */
dialog { border: none; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 24px)); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(10,12,16,.45); }
dialog header { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
dialog footer { display: flex; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
.close { border: none; background: transparent; }
.spacer { flex: 1; }
.form-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - 180px); overflow-y: auto; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.inline { display: flex; gap: 6px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; }
.meta:empty, .actions-row:empty { display: none; }
.actions-row { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 10px; border-top: 1px dashed var(--border); }

/* Toasts */
#toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: calc(100vw - 32px); }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 380px; animation: pop .2s ease-out; }
.toast.bad { background: var(--bad); color: #fff; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

/* Mobile */
@media (max-width: 900px) {
  .cal-layout { grid-template-columns: 1fr; }
  .day-panel { position: static; }
}
@media (max-width: 640px) {
  main { padding: 12px 16px; }
  .topbar { flex-direction: column; align-items: stretch; gap: 6px; padding: 8px 16px; }
  .cal-grid { grid-auto-rows: 56px; }
  .chip { font-size: 0; padding: 0; height: 6px; border-left: none; background: var(--accent); }
  .chip.paid { background: var(--ok); }
  .chip.overdue { background: var(--bad); }
  .chip.cancelled { background: var(--muted); }
  .more { font-size: 10px; }
  .cal-head div { padding: 6px 2px; text-align: center; }
  .row3 { grid-template-columns: 1fr 1fr; }
  .settings { grid-template-columns: 1fr; }
  .toolbar input[type=search] { width: 100%; }
  .hide-sm { display: none; }
  th, td { padding: 8px; }
}
