:root {
  --bg: #eef0f5;
  --panel: #ffffff;
  --ink: #1d2238;
  --text: #464c63;
  --muted: #5f657a; /* 5:1 or better on every background used (WCAG AA) */
  --line: #e2e5ec;
  --line-strong: #cfd3de;
  --soft: #f6f7fa;
  --primary: #1d2238;
  --primary-hover: #2e3552;
  --focus: #4f63d2;
  --ok: #17734a;
  --ok-bg: #e8f5ee;
  --warn: #8a5300;
  --warn-bg: #fff5e0;
  --bad: #b42318;
  --bad-bg: #fdecea;
  --radius: 10px;
  --topbar: 56px;
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter Invoice', 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

h1,
h2,
h3 {
  color: var(--ink);
  margin: 0;
  font-weight: 600;
  letter-spacing: -0.01em;
}

h1 {
  font-size: 22px;
}

h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
}

.loading {
  padding: 48px;
  color: var(--muted);
}

/* ---------- Top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  height: var(--topbar);
  display: flex;
  align-items: center;
  gap: 28px;
  padding: 0 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
  font-size: 15px;
  color: var(--ink);
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
}

.tabs {
  display: flex;
  gap: 4px;
  height: 100%;
}

.tabs a {
  display: flex;
  align-items: center;
  padding: 0 12px;
  text-decoration: none;
  color: var(--muted);
  font-weight: 500;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}

.tabs a:hover {
  color: var(--ink);
}

.tabs a[aria-current='page'] {
  color: var(--ink);
  border-bottom-color: var(--ink);
}

/* ---------- Buttons & fields ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: var(--panel);
  color: var(--ink);
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.btn:hover {
  background: var(--soft);
}

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn.primary:hover {
  background: var(--primary-hover);
}

.btn.primary:disabled {
  opacity: 0.6;
  cursor: progress;
}

.btn.small {
  height: 30px;
  padding: 0 10px;
  font-size: 13px;
}

.btn.link {
  border: 0;
  background: none;
  padding: 0 6px;
  height: auto;
  color: var(--muted);
  font-weight: 500;
}

.btn.link:hover {
  color: var(--ink);
  text-decoration: underline;
}

.btn.danger {
  color: var(--bad);
}

.btn.big {
  height: 44px;
  font-size: 15px;
  padding: 0 20px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}

.field > span,
.field-label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text);
}

.field small,
.hint {
  font-size: 12px;
  color: var(--muted);
}

input[type='text'],
input[type='email'],
input[type='date'],
input[type='number'],
input:not([type]),
select,
textarea {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
}

textarea {
  height: auto;
  min-height: 76px;
  padding: 8px 10px;
  resize: vertical;
  line-height: 1.45;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--focus);
  box-shadow: 0 0 0 3px rgb(79 99 210 / 0.15);
}

input[readonly] {
  background: var(--soft);
}

.input-affix {
  display: flex;
  align-items: stretch;
}

.input-affix > span {
  display: flex;
  align-items: center;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-right: 0;
  border-radius: 7px 0 0 7px;
  background: var(--soft);
  color: var(--muted);
  white-space: nowrap;
  font-size: 13px;
}

.input-affix > input {
  border-radius: 0 7px 7px 0;
}

.input-affix > input:not(:last-child) {
  border-radius: 0;
}

.affix-btn {
  flex: 0 0 auto;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-left: 0;
  border-radius: 0 7px 7px 0;
  background: var(--soft);
  color: var(--ink);
  font-size: 12.5px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}

.affix-btn:hover {
  background: #eceef3;
}

.hint.rate-ok {
  color: var(--ok);
}

.hint.rate-bad {
  color: var(--bad);
}

.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.grid-cur {
  display: grid;
  grid-template-columns: 84px 1fr;
  gap: 12px;
}

.grid3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.row > .grow {
  flex: 1 1 auto;
  min-width: 0;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  height: 26px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: #fff;
  font-size: 12.5px;
  color: var(--text);
  cursor: pointer;
}

.chip:hover {
  border-color: var(--ink);
  color: var(--ink);
}

.segmented {
  display: inline-flex;
  padding: 3px;
  border-radius: 8px;
  background: var(--soft);
  border: 1px solid var(--line);
}

.segmented button {
  border: 0;
  background: none;
  height: 28px;
  padding: 0 12px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
}

.segmented button[aria-pressed='true'] {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 1px 2px rgb(16 24 40 / 0.12);
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: var(--soft);
  color: var(--text);
  border: 1px solid var(--line);
  white-space: nowrap;
}

.badge.export {
  background: #eef2ff;
  border-color: #d9e0ff;
  color: #3842a0;
}

.badge.intra {
  background: #e9f6f0;
  border-color: #cfe9dc;
  color: #1c6b46;
}

.badge.inter {
  background: #fdf3e7;
  border-color: #f4dfc2;
  color: #8a5300;
}

.notice {
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.45;
}

.notice.warn {
  background: var(--warn-bg);
  color: var(--warn);
}

.notice.bad {
  background: var(--bad-bg);
  color: var(--bad);
}

.notice.ok {
  background: var(--ok-bg);
  color: var(--ok);
}

.notice ul {
  margin: 4px 0 0;
  padding-left: 18px;
}

.notice .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

/* ---------- Editor ---------- */

.editor {
  display: grid;
  grid-template-columns: minmax(420px, 480px) 1fr;
  height: calc(100vh - var(--topbar));
}

.form-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.form-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 22px 0;
}

.form-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 6px 22px 22px;
}

