:root {
  --brand: #0f766e;
  --brand-dark: #115e59;
  --brand-soft: #ccfbf1;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --success: #15803d;
  --success-soft: #dcfce7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --info: #1d4ed8;
  --info-soft: #dbeafe;
  --whatsapp: #25d366;
  --whatsapp-dark: #128c4b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 8px rgba(15, 23, 42, .04);
  --topbar-h: 56px;
  --bottom-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}
body.has-nav { padding-bottom: calc(var(--bottom-h) + var(--safe-b) + 16px); }
body.modal-open, body.drawer-open { overflow: hidden; }
a { color: var(--brand); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h3 { font-size: 1.15rem; margin-bottom: 8px; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

.container { max-width: 720px; margin: 0 auto; padding: 16px; }

/* ---------- Top bar & drawer ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  height: var(--topbar-h); padding: 0 8px;
  padding-top: env(safe-area-inset-top, 0px);
  background: var(--brand); color: #fff;
}
.topbar-title { flex: 1; font-size: 1.1rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-mini { color: #fff; opacity: .85; font-weight: 700; padding: 0 8px; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; border: 0; border-radius: 10px;
  background: transparent; color: inherit; cursor: pointer;
}
.icon-btn:active { background: rgba(0, 0, 0, .08); }
.icon-btn-danger { color: var(--muted); width: 40px; height: 40px; }
.icon-btn-danger svg { width: 20px; height: 20px; }

.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 41;
  width: min(300px, 84vw);
  display: flex; flex-direction: column;
  background: var(--card);
  transform: translateX(-100%); transition: transform .22s ease;
  overflow-y: auto;
  padding-bottom: var(--safe-b);
}
.drawer-backdrop {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(15, 23, 42, .45);
  opacity: 0; pointer-events: none; transition: opacity .22s ease;
}
.drawer-open .drawer { transform: none; }
.drawer-open .drawer-backdrop { opacity: 1; pointer-events: auto; }
.drawer-head { padding: 20px 20px 12px; padding-top: calc(20px + env(safe-area-inset-top, 0px)); background: var(--brand); color: #fff; }
.drawer-head .muted { color: rgba(255, 255, 255, .8); }
.brand { font-size: 1.4rem; font-weight: 800; letter-spacing: .5px; }
.drawer-group {
  padding: 16px 20px 6px; font-size: .75rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .08em; color: var(--muted);
}
.drawer-link { display: block; padding: 12px 20px; color: var(--text); font-weight: 500; }
.drawer-link.active { background: var(--brand-soft); color: var(--brand-dark); border-right: 3px solid var(--brand); }
.drawer-link:active { background: var(--bg); }
.drawer-foot { margin-top: auto; padding: 16px; }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--bottom-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--card); border-top: 1px solid var(--line);
}
.bottom-nav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: .72rem; font-weight: 600;
}
.bottom-nav a.active { color: var(--brand); }

/* ---------- Cards, lists ---------- */
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 16px;
}
.card-title { font-size: 1rem; font-weight: 700; margin-bottom: 8px; }
.stack > * + * { margin-top: 12px; }
.stack > .card { margin-bottom: 0; }
.mt { margin-top: 12px; }
.center { text-align: center; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.min-0 { min-width: 0; flex: 1; color: inherit; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warn { color: var(--warn); }
.text-whatsapp { color: var(--whatsapp-dark); }

.list-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 0; border-top: 1px solid var(--line); color: inherit;
}
.card-title + .list-row, .card > .list-row:first-child { border-top: 0; }
a.list-row:active { background: var(--bg); }
.row-title { font-weight: 600; }
.row-sub { font-size: .82rem; color: var(--muted); margin-top: 2px; }
.row-amount { font-weight: 700; white-space: nowrap; text-align: right; }
.row-end { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.row-actions { display: flex; gap: 14px; align-items: center; margin-top: 2px; }
.row-actions a { font-weight: 600; }
.history-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.history-opening { background: linear-gradient(90deg, var(--warn-soft), transparent 60%); margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.list-summary { display: flex; justify-content: space-between; font-size: .9rem; color: var(--muted); margin: 4px 2px 10px; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: .72rem; font-weight: 700; vertical-align: middle;
}
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-info { background: var(--info-soft); color: var(--info); }

/* ---------- Dashboard ---------- */
.greeting { margin: 0 0 12px; font-size: 1.05rem; }
.quick-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
.stat {
  display: block; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px; color: inherit;
}
.stat-wide { grid-column: 1 / -1; }
.stat-accent { background: linear-gradient(135deg, var(--brand), var(--brand-dark)); color: #fff; }
.stat-accent .stat-label, .stat-accent .stat-sub { color: rgba(255, 255, 255, .85); }
.stat-label { font-size: .78rem; font-weight: 600; color: var(--muted); }
.stat-value { font-size: 1.25rem; font-weight: 800; margin-top: 4px; word-break: break-word; }
.stat-accent .stat-value { font-size: 1.75rem; }
.stat-sub { font-size: .78rem; color: var(--muted); margin-top: 2px; }

/* ---------- Forms ---------- */
.field { display: block; }
.field > span, .field > label { display: block; font-size: .85rem; font-weight: 600; color: #334155; margin-bottom: 6px; }
input, select, textarea {
  width: 100%; font: inherit; font-size: 16px; /* 16px stops iOS zoom */
  padding: 12px; border: 1px solid #cbd5e1; border-radius: 10px;
  background: #fff; color: var(--text);
  -webkit-appearance: none; appearance: none;
}
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; background-size: 18px; padding-right: 36px;
}
input[type=date] { min-height: 48px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.input-lg { font-size: 1.4rem; font-weight: 700; }
.hint { display: block; font-size: .82rem; color: var(--muted); margin-top: 6px; min-height: 1em; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.segmented { display: flex; flex-wrap: wrap; gap: 8px; }
.segmented label { flex: 1 1 auto; }
.segmented input { position: absolute; opacity: 0; width: 0; height: 0; }
.segmented span {
  display: block; text-align: center; padding: 10px 12px; border: 1px solid #cbd5e1;
  border-radius: 10px; font-size: .9rem; font-weight: 600; cursor: pointer; background: #fff;
}
.segmented input:checked + span { background: var(--brand); border-color: var(--brand); color: #fff; }

/* Autocomplete */
.ac { position: relative; }
.ac-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 30;
  list-style: none; margin: 0; padding: 4px; max-height: 260px; overflow-y: auto;
  background: #fff; border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, .15);
}
.ac-list li {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 12px; border-radius: 8px; cursor: pointer;
}
.ac-list li small { color: var(--muted); white-space: nowrap; }
.ac-list li.active, .ac-list li:hover { background: var(--brand-soft); }

/* Order items */
.item-row { padding: 12px 0; border-bottom: 1px dashed var(--line); }
.item-row:first-child { padding-top: 0; }
.item-head { display: flex; gap: 6px; align-items: center; }
.item-product { flex: 1; }
.item-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin-top: 8px; }
.item-grid .field > span { font-size: .75rem; margin-bottom: 4px; }
.item-amount { padding: 12px 0; font-weight: 700; text-align: right; white-space: nowrap; font-size: .95rem; }
#add-item { margin-top: 12px; }

.sticky-actions {
  position: sticky; bottom: calc(var(--bottom-h) + var(--safe-b) + 8px); z-index: 10;
  background: var(--card); border-radius: var(--radius); padding: 12px;
  box-shadow: 0 -2px 20px rgba(15, 23, 42, .12);
}
.total-line { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }
.total-line strong { font-size: 1.4rem; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border: 1px solid transparent; border-radius: 10px;
  font: inherit; font-weight: 700; font-size: .95rem; text-align: center;
  cursor: pointer; user-select: none; transition: filter .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; pointer-events: none; }
.btn-lg { min-height: 52px; font-size: 1rem; }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: .85rem; }
.btn-block { display: flex; width: 100%; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-whatsapp { background: var(--whatsapp); color: #fff; }
.btn-whatsapp:active { background: var(--whatsapp-dark); }
.btn-ghost { background: #fff; color: var(--text); border-color: #cbd5e1; }
.btn-danger-ghost { background: transparent; color: var(--danger); border-color: #fecaca; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.link, .link-danger { background: none; border: 0; padding: 0; font: inherit; cursor: pointer; text-decoration: underline; }
.link { color: var(--brand); font-weight: 700; }
.link-danger { color: var(--danger); }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--bottom-h) + var(--safe-b) + 18px); z-index: 15;
  width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center;
  background: var(--brand); color: #fff; font-size: 2rem; line-height: 1;
  box-shadow: 0 6px 18px rgba(15, 118, 110, .45);
}

/* ---------- Alerts ---------- */
.alert { padding: 12px 14px; border-radius: 10px; margin-bottom: 12px; font-weight: 500; transition: opacity .4s; }
.alert-success { background: var(--success-soft); color: var(--success); }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.fade-out { opacity: 0; }

/* ---------- Reports ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 10px; align-items: center; scrollbar-width: none; }
.chip {
  flex: none; padding: 8px 14px; border-radius: 999px; background: var(--card);
  border: 1px solid var(--line); color: var(--text); font-weight: 600; font-size: .88rem;
}
.chip-sm { padding: 5px 10px; font-size: .8rem; }
.chip.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.filter-card summary { font-weight: 700; cursor: pointer; }
.filter-card[open] summary { margin-bottom: 12px; }
.search-bar { margin-bottom: 10px; }
.search-bar input { border-radius: 999px; padding-left: 18px; }

.table { width: 100%; border-collapse: collapse; font-size: .9rem; margin: 8px 0; }
.table th, .table td { padding: 8px 4px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.table th { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.table tfoot th { font-size: 1rem; color: var(--text); text-transform: none; border-bottom: 0; }
.table .num { text-align: right; white-space: nowrap; }

/* ---------- Detail pages ---------- */
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 12px; }
.detail-title { display: block; font-size: 1.3rem; font-weight: 800; color: var(--text); }
.balance-box { text-align: center; padding: 16px; border-radius: 12px; background: var(--bg); margin-bottom: 12px; }
.balance-box.is-debt { background: var(--danger-soft); color: var(--danger); }
.balance-box.is-credit { background: var(--success-soft); color: var(--success); }
.balance-value { font-size: 1.9rem; font-weight: 800; }
.mini-stats { text-align: center; margin-bottom: 14px; font-size: .9rem; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, .5); }
.modal-card {
  position: relative; width: 100%; max-width: 480px; background: var(--card);
  border-radius: 18px 18px 0 0; padding: 20px 16px calc(20px + var(--safe-b));
  animation: slide-up .2s ease;
}
.modal-card > * + * { margin-top: 12px; }
.modal-actions { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; }
@keyframes slide-up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Auth ---------- */
.auth-page { display: flex; min-height: 100vh; align-items: center; background: linear-gradient(160deg, var(--brand), var(--brand-dark)); }
.auth-page .container { width: 100%; }
.auth-card { background: var(--card); border-radius: 18px; padding: 28px 20px; box-shadow: 0 20px 50px rgba(0, 0, 0, .2); max-width: 400px; margin: 0 auto; }
.auth-logo { text-align: center; font-size: 2rem; font-weight: 800; color: var(--brand); }

/* ---------- Larger screens ---------- */
@media (min-width: 640px) {
  .container { padding: 24px; }
  .stat-grid { grid-template-columns: repeat(3, 1fr); }
  .stat-wide { grid-column: auto; }
  .modal { align-items: center; }
  .modal-card { border-radius: 18px; }
}

/* ---------- Cari ekstre ---------- */
.statement-period { margin: 10px 0 8px; }
.statement-summary { border-radius: 12px; background: var(--bg); padding: 4px 12px; }
.statement-summary > div { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; padding: 8px 0; font-size: .92rem; }
.statement-summary > div + div { border-top: 1px solid var(--line); }
.statement-summary span { color: var(--muted); }
.statement-summary strong { white-space: nowrap; }
.statement-summary .statement-closing { font-size: 1.05rem; }
.statement-summary .statement-closing span { color: var(--text); font-weight: 700; }
.statement-carry { background: var(--bg); margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.statement-actions .alert { margin-bottom: 0; }
