/*
  Hand-rolled stylesheet - no build step, no framework. Deliberately not a
  Tailwind CDN <script> tag: Tailwind's own docs flag that as unsuitable for
  production (it recompiles client-side on every page load). Palette matches
  the approved dashboard mockup (neutral zinc, single dark accent).
*/

:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --border: #e4e4e7;
  --border-subtle: #f4f4f5;
  --text: #18181b;
  --text-muted: #71717a;
  --text-faint: #a1a1aa;
  --accent: #18181b;
  --ok-bg: #ecfdf5; --ok-fg: #059669; --ok-border: #d1fae5;
  --warn-bg: #fffbeb; --warn-fg: #d97706;
  --danger-bg: #fef2f2; --danger-fg: #dc2626;
  --neutral-bg: #f4f4f5; --neutral-fg: #52525b;
  --radius: 10px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

a { color: inherit; }
button { font-family: inherit; }
input, select { font-family: inherit; color: var(--text); }
input::placeholder { color: var(--text-faint); }

/* ---------- App shell ---------- */

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 248px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-sizing: border-box;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px 28px 8px;
}

.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.brand-name {
  font-size: 16px;
  font-weight: 600;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-grow: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
}

.nav-item.active,
.nav-item:hover {
  background: var(--neutral-bg);
}

.user-chip {
  border-top: 1px solid var(--border);
  padding-top: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 9999px;
  background: var(--neutral-bg);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.user-info {
  flex-grow: 1;
  min-width: 0;
}

.user-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-email {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  text-decoration: none;
  flex-shrink: 0;
}

.icon-btn:hover { background: var(--neutral-bg); }

.main {
  flex-grow: 1;
  min-width: 0;
  padding: 32px 40px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ---------- Topbar ---------- */

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.topbar h1 { margin: 0; font-size: 22px; font-weight: 600; }
.topbar-subtitle { margin: 4px 0 0 0; font-size: 14px; color: var(--text-muted); }

.sync-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 9999px;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
}

.sync-dot { width: 8px; height: 8px; border-radius: 9999px; }

.sync-ok { background: var(--ok-bg); border: 1px solid var(--ok-border); color: var(--ok-fg); }
.sync-ok .sync-dot { background: var(--ok-fg); }

.sync-failed { background: var(--danger-bg); color: var(--danger-fg); }
.sync-failed .sync-dot { background: var(--danger-fg); }

.sync-neutral { background: var(--neutral-bg); color: var(--neutral-fg); }
.sync-neutral .sync-dot { background: var(--text-faint); }

/* ---------- Stat cards ---------- */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-value { font-size: 24px; font-weight: 600; }
.stat-sub { font-size: 12px; color: var(--text-faint); }

/* ---------- Filters ---------- */

.filters {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.search-wrap { position: relative; flex-grow: 1; max-width: 340px; min-width: 220px; }

.search-icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  color: var(--text-faint);
}

.search-wrap input {
  width: 100%;
  padding: 8px 12px 8px 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--surface);
}

.filters input[type="date"],
.filters select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--surface);
}

.filter-submit {
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.filter-submit:hover { opacity: 0.9; }

/* ---------- Table ---------- */

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.table-header,
.table-row {
  display: grid;
  grid-template-columns: 110px 1fr 160px 140px 150px 90px;
  gap: 12px;
  padding: 12px 20px;
  align-items: center;
}

.table-header {
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.col-actions-header { text-align: right; }

.table-row { border-bottom: 1px solid var(--border-subtle); font-size: 13px; }
.table-row:last-child { border-bottom: none; }

.cell-sender { min-width: 0; }
.sender-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sender-vat { font-size: 12px; color: var(--text-faint); }

.cell-amount { font-weight: 600; font-variant-numeric: tabular-nums; }

.cell-actions { display: flex; gap: 4px; justify-content: flex-end; }

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

/* ---------- Badges ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 9999px;
  font-size: 12px;
  font-weight: 500;
}

.badge-ok { background: var(--ok-bg); color: var(--ok-fg); }
.badge-warning { background: var(--warn-bg); color: var(--warn-fg); }
.badge-danger { background: var(--danger-bg); color: var(--danger-fg); }
.badge-neutral { background: var(--neutral-bg); color: var(--neutral-fg); }

/* ---------- Pagination ---------- */

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

.page-info { font-size: 13px; color: var(--text-muted); }
.page-buttons { display: flex; align-items: center; gap: 6px; }

.page-btn {
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--text);
}

.page-btn:hover:not(.page-btn-disabled) { background: var(--bg); }
.page-btn-disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- Detail page ---------- */

.back-link {
  font-size: 13px;
  color: var(--text-muted);
  text-decoration: none;
}
.back-link:hover { color: var(--text); }

.detail-actions { display: flex; gap: 8px; }

.btn-primary, .btn-secondary {
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { opacity: 0.9; }

.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--bg); }

