/* ---------------------------------------------------------------------------
   Invoice generator stylesheet.

   Self-hosted, no framework, no web fonts, no CDN. That is a performance choice
   (one ~12 KB request instead of 300 KB of framework) and a security one: the
   Content-Security-Policy can forbid every external origin because nothing is
   loaded from one.
   ------------------------------------------------------------------------- */

:root {
  --brand: #1f3a5f;
  --brand-dark: #16293f;
  --accent: #2f7d64;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e2e6ec;
  --text: #1c2530;
  --muted: #6b7683;
  --danger: #c0392b;
  --warning: #b06f00;
  --ok: #1e7d45;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(20, 30, 45, .06), 0 4px 14px rgba(20, 30, 45, .05);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

body.centered { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
body.centered .main { width: 100%; }

.wrap { width: min(1180px, 100% - 2rem); margin-inline: auto; }
.main { padding: 1.5rem 0 3rem; }

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
h3 { font-size: .92rem; margin: 1.25rem 0 .4rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.danger { color: var(--danger); }
.ok { color: var(--ok); }
.strong th, .strong td { font-weight: 600; }
.prewrap { white-space: pre-wrap; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

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

.topbar { background: var(--brand); color: #fff; }
.topbar-inner { display: flex; align-items: center; gap: 1.25rem; height: 56px; }
.brand { color: #fff; font-weight: 700; letter-spacing: .01em; white-space: nowrap; }
.brand-mark { opacity: .55; }
.topnav { display: flex; gap: .95rem; flex: 1; overflow-x: auto; }
.topnav a { color: rgba(255, 255, 255, .82); font-size: .9rem; white-space: nowrap; padding: .3rem 0; }
.topnav a:hover, .topnav a[aria-current] { color: #fff; text-decoration: none; box-shadow: inset 0 -2px 0 rgba(255, 255, 255, .8); }
.nav-ai { color: #ffd9a0 !important; }
.topbar-right { display: flex; align-items: center; gap: .6rem; }

.menu { position: relative; }
.menu summary { list-style: none; cursor: pointer; color: rgba(255, 255, 255, .9); font-size: .88rem; padding: .35rem .5rem; border-radius: var(--radius); }
.menu summary::-webkit-details-marker { display: none; }
.menu summary:hover { background: rgba(255, 255, 255, .12); }
.menu-body { position: absolute; right: 0; top: 2.2rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 190px; padding: .35rem; z-index: 20; }
.menu-body a, .menu-body button { display: block; width: 100%; text-align: left; padding: .45rem .6rem; color: var(--text); font-size: .9rem; border-radius: 6px; }
.menu-body a:hover, .menu-body button:hover { background: var(--bg); text-decoration: none; }

/* --- Layout --------------------------------------------------------------- */

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.1rem; }
.page-actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem 1.15rem; box-shadow: var(--shadow); margin-bottom: 1rem; }
.card.narrow { max-width: 640px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; }
.auth-card { max-width: 400px; margin: 3rem auto; }
.notice-card { border-left: 3px solid var(--warning); }

.footer { display: flex; justify-content: space-between; padding: 1rem 0 2rem; color: var(--muted); font-size: .8rem; }

/* --- Stats ---------------------------------------------------------------- */

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .85rem 1rem; box-shadow: var(--shadow); }
.stat-alert { border-color: #f0c9c4; background: #fff8f7; }
.stat-label { display: block; font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.stat-value { display: block; font-size: 1.45rem; font-weight: 650; margin: .15rem 0; }
.stat-value small { font-size: .8rem; font-weight: 400; color: var(--muted); }
.stat-note { font-size: .8rem; color: var(--muted); }

/* --- Forms ---------------------------------------------------------------- */

.field { margin-bottom: .8rem; }
.field label { display: block; font-size: .8rem; font-weight: 600; margin-bottom: .25rem; color: #3a4653; }
.input, input[type=text], input[type=email], input[type=password], input[type=date],
input[type=search], input[type=number], input[type=color], select, textarea {
  width: 100%; padding: .5rem .6rem; border: 1px solid #cdd4dd; border-radius: 6px;
  background: #fff; font: inherit; color: var(--text);
}
input[type=color] { padding: .2rem; height: 2.2rem; }
textarea { min-height: 5rem; resize: vertical; }
.input:focus, select:focus, textarea:focus { outline: 2px solid rgba(31, 58, 95, .35); outline-offset: 1px; border-color: var(--brand); }
.input[readonly] { background: #f4f6f8; }
.input:disabled { background: #eef1f4; color: var(--muted); }
.field-error .input { border-color: var(--danger); }
.hint { font-size: .78rem; color: var(--muted); margin: .25rem 0 0; }
.error { font-size: .8rem; color: var(--danger); margin: .25rem 0 0; }
.check { display: flex; align-items: center; gap: .45rem; font-size: .88rem; }
.check input { width: auto; }
.field-check { margin-bottom: .7rem; }
.filters { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.filters .input { width: auto; min-width: 220px; }
.form-actions { display: flex; gap: .5rem; align-items: center; margin-top: 1rem; }
.code-input { font-family: var(--mono); font-size: 1.3rem; letter-spacing: .3em; text-align: center; }

/* --- Buttons -------------------------------------------------------------- */

.btn, button.btn, input[type=submit] {
  display: inline-block; padding: .5rem .9rem; border: 1px solid #cdd4dd; border-radius: 6px;
  background: #fff; color: var(--text); font: inherit; font-size: .88rem; font-weight: 550;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: #f2f5f8; text-decoration: none; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-danger { background: #fff; border-color: #e3b7b1; color: var(--danger); }
.btn-danger:hover { background: #fdf2f0; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--brand); }
.btn-ghost:hover { background: rgba(31, 58, 95, .07); }
.btn-sm { padding: .28rem .55rem; font-size: .8rem; }
.btn-block { width: 100%; }
.btn:disabled, input[type=submit]:disabled { opacity: .5; cursor: not-allowed; }
.btn-icon { border: none; background: none; font-size: 1.15rem; line-height: 1; color: var(--muted); cursor: pointer; padding: .2rem .4rem; }
.btn-icon:hover { color: var(--danger); }
.linklike { border: none; background: none; padding: 0; color: var(--brand); font: inherit; font-size: .85rem; cursor: pointer; }
.linklike:hover { text-decoration: underline; }
.inline-form { display: inline; }

/* --- Tables --------------------------------------------------------------- */

.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th, .table td { text-align: left; padding: .55rem .6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.table thead th { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); border-bottom-width: 2px; }
.table tbody tr:hover { background: #fafbfc; }
.table .num, .num { text-align: right; font-variant-numeric: tabular-nums; }
.table.compact th, .table.compact td { padding: .35rem .5rem; }
.table-scroll { overflow-x: auto; }
.muted-row td { opacity: .62; }
.table td .muted { display: block; font-size: .8rem; }

/* --- Badges & chips ------------------------------------------------------- */

.badge { display: inline-block; padding: .12rem .45rem; border-radius: 999px; font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; background: #eef1f5; color: #4a5765; }
.badge-paid, .badge-done { background: #e3f4e9; color: var(--ok); }
.badge-overdue, .badge-error { background: #fbe9e6; color: var(--danger); }
.badge-partial, .badge-running { background: #fff3dd; color: var(--warning); }
.badge-sent, .badge-viewed, .badge-queued { background: #e7eefb; color: #2a4f96; }
.badge-void { background: #eceff2; color: #7b8794; text-decoration: line-through; }
.chip { display: inline-block; font-size: .7rem; padding: .05rem .4rem; border: 1px solid var(--border); border-radius: 4px; color: var(--muted); margin-left: .3rem; }

/* --- Flash messages ------------------------------------------------------- */

.flashes { margin-bottom: 1rem; display: grid; gap: .5rem; }
.flash { padding: .6rem .85rem; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); font-size: .9rem; }
.flash-success { border-left: 3px solid var(--ok); }
.flash-danger { border-left: 3px solid var(--danger); }
.flash-warning { border-left: 3px solid var(--warning); }
.flash-info { border-left: 3px solid var(--brand); }
.notice { background: #fffaf0; border: 1px solid #f0dfba; border-radius: var(--radius); padding: .6rem .8rem; font-size: .88rem; }

/* --- Charts --------------------------------------------------------------- */

.chart { width: 100%; height: 220px; }
.chart .axis { stroke: var(--border); stroke-width: 1; }
.chart .bar-billed { fill: #9fb4d0; }
.chart .bar-paid { fill: var(--accent); }
.chart .tick { font-size: 9px; fill: var(--muted); }
.legend { font-size: .78rem; color: var(--muted); display: flex; align-items: center; gap: .4rem; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.swatch-billed { background: #9fb4d0; }
.swatch-paid { background: var(--accent); }
.minibar { display: block; width: 100%; height: 8px; }
.minibar rect { fill: #9fb4d0; }
.bar-cell { width: 45%; vertical-align: middle; }

/* --- Invoice editor ------------------------------------------------------- */

.items-table .col-desc { width: 40%; }
.items-table .col-qty, .items-table .col-tax { width: 8%; }
.items-table .col-unit { width: 10%; }
.items-table .col-price, .items-table .col-total { width: 14%; }
.items-table .col-remove { width: 3%; }
.items-table td { padding: .3rem .35rem; }
.items-table .input { padding: .4rem .45rem; font-size: .88rem; }
.items-actions { display: flex; align-items: center; gap: 1rem; margin-top: .6rem; }
.line-amount { vertical-align: middle; color: var(--muted); font-variant-numeric: tabular-nums; }

.totals { display: flex; justify-content: flex-end; margin-top: 1rem; }
.totals dl { display: grid; grid-template-columns: auto auto; gap: .2rem 1.5rem; min-width: 260px; margin: 0; }
.totals dt { color: var(--muted); font-size: .88rem; }
.totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-size: .9rem; }
.totals .grand { font-weight: 700; font-size: 1.02rem; color: var(--text); border-top: 1px solid var(--border); padding-top: .35rem; margin-top: .2rem; }

.action-grid { display: flex; flex-wrap: wrap; gap: .5rem; }
.detail-list { display: grid; grid-template-columns: 130px 1fr; gap: .3rem .8rem; margin: 0; font-size: .9rem; }
.detail-list dt { color: var(--muted); }
.detail-list dd { margin: 0; }

/* --- AI ------------------------------------------------------------------- */

.ai-review { border-left: 3px solid #d9a441; }
.assumptions, .plain-list { margin: .4rem 0; padding-left: 1.1rem; font-size: .9rem; }
.assumptions li, .plain-list li { margin-bottom: .3rem; }
.confidence-high { color: var(--ok); }
.confidence-medium { color: var(--warning); }
.confidence-low { color: var(--danger); }
.draft-body { background: #f7f8fa; border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem; white-space: pre-wrap; font: inherit; font-size: .88rem; }
.lead { font-size: 1rem; }

.working { display: flex; align-items: center; gap: .6rem; font-size: .95rem; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }

/* --- Misc ----------------------------------------------------------------- */

.empty { text-align: center; padding: 3rem 1rem; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }
.empty h3 { text-transform: none; letter-spacing: 0; color: var(--text); font-size: 1.05rem; }
.empty p { color: var(--muted); max-width: 46ch; margin: .4rem auto 1rem; }
.pagination { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .8rem 0; font-size: .88rem; }
.pagination .disabled { color: #b6bec7; }
.page-info { color: var(--muted); }
.steps { padding-left: 1.2rem; font-size: .92rem; }
.steps li { margin-bottom: .8rem; }
.secret { display: inline-block; font-family: var(--mono); background: #f2f4f7; padding: .3rem .5rem; border-radius: 6px; letter-spacing: .08em; word-break: break-all; }
.qr { margin: .6rem 0; }
.qr img { border: 1px solid var(--border); border-radius: var(--radius); background: #fff; padding: .4rem; }
.logo-preview { max-height: 70px; max-width: 220px; }
.error-code { font-size: 2.6rem; color: var(--muted); }

/* --- Public invoice ------------------------------------------------------- */

body.public { background: #eef0f3; }
.public-doc { background: #fff; max-width: 860px; margin: 2rem auto; padding: 2.2rem; border-radius: var(--radius); box-shadow: var(--shadow); }
.public-head { display: flex; justify-content: space-between; gap: 2rem; flex-wrap: wrap; border-bottom: 2px solid var(--brand); padding-bottom: 1rem; margin-bottom: 1.4rem; }
.public-head h1 { font-size: 1.2rem; }
.public-meta { text-align: right; }
.public-meta h2 { font-size: 1.4rem; letter-spacing: .08em; color: var(--brand); margin-bottom: .5rem; }
.public-meta dl { display: grid; grid-template-columns: auto auto; gap: .12rem .8rem; margin: 0; font-size: .85rem; }
.public-meta dt { color: var(--muted); text-align: left; }
.public-meta dd { margin: 0; }
.public-billto { display: flex; justify-content: space-between; gap: 2rem; flex-wrap: wrap; margin-bottom: 1.4rem; }
.public-billto h3 { margin-top: 0; }
.amount-due { background: #f6f8fa; border: 1px solid var(--border); border-radius: var(--radius); padding: .7rem 1.1rem; text-align: right; min-width: 200px; }
.public-totals { display: flex; justify-content: flex-end; margin-top: 1.2rem; }
.public-totals dl { display: grid; grid-template-columns: auto auto; gap: .2rem 2rem; min-width: 280px; margin: 0; }
.public-totals dt { color: var(--muted); font-size: .88rem; }
.public-totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.public-totals .grand { font-weight: 700; border-top: 1px solid var(--border); padding-top: .35rem; }
.public-notes { margin-top: 1.4rem; font-size: .9rem; }
.public-actions { margin-top: 2rem; text-align: center; }

@media print {
  .topbar, .footer, .public-actions, .page-actions { display: none; }
  body, body.public { background: #fff; }
  .public-doc, .card { box-shadow: none; border: none; margin: 0; padding: 0; }
}

@media (max-width: 720px) {
  .row { grid-template-columns: 1fr; }
  .page-head { flex-direction: column; }
  .detail-list { grid-template-columns: 1fr; }
  .public-doc { padding: 1.2rem; }
}
