:root {
  --bg: #f6f6f2;
  --surface: #ffffff;
  --surface-2: #f0f1ea;
  --text: #1b1d18;
  --muted: #6c7065;
  --border: #e2e3db;
  --border-strong: #d3d5c9;
  --accent: #40592a;
  --accent-press: #33481f;
  --accent-soft: #e9efe0;
  --on-accent: #ffffff;
  --danger: #9a3320;
  --shadow: 0 1px 2px rgba(27,29,24,.06), 0 1px 3px rgba(27,29,24,.05);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14150f; --surface: #1d1f17; --surface-2: #24261c;
    --text: #ecefe3; --muted: #9a9d90; --border: #2e3125; --border-strong: #3a3d2f;
    --accent: #93b46d; --accent-press: #a6c584; --accent-soft: #232f19;
    --on-accent: #16180f; --danger: #d98168;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 1px 3px rgba(0,0,0,.25);
  }
}
:root[data-theme="dark"] {
  --bg: #14150f; --surface: #1d1f17; --surface-2: #24261c;
  --text: #ecefe3; --muted: #9a9d90; --border: #2e3125; --border-strong: #3a3d2f;
  --accent: #93b46d; --accent-press: #a6c584; --accent-soft: #232f19;
  --on-accent: #16180f; --danger: #d98168;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 1px 3px rgba(0,0,0,.25);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px; line-height: 1.45;
}
a { color: var(--accent); text-decoration: none; }
.tabnum { font-variant-numeric: tabular-nums; }

.wrap { max-width: 560px; margin: 0 auto; padding: 0 16px 120px; }

