@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
  /* Brand */
  --brand: #E1251B;
  --brand-dark: #B81B16;
  --brand-soft: #FDECEA;

  /* Neutrals */
  --bg: #F4F5F7;
  --surface: #FFFFFF;
  --border: #E5E7EB;
  --text: #1A1D23;
  --muted: #6B7280;
  --muted-2: #9AA1AC;

  /* Status */
  --st-draft-fg: #6B7280;
  --st-draft-bg: #F3F4F6;
  --st-issued-fg: #047857;
  --st-issued-bg: #ECFDF5;
  --st-void-fg: #B91C1C;
  --st-void-bg: #FEF2F2;

  /* Shape */
  --r: 8px;
  --r-sm: 6px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.1);

  /* Layout */
  --sidebar-w: 240px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--brand); }

.num { font-variant-numeric: tabular-nums; }

/* ===== Focus ring ===== */
:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* ===========================================================
   Shell layout (logged in)
   =========================================================== */
.app-shell {
  display: flex;
  min-height: 100dvh;
}

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100dvh;
}

.sidebar-logo {
  padding: 20px;
}
.sidebar-logo img { display: block; }

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 12px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 12px;
  border-radius: var(--r-sm);
  color: var(--text);
  text-decoration: none;
  font-size: 15px;
  border-left: 3px solid transparent;
  transition: background .15s ease, color .15s ease;
}
.nav-item:hover { background: #F3F4F6; }
.nav-item svg { flex: 0 0 auto; color: var(--muted); transition: color .15s ease; }
.nav-item.active {
  background: var(--brand-soft);
  color: var(--brand);
  border-left-color: var(--brand);
  font-weight: 600;
}
.nav-item.active svg { color: var(--brand); }

.sidebar-foot {
  margin-top: auto;
  padding: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sidebar-user {
  font-size: 13px;
  color: var(--muted);
  padding: 0 4px;
}
.sidebar-logout {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--muted);
  text-decoration: none;
  padding: 6px 4px;
  border-radius: var(--r-sm);
  transition: color .15s ease;
}
.sidebar-logout:hover { color: var(--brand); }
.sidebar-logout svg { color: inherit; }

.main {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--bg);
  padding: 32px;
  min-height: 100dvh;
}

/* ===========================================================
   Logged-out (auth) layout
   =========================================================== */
.auth-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 360px;
}
.auth-card .auth-logo {
  display: block;
  width: 160px;
  margin: 0 auto 20px;
}
.auth-card h1 { text-align: center; margin: 0 0 20px; }
.auth-card label { margin-bottom: 14px; }
.auth-card .btn-primary,
.auth-card button[type="submit"] { width: 100%; }
.auth-card .error {
  color: var(--brand-dark);
  background: var(--brand-soft);
  border: 1px solid var(--brand-soft);
  border-radius: var(--r-sm);
  padding: 8px 12px;
  font-size: 13px;
  margin: 0 0 14px;
}

/* ===========================================================
   Typography & page head
   =========================================================== */
h1 { font-size: 22px; font-weight: 600; margin: 0 0 16px; line-height: 1.25; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.page-head h1 { font-size: 20px; margin: 0; }
.page-head-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ===========================================================
   Card
   =========================================================== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card + .card { margin-top: 16px; }

/* ===========================================================
   Buttons
   =========================================================== */
.btn,
a.btn,
button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 16px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, color .15s ease;
}
.btn:hover,
a.btn:hover,
button:hover { background: #F3F4F6; }

button[type="submit"],
.btn-primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  font-weight: 600;
}
button[type="submit"]:hover,
.btn-primary:hover {
  background: var(--brand-dark);
  border-color: var(--brand-dark);
  color: #fff;
}

button:disabled,
.btn:disabled,
button[disabled] { opacity: .5; cursor: not-allowed; }

.btn svg { flex: 0 0 auto; }

.btn-del {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
  width: 28px;
  height: 28px;
  line-height: 1;
  font-size: 16px;
  color: var(--muted-2);
  border: 1px solid transparent;
  background: transparent;
  border-radius: var(--r-sm);
}
.btn-del:hover { color: var(--brand); background: var(--brand-soft); }

/* ===========================================================
   Filters / chips
   =========================================================== */