.notice-warning {
  background: var(--warn-bg);
  color: var(--warn-fg);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 13px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.detail-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}

.detail-card h2 {
  margin: 0 0 12px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.detail-field {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 0;
  font-size: 14px;
  border-bottom: 1px solid var(--border-subtle);
}
.detail-field:last-child { border-bottom: none; }
.detail-field span { color: var(--text-muted); }

.notification-list {
  margin: 0;
  padding-left: 18px;
  font-size: 13px;
  color: var(--text-muted);
}

/* ---------- Login page ---------- */

.login-body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.login-card {
  width: 320px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 32px;
  text-align: center;
}

.login-brand-mark {
  margin: 0 auto 16px auto;
  width: 44px;
  height: 44px;
  font-size: 20px;
  border-radius: 12px;
}

.login-card h1 { margin: 0; font-size: 20px; font-weight: 600; }
.login-subtitle { margin: 4px 0 28px 0; font-size: 13px; color: var(--text-muted); }

.google-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  text-decoration: none;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
}
.google-btn:hover { background: var(--bg); }

.login-footnote {
  margin: 16px 0 0 0;
  font-size: 12px;
  color: var(--text-faint);
}

.error-msg {
  background: var(--danger-bg);
  color: var(--danger-fg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
  margin: 0 0 16px 0;
}

/* ---------- Shared helpers (v2 pages) ---------- */

.mono { font-variant-numeric: tabular-nums; }
.col-right { text-align: right; }
.cell-strong { font-weight: 600; }
.cell-muted { color: var(--text-muted); font-size: 12px; }
.pre-line { white-space: pre-line; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

.flash {
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 14px;
  line-height: 1.5;
}
.flash ul { margin: 6px 0 0 0; padding-left: 18px; }
.flash-success { background: var(--ok-bg); color: #047857; border: 1px solid var(--ok-border); }
.flash-error { background: var(--danger-bg); color: #b91c1c; border: 1px solid #fecaca; }
.flash-warning { background: var(--warn-bg); color: #92400e; border: 1px solid #fde68a; }
.flash-info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }

.stat-danger { color: var(--danger-fg); }
.badge-title { vertical-align: middle; margin-left: 8px; font-size: 13px; }

.btn-primary, .btn-secondary { border: none; cursor: pointer; font-family: inherit; }
.btn-secondary { border: 1px solid var(--border); }
.btn-small { padding: 6px 12px; font-size: 13px; }
.btn-link {
  font-size: 13px; font-weight: 500; color: var(--text); text-decoration: none;
  padding: 4px 8px; border-radius: 6px;
}
.btn-link:hover { background: var(--neutral-bg); }
.detail-actions form { display: contents; }

/* ---------- Data tables (customers, issued invoices) ---------- */

.table-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}

.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  text-align: left;
  padding: 10px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.data-table th.col-right { text-align: right; }
.data-table td { padding: 12px 16px; border-bottom: 1px solid var(--border-subtle); vertical-align: middle; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table a { color: inherit; text-decoration: none; }
.row-link { cursor: pointer; }
.row-link:hover td { background: var(--bg); }

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

.form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.form-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px 16px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; }
.field.span-2 { grid-column: span 2; }
.field[hidden], .form-hint[hidden], small[hidden] { display: none; }
.field small { font-weight: 400; color: var(--text-muted); }
.field-link { font-weight: 400; margin-left: 8px; color: var(--text-muted); }
.field input, .field select, .field textarea,
.lines-table input, .lines-table select, .lines-table textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--surface);
  font-family: inherit;
}
.field input:focus, .field select:focus, .field textarea:focus,
.lines-table input:focus, .lines-table select:focus, .lines-table textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.field textarea, .lines-table textarea { resize: vertical; }

.kind-picker { border: none; margin: 0; padding: 0; display: flex; gap: 8px; flex-wrap: wrap; }
.kind-picker legend { font-size: 13px; font-weight: 500; margin-bottom: 8px; }
.kind-option {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  cursor: pointer;
}
.kind-option:has(input:checked) { border-color: var(--accent); background: var(--neutral-bg); }

.form-hint { margin: 0; font-size: 13px; color: var(--text-muted); }
.form-actions { display: flex; gap: 8px; }
.section-title {
  margin: 4px 0 0 0; font-size: 13px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.03em;
}

.danger-zone { margin-top: -4px; }
.btn-danger-link {
  background: none; border: none; padding: 0; cursor: pointer;
  color: var(--danger-fg); font-size: 13px; font-family: inherit;
}
.btn-danger-link:hover { text-decoration: underline; }

/* ---------- Invoice lines ---------- */

.lines-table-wrap { overflow-x: auto; }
.lines-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.lines-table th {
  text-align: left; padding: 0 6px 6px 0;
  font-size: 12px; font-weight: 500; color: var(--text-muted);
}
.lines-table td { padding: 0 6px 8px 0; vertical-align: top; }
.lines-table .col-desc { width: 44%; min-width: 220px; }
.lines-table .col-num { width: 12%; min-width: 90px; }
.lines-table .col-vat { width: 18%; min-width: 130px; }
.lines-table .line-total { padding-top: 9px; text-align: right; white-space: nowrap; }

.form-bottom { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; flex-wrap: wrap; }
.form-bottom-end { justify-content: flex-end; }
.stats-grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.topbar-subtitle a { color: inherit; }

.totals-box { margin: 0; min-width: 240px; font-size: 14px; }
.totals-box div { display: flex; justify-content: space-between; gap: 24px; padding: 4px 0; }
.totals-box dt { color: var(--text-muted); }
.totals-box dd { margin: 0; font-variant-numeric: tabular-nums; }
.totals-grand { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 8px !important; font-weight: 600; font-size: 16px; }
.totals-grand dt { color: var(--text); }
.totals-inline { margin-left: auto; padding: 12px 16px; max-width: 320px; }

/* ---------- SDI replies timeline ---------- */

.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; font-size: 14px; }
.timeline li { padding-left: 14px; border-left: 3px solid var(--border); }
.timeline .cell-muted { margin-left: 8px; }
.timeline-good { border-left-color: var(--ok-fg) !important; }
.timeline-bad { border-left-color: var(--danger-fg) !important; }
.timeline-error { color: #b91c1c; font-size: 13px; margin-top: 2px; }

/* ---------- Confirm dialog ---------- */

.dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 440px;
  width: calc(100% - 32px);
  font-family: inherit;
  color: var(--text);
}
.dialog::backdrop { background: rgba(24, 24, 27, 0.45); }
.dialog h2 { margin: 0 0 16px 0; font-size: 18px; }
.dialog-summary { margin: 0 0 16px 0; font-size: 14px; }
.dialog-summary div { display: flex; justify-content: space-between; gap: 16px; padding: 4px 0; }
.dialog-summary dt { color: var(--text-muted); }
.dialog-summary dd { margin: 0; font-weight: 600; text-align: right; }
.dialog-warning {
  background: var(--warn-bg); color: #92400e;
  border-radius: var(--radius-sm); padding: 10px 12px; font-size: 13px; line-height: 1.5;
  margin: 0 0 18px 0;
}

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

@media (max-width: 900px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; flex-direction: row; align-items: center; flex-wrap: wrap; }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .user-chip { border-top: none; padding-top: 0; }
  .main { padding: 20px; }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .table-header, .table-row { grid-template-columns: 90px 1fr 110px 100px; }
  .table-header > div:nth-child(5), .table-row > div:nth-child(5) { display: none; }
  .detail-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr 1fr; }
  .field.span-2 { grid-column: span 2; }
}
