:root {
  --navy: #003d5c;
  --navy-2: #0d5079;
  --navy-3: #2a6487;
  --silver: #9aa3ad;
  --silver-2: #d5dae1;
  --silver-3: #edf0f4;
  --white: #ffffff;
  --bg: #f4f6f9;
  --text: #1d2330;
  --muted: #5d6676;
  --danger: #b42318;
  --ok: #1e7846;
  --warn-bg: #fff6e0;
  --warn-border: #f0c96a;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 61, 92, .08), 0 4px 14px rgba(0, 61, 92, .07);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Samsung Sans", "Helvetica Neue", Arial, sans-serif;
  font-size: 17px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
main { max-width: 760px; margin: 0 auto; padding: 16px 16px calc(120px + var(--safe-b)); outline: none; }
h1 { font-size: 1.45rem; margin: 6px 0 14px; color: var(--navy); }
h2 { font-size: 1.1rem; margin: 0 0 12px; color: var(--navy); }
h3 { font-size: 1rem; margin: 0 0 8px; color: var(--navy); }
a { color: var(--navy-2); }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--navy); color: var(--white);
  border-bottom: 3px solid var(--silver);
}
.brand { flex: 1; display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; min-width: 0; }
.brand img { width: 38px; height: 38px; object-fit: contain; background: var(--white); border-radius: 9px; padding: 2px; }
.brand span { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand strong { font-size: 1.05rem; letter-spacing: .2px; }
.brand small { font-size: .78rem; color: var(--silver-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 46px; height: 46px; border-radius: 12px; border: 0;
  background: transparent; color: var(--white); cursor: pointer;
}
.icon-btn:active { background: rgba(255,255,255,.14); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; padding: 12px 18px; border-radius: 12px;
  font: inherit; font-weight: 650; font-size: 1rem; text-decoration: none; text-align: center;
  border: 2px solid transparent; cursor: pointer; user-select: none;
  transition: transform .06s ease, background .15s ease;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; }
.btn-primary { background: var(--navy); color: var(--white); }
.btn-primary:active { background: var(--navy-2); }
.btn-secondary { background: var(--white); color: var(--navy); border-color: var(--navy); }
.btn-silver { background: var(--silver-3); color: var(--navy); border-color: var(--silver-2); }
.btn-ghost { background: transparent; color: var(--navy); }
.btn-danger { background: var(--white); color: var(--danger); border-color: #f1b8b2; }
.btn-block { width: 100%; }
.btn-sm { min-height: 42px; padding: 8px 12px; font-size: .92rem; }
.btn svg { flex: none; }

.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn-grid .full { grid-column: 1 / -1; }

/* ---------- Dashboard ---------- */
.hero-actions { display: grid; gap: 14px; margin: 8px 0 18px; }
.hero-btn {
  display: flex; align-items: center; gap: 16px;
  min-height: 108px; padding: 20px; border-radius: 18px;
  text-decoration: none; color: var(--white); background: var(--navy);
  box-shadow: var(--shadow); border: 0; font: inherit; text-align: left; cursor: pointer;
  position: relative; overflow: hidden;
}
.hero-btn::after {
  content: ""; position: absolute; right: -40px; top: -40px; width: 160px; height: 160px; border-radius: 50%;
  background: radial-gradient(circle, rgba(255,255,255,.16), rgba(255,255,255,0) 70%);
}
.hero-btn.invoice { background: linear-gradient(135deg, var(--navy-2), var(--navy)); }
.hero-btn .hero-icon { flex: none; width: 58px; height: 58px; border-radius: 14px; background: rgba(255,255,255,.14); display: grid; place-items: center; }
.hero-btn strong { display: block; font-size: 1.35rem; }
.hero-btn small { color: var(--silver-2); font-size: .92rem; }
.hero-btn:active { transform: scale(.99); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 18px; }
.stat { background: var(--white); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); text-align: center; }
.stat b { display: block; font-size: 1.05rem; color: var(--navy); }
.stat span { font-size: .78rem; color: var(--muted); }

/* ---------- Cards / forms ---------- */
.card { background: var(--white); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }

.field { display: block; margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field > span { display: block; font-size: .9rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.field > span .req { color: var(--danger); }
input, select, textarea {
  width: 100%; font: inherit; font-size: 1.02rem; color: var(--text);
  min-height: 52px; padding: 12px 14px; border-radius: 11px;
  border: 1.5px solid var(--silver-2); background: var(--white);
  appearance: none; -webkit-appearance: none;
}
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='9'%3E%3Cpath d='M1 1l6 6 6-6' stroke='%23003d5c' stroke-width='2' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 14px center; padding-right: 38px;
}
textarea { min-height: 96px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--navy-2); box-shadow: 0 0 0 3px rgba(13, 80, 121, .2); }
.invalid input, .invalid select, .invalid textarea, input.invalid { border-color: var(--danger); background: #fff8f7; }
.err { display: block; color: var(--danger); font-size: .85rem; margin-top: 4px; font-weight: 600; }
.hint { font-size: .85rem; color: var(--muted); margin: 4px 0 0; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row2 > .field { margin-bottom: 14px; }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 52px; font-weight: 600; cursor: pointer; }
.switch input { appearance: none; -webkit-appearance: none; width: 54px; height: 32px; min-height: 0; padding: 0; border-radius: 99px; background: var(--silver-2); border: 0; position: relative; flex: none; cursor: pointer; transition: background .15s; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: var(--white); box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: left .15s; }
.switch input:checked { background: var(--navy); }
.switch input:checked::after { left: 25px; }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0; border: 1.5px solid var(--navy); border-radius: 11px; overflow: hidden; }
.seg button { min-height: 46px; border: 0; background: var(--white); color: var(--navy); font: inherit; font-size: .9rem; font-weight: 600; cursor: pointer; padding: 6px 4px; }
.seg button + button { border-left: 1.5px solid var(--navy); }
.seg button[aria-pressed="true"] { background: var(--navy); color: var(--white); }
.seg.two { grid-template-columns: 1fr 1fr; }

/* ---------- Line items ---------- */
.item { border: 1.5px solid var(--silver-2); border-radius: 13px; padding: 14px; margin-bottom: 12px; background: #fbfcfd; }
.item-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.item-head b { color: var(--navy); }
.item-tools { display: flex; gap: 4px; }
.item-tools button { width: 42px; height: 42px; border-radius: 10px; border: 1.5px solid var(--silver-2); background: var(--white); color: var(--navy); font-size: 1.1rem; cursor: pointer; display: grid; place-items: center; }
.item-tools button.del { color: var(--danger); }
.line-total { display: flex; justify-content: space-between; align-items: center; padding-top: 10px; border-top: 1px dashed var(--silver-2); font-weight: 700; color: var(--navy); }
.line-total span { color: var(--muted); font-weight: 600; font-size: .9rem; }

.totals { display: grid; gap: 6px; }
.totals div { display: flex; justify-content: space-between; }
.totals .grand { margin-top: 6px; padding: 12px 14px; border-radius: 11px; background: var(--navy); color: var(--white); font-weight: 800; font-size: 1.15rem; }

/* ---------- Sticky action bar ---------- */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  background: var(--white); border-top: 1px solid var(--silver-2);
  box-shadow: 0 -6px 18px rgba(0, 61, 92, .08);
  padding: 10px 16px calc(10px + var(--safe-b));
}
.actionbar-inner { max-width: 760px; margin: 0 auto; display: flex; align-items: center; gap: 12px; }
.actionbar .total { flex: 1; line-height: 1.15; }
.actionbar .total small { display: block; color: var(--muted); font-size: .8rem; }
.actionbar .total b { font-size: 1.3rem; color: var(--navy); }
.actionbar .btn { min-width: 140px; }

/* ---------- Lists ---------- */
.list { display: grid; gap: 10px; }
.doc-card {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: center;
  padding: 14px 16px; background: var(--white); border-radius: var(--radius); box-shadow: var(--shadow);
  text-decoration: none; color: inherit; border-left: 5px solid var(--navy);
}
.doc-card.invoice { border-left-color: var(--silver); }
.doc-card .num { font-weight: 700; color: var(--navy); font-size: .92rem; }
.doc-card .cust { font-weight: 650; font-size: 1.02rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-card .meta { font-size: .82rem; color: var(--muted); }
.doc-card .amt { font-weight: 800; color: var(--navy); text-align: right; }
.pill { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: .74rem; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; background: var(--silver-3); color: var(--muted); justify-self: end; }
.pill.sent { background: #e3ecfb; color: var(--navy-2); }
.pill.accepted, .pill.paid { background: #dff3e7; color: var(--ok); }
.pill.declined, .pill.void { background: #fde8e6; color: var(--danger); }
.pill.converted { background: #ece6fb; color: #4b2a96; }
.pill.overdue { background: #fde8e6; color: var(--danger); }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; background: var(--white); border-radius: var(--radius); border: 2px dashed var(--silver-2); }
.toolbar { display: grid; grid-template-columns: 1fr auto; gap: 10px; margin-bottom: 12px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 0; margin-bottom: 12px; background: var(--silver-3); border-radius: 12px; padding: 4px; }
.tabs a { text-align: center; padding: 11px 6px; border-radius: 9px; text-decoration: none; color: var(--navy); font-weight: 650; }
.tabs a.active { background: var(--white); box-shadow: var(--shadow); }
.section-title { display: flex; align-items: center; justify-content: space-between; margin: 18px 0 10px; }
.section-title h2 { margin: 0; }

/* ---------- Document view ---------- */
.summary { display: grid; gap: 4px; }
.summary .big { font-size: 1.9rem; font-weight: 800; color: var(--navy); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: .95rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; word-break: break-word; }
.view-items { display: grid; gap: 10px; }
.view-items .vi { display: flex; justify-content: space-between; gap: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--silver-3); }
.view-items .vi:last-child { border-bottom: 0; padding-bottom: 0; }
.view-items .vi small { display: block; color: var(--muted); }
.share-note { font-size: .85rem; color: var(--muted); margin: 10px 2px 0; }
.pdf-frame { width: 100%; height: 70vh; border: 1px solid var(--silver-2); border-radius: 12px; background: #fff; }

/* ---------- Notices ---------- */
.notice { display: flex; gap: 12px; padding: 12px 14px; border-radius: 12px; background: var(--warn-bg); border: 1px solid var(--warn-border); font-size: .9rem; margin-bottom: 16px; }
.notice b { display: block; }
.notice .ico { flex: none; font-size: 1.2rem; }
.logo-preview { display: flex; align-items: center; gap: 14px; }
.logo-preview img { width: 120px; height: 80px; object-fit: contain; border: 1px solid var(--silver-2); border-radius: 10px; background: #fff; }
.svc-row { border-bottom: 1px solid var(--silver-3); padding: 12px 0; }
.svc-row:first-of-type { padding-top: 0; }
details.svc > summary { list-style: none; cursor: pointer; display: flex; justify-content: space-between; align-items: center; min-height: 48px; font-weight: 650; color: var(--navy); }
details.svc > summary::-webkit-details-marker { display: none; }
details.svc > summary::after { content: "\203A"; font-size: 1.5rem; transform: rotate(90deg); transition: transform .15s; color: var(--silver); }
details.svc[open] > summary::after { transform: rotate(-90deg); }
details.svc > summary small { color: var(--muted); font-weight: 500; }

/* ---------- Toast & dialog ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: var(--navy); color: var(--white); padding: 12px 18px; border-radius: 12px;
  font-weight: 600; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50;
  max-width: calc(100% - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); }
dialog { border: 0; border-radius: 18px; padding: 22px; width: min(420px, calc(100% - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0, 61, 92, .45); }
dialog p { color: var(--muted); white-space: pre-line; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.dialog-actions .btn { min-width: 100px; }

@media (min-width: 640px) {
  .hero-actions { grid-template-columns: 1fr 1fr; }
  .btn-grid.wide { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 360px) {
  .row2 { grid-template-columns: 1fr; gap: 0; }
  .stats { grid-template-columns: 1fr 1fr; }
}
