:root {
  --bg: #f4f6f4;
  --surface: #ffffff;
  --ink: #14211a;
  --ink-soft: #4d5b54;
  --line: #e3e8e4;
  --brand: #1f7a3d;
  --brand-ink: #ffffff;
  --good: #2f8a4a;
  --warn: #c79115;
  --bad: #c0392b;
  --shadow: 0 1px 2px rgba(20,33,26,.06), 0 4px 12px rgba(20,33,26,.04);
  --radius: 14px;
  --tap: 48px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  cursor: pointer;
}

a { color: var(--brand); }

input, select {
  font: inherit;
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  min-height: var(--tap);
}
input:focus, select:focus {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
}

label {
  display: block;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--ink-soft);
}
label input, label select { margin-top: 6px; }

.screen {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 16px 96px;
  min-height: 100dvh;
}

/* ---- Top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding-top: env(safe-area-inset-top);
}
.topbar h1 {
  flex: 1;
  font-size: 22px;
  margin: 0;
}
.topbar-actions {
  display: flex;
  gap: 6px;
}
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 18px;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ---- Buttons ---- */
.btn-primary, .btn-secondary, .btn-ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  min-height: var(--tap);
  padding: 0 16px;
  font-weight: 600;
  border: 1px solid transparent;
  width: 100%;
}
.btn-primary {
  background: var(--brand);
  color: var(--brand-ink);
}
.btn-primary:active { filter: brightness(.95); }
.btn-secondary {
  background: #fff;
  color: var(--brand);
  border-color: var(--brand);
}
.btn-ghost {
  background: transparent;
  color: var(--bad);
  width: auto;
}
.link {
  background: none;
  border: none;
  color: var(--brand);
  text-decoration: underline;
  padding: 0;
}

/* ---- Login ---- */
#screen-login {
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 22px;
  width: 100%;
  max-width: 380px;
}
.logo {
  text-align: center;
  font-size: 26px;
  margin: 0 0 24px;
  color: var(--brand);
}
.logo span { color: var(--ink); font-weight: 500; }
.error {
  color: var(--bad);
  font-size: 13px;
  margin: 8px 0 0;
}

/* ---- Client cards ---- */
.clients-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.client-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  border-left: 6px solid var(--good);
}
.client-card.warn  { border-left-color: var(--warn); }
.client-card.bad   { border-left-color: var(--bad);  }

.client-card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}
.client-name {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}
.client-balance {
  font-size: 24px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.client-card.good .client-balance { color: var(--good); }
.client-card.warn .client-balance { color: var(--warn); }
.client-card.bad  .client-balance { color: var(--bad);  }

.client-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  font-size: 13px;
  color: var(--ink-soft);
  margin: 6px 0 12px;
}
.badge {
  display: inline-block;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: #eef3ee;
  color: var(--ink-soft);
}
.badge.bad  { background: #fbe7e4; color: var(--bad); }
.badge.warn { background: #fbf1d6; color: var(--warn); }
.badge.good { background: #e3f1e7; color: var(--good); }

.client-actions {
  display: flex;
  gap: 8px;
}
.client-actions button {
  flex: 1;
}

/* ---- Detail screen ---- */
.detail-summary {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 16px;
  margin-bottom: 20px;
  text-align: center;
}
.balance {
  font-size: 40px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.balance.good { color: var(--good); }
.balance.warn { color: var(--warn); }
.balance.bad  { color: var(--bad);  }
.balance-sub {
  color: var(--ink-soft);
  font-size: 14px;
  margin-bottom: 14px;
  min-height: 18px;
}
.kv-row {
  display: flex;
  justify-content: space-around;
  gap: 12px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 12px 0;
  margin-bottom: 14px;
}
.kv-row .k {
  display: block;
  color: var(--ink-soft);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.kv-row .v {
  font-weight: 600;
  font-size: 18px;
}
.detail-actions {
  display: flex;
  gap: 10px;
}

.section-title {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--ink-soft);
  margin: 18px 4px 8px;
}
.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.history-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  gap: 10px;
}
.history-list li:last-child { border-bottom: none; }
.history-list li.empty {
  color: var(--ink-soft);
  font-style: italic;
  justify-content: center;
}
.history-list .row-main { display: flex; flex-direction: column; gap: 2px; }
.history-list .row-date { font-weight: 600; font-size: 14px; }
.history-list .row-sub  { font-size: 12px; color: var(--ink-soft); }
.history-list .row-amt  { font-weight: 600; font-variant-numeric: tabular-nums; }
.history-list .row-amt.cut     { color: var(--ink); }
.history-list .row-amt.payment { color: var(--good); }
.history-list .row-edit {
  background: none;
  border: none;
  padding: 6px;
  color: var(--ink-soft);
  font-size: 16px;
}

/* ---- Settings list ---- */
.settings-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.settings-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.settings-list li:last-child { border-bottom: none; }
.settings-list .s-name { font-weight: 600; }
.settings-list .s-sub  { color: var(--ink-soft); font-size: 13px; }

/* ---- Modal ---- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(20,33,26,.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--surface);
  width: 100%;
  max-width: 520px;
  border-radius: 18px 18px 0 0;
  box-shadow: var(--shadow);
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 520px) {
  .modal { align-items: center; }
  .modal-card { border-radius: 18px; }
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.modal-header h2 {
  margin: 0;
  font-size: 18px;
}
#modal-body {
  padding: 16px;
  overflow-y: auto;
}
.modal-footer {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  align-items: center;
}
.modal-footer .btn-primary { flex: 1; }

.radio-group {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}
.radio-group label {
  flex: 1;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  font-size: 14px;
  color: var(--ink);
  background: #fff;
}
.radio-group input[type="radio"] {
  width: auto;
  min-height: 0;
  margin: 0;
}
.radio-group label:has(input:checked) {
  border-color: var(--brand);
  background: #eaf4ee;
}

.empty {
  text-align: center;
  color: var(--ink-soft);
  margin-top: 40px;
}

/* ---- Diagnostic bar ---- */
.diag {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 9999;
  padding: 10px 14px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center;
  border-bottom: 1px solid rgba(0,0,0,.1);
  padding-top: max(10px, env(safe-area-inset-top));
}
.diag.err  { background: #fbe7e4; color: #7a1f15; }
.diag.warn { background: #fbf1d6; color: #6e4b00; }
.diag.info { background: #e3f1e7; color: #1a5c2f; }
.diag[hidden] { display: none; }