.sec {
  padding: 18px 0;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sec:last-child {
  border-bottom: 0;
}

.sec-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.client-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  background: var(--soft);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.client-card .addr {
  color: var(--muted);
  font-size: 13px;
  white-space: pre-line;
}

.client-card .top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.client-card strong {
  color: var(--ink);
  font-weight: 600;
}

.item {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item-top {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.item-top textarea {
  resize: none;
  min-height: 36px;
  height: 36px;
  padding-top: 7px;
  overflow: hidden;
}

.item-remove {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-radius: 7px;
  border: 1px solid transparent;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.item-remove:hover {
  border-color: var(--line-strong);
  color: var(--bad);
}

.item-calc {
  display: grid;
  grid-template-columns: 92px 1fr 1fr 1fr;
  gap: 8px;
  align-items: end;
}

.item-calc.fixed {
  grid-template-columns: 92px 1fr auto;
}

.line-total {
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.form-foot {
  border-top: 1px solid var(--line);
  padding: 14px 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--panel);
}

.sum {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-variant-numeric: tabular-nums;
}

.sum-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
}

.sum-row.total {
  font-size: 16px;
  font-weight: 650;
  color: var(--ink);
  margin-top: 2px;
}

.sum-row.sub {
  color: var(--muted);
  font-size: 12.5px;
}

.preview-pane {
  overflow: auto;
  padding: 28px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.preview-frame-wrap {
  position: relative;
  overflow: hidden;
  flex: 0 0 auto;
  box-shadow: 0 1px 3px rgb(16 24 40 / 0.1), 0 12px 32px rgb(16 24 40 / 0.1);
  background: #fff;
}

.preview-frame-wrap iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 794px;
  height: 1123px;
  border: 0;
  transform-origin: 0 0;
  background: #fff;
}

/* ---------- List pages ---------- */

.page {
  max-width: 1080px;
  margin: 0 auto;
  padding: 28px 24px 64px;
}

.page-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.page-head h1 {
  margin-right: auto;
}

.page-head input[type='search'] {
  width: 240px;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: #fff;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}

.list {
  width: 100%;
  border-collapse: collapse;
}

.list th {
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--soft);
}

.list td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}

.list tr:last-child td {
  border-bottom: 0;
}

.list .r {
  text-align: right;
}

.list .num {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.list .month td {
  background: #fafbfc;
  font-weight: 600;
  color: var(--ink);
  font-size: 13px;
  padding-top: 9px;
  padding-bottom: 9px;
}

.list .actions {
  display: flex;
  justify-content: flex-end;
  gap: 2px;
  white-space: nowrap;
}

.list a.inv-link {
  white-space: nowrap;
  color: var(--ink);
  font-weight: 600;
  text-decoration: none;
}

.list a.inv-link:hover {
  text-decoration: underline;
}

.muted {
  color: var(--muted);
}

.empty {
  padding: 40px;
  text-align: center;
  color: var(--muted);
}

/* ---------- Settings ---------- */

.settings {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.settings .card {
  padding: 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: visible;
}

.settings .card > p {
  margin: -6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.bank-rows {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bank-row {
  display: grid;
  grid-template-columns: 200px 1fr 36px;
  gap: 8px;
  align-items: start;
}

.bank-row textarea {
  resize: none;
  min-height: 36px;
  height: 36px;
  padding-top: 7px;
}

.sig-box {
  display: flex;
  align-items: center;
  gap: 14px;
}

.sig-preview {
  width: 200px;
  height: 64px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #fff;
  color: var(--muted);
  font-size: 12px;
}

.sig-preview img {
  max-width: 190px;
  max-height: 56px;
}

.save-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
  padding: 14px 0;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
}

/* ---------- Dialog ---------- */

.dialog {
  width: min(560px, calc(100vw - 32px));
  border: 0;
  border-radius: 14px;
  padding: 0;
  box-shadow: 0 24px 64px rgb(16 24 40 / 0.25);
  color: var(--text);
}

.dialog::backdrop {
  background: rgb(16 20 36 / 0.45);
}

.dialog form {
  display: flex;
  flex-direction: column;
}

.dialog-head {
  padding: 20px 22px 4px;
}

.dialog-body {
  padding: 14px 22px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: calc(100vh - 200px);
  overflow-y: auto;
}

.dialog-foot {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 14px 22px;
  border-top: 1px solid var(--line);
  background: var(--soft);
}

.dialog-foot .spacer {
  flex: 1;
}

.gst-msg {
  font-size: 12px;
}

.gst-msg.ok {
  color: var(--ok);
}

.gst-msg.bad {
  color: var(--bad);
}

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13.5px;
  box-shadow: 0 8px 24px rgb(16 24 40 / 0.25);
  z-index: 50;
}

/* ---------- Small screens ---------- */

@media (max-width: 980px) {
  .editor {
    grid-template-columns: 1fr;
    height: auto;
  }

  .form-panel {
    border-right: 0;
  }

  .form-scroll {
    overflow: visible;
  }

  .preview-pane {
    padding: 16px;
  }

  .tabs a {
    padding: 0 8px;
  }

  .bank-row {
    grid-template-columns: 1fr 1fr 36px;
  }
}

/* ---------- Sign-in page ---------- */

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.login {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px 24px 24px;
  box-shadow: 0 12px 32px rgb(16 24 40 / 0.08);
}

.login-brand {
  margin: 0 0 20px;
  font-size: 17px;
}

.login input[type='password'] {
  height: 44px;
  font-size: 18px;
  letter-spacing: 0.2em;
}

.topbar .signout {
  margin-left: auto;
}