/* Top nav */
.nav {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.nav-inner { max-width: 560px; margin: 0 auto; padding: 12px 16px; display: flex; align-items: center; gap: 12px; }
.nav-brand { font-weight: 600; font-size: 16px; letter-spacing: -.01em; }
.nav-links { margin-left: auto; display: flex; gap: 4px; align-items: center; }
.nav-links a { color: var(--muted); font-size: 14px; padding: 6px 9px; border-radius: 8px; }
.nav-links a.active { color: var(--text); background: var(--surface-2); }
.nav-links a:active { background: var(--surface-2); }

/* Telefonis: firma nimi ühel real, menüü teisel real all. */
@media (max-width: 560px) {
  .nav-inner { flex-direction: column; align-items: stretch; gap: 8px; }
  .nav-brand { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .nav-links { margin-left: 0; width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .nav-links a { white-space: nowrap; }
}

.page-head { display: flex; align-items: center; gap: 12px; padding: 18px 0 12px; }
.page-head h1 { font-size: 20px; margin: 0; letter-spacing: -.01em; }
.page-head .spacer { margin-left: auto; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
  margin-bottom: 14px;
}
.card-head { padding: 13px 15px; font-size: 13px; font-weight: 600; color: var(--muted); border-bottom: 1px solid var(--border); }
.card-body { padding: 6px 15px 14px; display: flex; flex-direction: column; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  font: inherit; font-size: 15px; font-weight: 500; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text);
  border-radius: 11px; padding: 11px 16px; -webkit-tap-highlight-color: transparent;
}
.btn:active { background: var(--border); }
.btn-primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); font-weight: 600; }
.btn-primary:active { background: var(--accent-press); border-color: var(--accent-press); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn-danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn-sm { padding: 7px 11px; font-size: 14px; border-radius: 9px; }
.btn-block { width: 100%; }

/* Forms */
.field { display: flex; flex-direction: column; gap: 5px; padding: 8px 0; }
.field label { font-size: 13px; color: var(--muted); display: flex; align-items: baseline; gap: 6px; }
.field label .opt { color: color-mix(in srgb, var(--muted) 65%, transparent); font-size: 12px; }
.field input, .field select, .field textarea {
  font: inherit; font-size: 16px; color: var(--text); background: transparent;
  border: none; border-bottom: 1px solid var(--border-strong); padding: 8px 2px; outline: none;
}
.field textarea { border: 1px solid var(--border-strong); border-radius: 10px; padding: 10px; min-height: 64px; resize: vertical; }
.field select { background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 10px; padding: 11px 12px; width: 100%; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }
.field input:focus { border-bottom-color: var(--accent); }
.field .err { color: var(--danger); font-size: 12px; display: none; }
.field.error .err { display: block; }
.field.error input { border-bottom-color: var(--danger); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }

.flash { background: var(--accent-soft); color: var(--accent); border-radius: 10px; padding: 11px 14px; font-size: 14px; margin: 12px 0; }
.muted { color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 40px 16px; }

/* List rows (archive, clients, products) */
.list { display: flex; flex-direction: column; }
.list-item {
  display: flex; align-items: center; gap: 12px; padding: 14px 15px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); margin-bottom: 10px;
}
.list-item .main { min-width: 0; flex: 1; }
.list-item .t { font-weight: 600; font-size: 16px; }
.list-item .s { color: var(--muted); font-size: 13px; margin-top: 1px; }
.list-item .amt { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.list-item .chev { color: var(--muted); }

.pill { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; }
.pill.draft { background: var(--surface-2); color: var(--muted); }
.pill.sent  { background: var(--accent-soft); color: var(--accent); }
.pill.paid  { background: #dfeccd; color: #3a5320; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .pill.paid { background: #2b3b1c; color: #a6c584; } }
:root[data-theme="dark"] .pill.paid { background: #2b3b1c; color: #a6c584; }

/* Invoice line editor (from prototype) */
.rows { display: flex; flex-direction: column; }
.line { display: flex; flex-direction: column; gap: 11px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.line:first-child { padding-top: 4px; }
.line-top { display: flex; align-items: center; gap: 10px; }
.i-name { flex: 1; min-width: 0; font: inherit; font-size: 16px; font-weight: 600; color: var(--text); background: transparent; border: none; border-bottom: 1px solid var(--border-strong); outline: none; padding: 5px 0; }
.i-name::placeholder { color: color-mix(in srgb, var(--muted) 70%, transparent); font-weight: 400; }
.i-name:focus { border-bottom-color: var(--accent); }

/* Tootevaliku väli + kohandatud dropdown */
.name-field { position: relative; flex: 1; min-width: 0; }
.name-field .i-name { width: 100%; padding-right: 32px; }
.name-caret { position: absolute; right: 0; top: 50%; transform: translateY(-50%); width: 30px; height: 30px; padding: 0; border: none; background: transparent; color: var(--muted); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; -webkit-tap-highlight-color: transparent; }
.name-caret:active { color: var(--accent); }
.name-dd { position: absolute; left: 0; right: 0; top: 100%; margin-top: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.14); z-index: 30; max-height: 240px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.dd-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; cursor: pointer; font-size: 15px; }
.dd-item + .dd-item { border-top: 1px solid var(--border); }
.dd-item:hover, .dd-item:active { background: var(--accent-soft); }
.dd-name { font-weight: 500; color: var(--text); }
.dd-price { color: var(--muted); font-variant-numeric: tabular-nums; }
.line-remove { flex: none; width: 30px; height: 30px; border: none; background: transparent; color: var(--muted); font-size: 22px; line-height: 1; cursor: pointer; border-radius: 8px; }
.line-remove:active { background: var(--surface-2); color: var(--danger); }
.line-bottom { display: flex; align-items: center; gap: 12px; }
.qty { display: inline-flex; align-items: center; border: 1px solid var(--border-strong); border-radius: 9px; overflow: hidden; }
.qty button { width: 34px; height: 34px; border: none; background: var(--surface-2); color: var(--text); font-size: 19px; line-height: 1; cursor: pointer; }
.qty button:active { background: var(--accent-soft); }
.qty input { width: 40px; height: 34px; text-align: center; border: none; background: transparent; color: var(--text); font: inherit; font-size: 16px; font-variant-numeric: tabular-nums; outline: none; }
.qty input::-webkit-outer-spin-button, .qty input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.price { display: inline-flex; align-items: center; gap: 2px; border-bottom: 1px solid var(--border-strong); }
.price input { width: 64px; text-align: right; border: none; background: transparent; color: var(--text); font: inherit; font-size: 16px; font-variant-numeric: tabular-nums; outline: none; padding: 5px 0; }
.price span { color: var(--muted); }
.line-sum { margin-left: auto; color: var(--text); font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.add-line { align-self: flex-start; margin-top: 12px; font: inherit; font-size: 15px; font-weight: 500; color: var(--accent); background: transparent; border: 1px dashed var(--border-strong); border-radius: 10px; padding: 9px 14px; cursor: pointer; }
.add-line:active { background: var(--accent-soft); }

.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 15px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); cursor: pointer; margin-bottom: 14px; }
.toggle-main .t { font-weight: 600; font-size: 16px; }
.toggle-main .s { font-size: 13px; color: var(--muted); }
.toggle-right { display: flex; align-items: center; gap: 12px; }
.toggle-amount { display: inline-flex; align-items: center; gap: 2px; color: var(--muted); font-variant-numeric: tabular-nums; opacity: .45; transition: opacity .12s ease; }
.toggle-amount input { width: 52px; text-align: right; font: inherit; font-size: 16px; border: none; background: transparent; color: var(--text); border-bottom: 1px solid transparent; outline: none; font-variant-numeric: tabular-nums; }
.toggle.on .toggle-amount { opacity: 1; }
.toggle.on .toggle-amount input { border-bottom-color: var(--border-strong); }
.switch { width: 50px; height: 30px; border-radius: 999px; background: var(--border-strong); position: relative; flex: none; transition: background .15s ease; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.25); transition: transform .15s ease; }
.toggle.on .switch { background: var(--accent); }
.toggle.on .switch::after { transform: translateX(20px); }

/* Editable invoice number */
.docno { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.docno-val { color: var(--text); font-weight: 600; }
.docno input { width: 100px; font: inherit; font-size: 14px; font-weight: 600; color: var(--text); background: transparent; border: none; border-bottom: 1px solid var(--accent); outline: none; font-variant-numeric: tabular-nums; padding: 2px 0; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; padding: 0; border: none; background: transparent; color: var(--muted); cursor: pointer; border-radius: 7px; }
.icon-btn:active { background: var(--surface-2); color: var(--accent); }

/* Sticky bottom action bar */
.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; max-width: 560px; margin: 0 auto;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--border); display: flex; align-items: center; gap: 14px;
}
.bottombar .total-block { display: flex; flex-direction: column; line-height: 1.2; }
.bottombar .total-block .l { font-size: 12px; color: var(--muted); }
.bottombar .total-block .v { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.bottombar .btn-primary { margin-left: auto; }

/* Invoice document (green template) */
.doc { background: #fff; color: #1b1d18; border: 1px solid #e2e3db; border-radius: 8px; overflow: hidden; box-shadow: var(--shadow); font-size: 13px; }
.doc-inner { padding: 20px 18px; }
.doc-meta { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.doc-meta .left { flex: 0 0 auto; width: 230px; max-width: 62%; }
.doc-meta .mrow { display: flex; justify-content: space-between; gap: 14px; padding: 3px 0; }
.doc-meta .k { color: #6c7065; }
.doc-meta .v { font-variant-numeric: tabular-nums; }
.doc-meta .strong { font-weight: 700; }
.doc-no-big { text-align: right; }
.doc-no-big .lbl { color: #6c7065; font-size: 12px; }
.doc-no-big .num { font-weight: 700; font-size: 13px; font-variant-numeric: tabular-nums; }
.band { background: #40592a; color: #fff; font-weight: 600; font-size: 12px; padding: 5px 10px; display: inline-block; border-radius: 3px; }
.parties { display: flex; justify-content: space-between; gap: 16px; margin: 12px 0 18px; }
.party.right { text-align: right; }
.party .nm { font-weight: 700; margin-bottom: 3px; }
.party .ln { color: #33352d; line-height: 1.5; }
table.items { width: 100%; border-collapse: collapse; margin-top: 6px; }
table.items thead th { background: #40592a; color: #fff; font-weight: 600; font-size: 12px; text-align: right; padding: 7px 8px; }
table.items thead th:first-child { text-align: left; }
table.items td { padding: 8px; border-bottom: 1px solid #ebece5; text-align: right; font-variant-numeric: tabular-nums; }
table.items td:first-child { text-align: left; }
.doc-totals { margin-top: 14px; margin-left: auto; width: 62%; }
.doc-totals .trow { display: flex; justify-content: space-between; padding: 4px 8px; font-variant-numeric: tabular-nums; }
.doc-totals .trow.grand { font-weight: 700; border-top: 1px solid #d3d5c9; margin-top: 4px; padding-top: 8px; }
.doc-foot { margin-top: 22px; padding-top: 14px; border-top: 1px solid #ebece5; color: #4a4c43; font-size: 12px; line-height: 1.6; }
.doc-foot .thanks { margin-bottom: 12px; color: #1b1d18; }
.doc-foot .cols { display: flex; justify-content: space-between; gap: 14px; }

/* Login */
.login-wrap { max-width: 380px; margin: 0 auto; padding: 60px 20px; }
.login-wrap h1 { font-size: 22px; text-align: center; margin: 0 0 6px; }
.login-wrap .sub { text-align: center; color: var(--muted); font-size: 14px; margin-bottom: 24px; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