.filters { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.chip {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  background: var(--surface);
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.chip:hover { background: #F3F4F6; }
.chip.active {
  background: var(--brand-soft);
  color: var(--brand);
  border-color: var(--brand);
}
.new-doc { display: flex; gap: 8px; align-items: center; }

/* ===========================================================
   Tables
   =========================================================== */
table.grid {
  width: 100%;
  border-collapse: collapse;
  background: transparent;
}
table.grid th,
table.grid td {
  text-align: left;
  padding: 11px 12px;
  vertical-align: middle;
}
table.grid thead th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .03em;
  border-bottom: 1px solid var(--border);
}
table.grid tbody td {
  border-bottom: 1px solid #F1F2F4;
  font-size: 14px;
}
table.grid tbody tr:last-child td { border-bottom: none; }
table.grid tbody tr:hover { background: #FAFAFB; }
table.grid td.num,
table.grid th.num { text-align: right; }

/* ===========================================================
   Status badge
   =========================================================== */
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.5;
}
.badge-draft  { color: var(--st-draft-fg);  background: var(--st-draft-bg); }
.badge-issued { color: var(--st-issued-fg); background: var(--st-issued-bg); }
.badge-void   { color: var(--st-void-fg);   background: var(--st-void-bg); }

/* keep legacy .status classes mapped to badge look (defensive) */
.status { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.status-draft  { color: var(--st-draft-fg);  background: var(--st-draft-bg); }
.status-issued { color: var(--st-issued-fg); background: var(--st-issued-bg); }
.status-void   { color: var(--st-void-fg);   background: var(--st-void-bg); }

/* ===========================================================
   Forms
   =========================================================== */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}

label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
}
.form-grid > label,
.form label { margin-bottom: 6px; }

/* label text sits above input; the input is block below */
label > input,
label > select,
label > textarea { margin-top: 6px; }

input, select, textarea {
  font: inherit;
  font-size: 14px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  background: var(--surface);
  width: 100%;
  transition: border-color .15s ease, box-shadow .15s ease;
}
input::placeholder, textarea::placeholder { color: var(--muted-2); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 2px var(--brand-soft);
}
textarea { min-height: 4rem; resize: vertical; }

.muted { color: var(--muted); font-size: 12px; font-weight: 400; }
.hint-link { font-size: 12px; display: inline-block; margin-top: 4px; }

/* Items table inputs are compact and inline */
.items-table input { padding: 6px 8px; }
.items-table thead th { white-space: nowrap; }
.col-pos { width: 2.5rem; text-align: center; color: var(--muted); }
.col-qty { width: 5rem; }
.col-unit { width: 5rem; }
.col-price, .col-line { width: 9rem; }
.col-actions { width: 3rem; text-align: center; }

#add-row { margin: 12px 0; }

.actions, .doc-secondary-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 16px;
}
.doc-secondary-actions {
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.inline-form { display: inline-flex; gap: 8px; margin: 0; align-items: center; }

/* ===========================================================
   Totals
   =========================================================== */
.totals {
  margin: 16px 0 16px auto;
  max-width: 320px;
}
.totals div {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 14px;
}
.totals .total-row {
  border-top: 1px solid var(--border);
  font-weight: 700;
  margin-top: 4px;
  padding-top: 8px;
  font-size: 16px;
}

/* ===========================================================
   Utilities
   =========================================================== */
.hidden { display: none !important; }
.doc-form.is-delivery .price-col { display: none !important; }

/* ===========================================================
   Responsive: sidebar -> top bar
   =========================================================== */
@media (max-width: 860px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    flex: 0 0 auto;
    height: auto;
    position: static;
    border-right: none;
    border-bottom: 1px solid var(--border);
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px 12px;
  }
  .sidebar-logo { padding: 8px 12px; }
  .sidebar-logo img { width: 120px !important; }
  .sidebar-nav {
    flex-direction: row;
    flex: 1 1 auto;
    padding: 0;
    gap: 4px;
    flex-wrap: wrap;
  }
  .nav-item {
    border-left: none;
    border-bottom: 3px solid transparent;
  }
  .nav-item.active { border-left: none; border-bottom-color: var(--brand); }
  .sidebar-foot {
    margin-top: 0;
    border-top: none;
    border-left: 1px solid var(--border);
    flex-direction: row;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
  }
  .main { padding: 20px 16px; }
  .form-grid { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
  .form-grid { grid-template-columns: 1fr; }
}

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