:root {
  --black: #0d0d0d;
  --gray-900: #1a1a1a;
  --gray-700: #444;
  --gray-500: #767676;
  --gray-300: #d8d8d8;
  --gray-100: #f4f4f4;
  --white: #fff;
  --accent: #0d0d0d;
  --danger: #b3261e;
  --success: #1e7a34;
  --warn: #a8710a;
  --radius: 6px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--gray-900);
  background: var(--gray-100);
}

a { color: inherit; }

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

.sidebar {
  width: 220px;
  background: var(--black);
  color: var(--white);
  display: flex;
  flex-direction: column;
  padding: 24px 0;
  flex-shrink: 0;
}

.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 20px 24px;
  border-bottom: 1px solid #333;
  margin-bottom: 16px;
}

.sidebar .brand img { height: 28px; filter: invert(1); }
.sidebar .brand span { font-weight: 700; letter-spacing: 0.02em; font-size: 15px; }

.sidebar-top { display: flex; align-items: center; justify-content: space-between; }
.nav-toggle-checkbox { display: none; }
.nav-toggle-label {
  display: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid #444;
  border-radius: var(--radius);
  color: var(--white);
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
}

.sidebar nav a {
  display: block;
  padding: 10px 20px;
  text-decoration: none;
  color: #cfcfcf;
  font-size: 14px;
  font-weight: 500;
  border-left: 3px solid transparent;
}

.sidebar nav a:hover { background: #1f1f1f; color: var(--white); }
.sidebar nav a.active {
  background: #1f1f1f;
  color: var(--white);
  border-left-color: var(--white);
}

.logout-form { margin-top: auto; padding: 16px 20px 0; }
.logout-btn {
  width: 100%;
  padding: 9px 12px;
  background: transparent;
  border: 1px solid #444;
  color: #cfcfcf;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.logout-btn:hover { background: #1f1f1f; color: var(--white); border-color: #666; }

.auth-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--gray-100);
  padding: 20px;
}
.auth-card {
  background: var(--white);
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  padding: 40px;
  width: 100%;
  max-width: 380px;
}
.auth-logo { height: 36px; margin-bottom: 24px; display: block; }
.auth-card h1 { font-size: 20px; margin: 0 0 8px; }
.auth-card p.sub { color: var(--gray-500); font-size: 13px; margin: 0 0 24px; }

.main { flex: 1; padding: 32px 40px; max-width: 1100px; }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  gap: 16px;
  flex-wrap: wrap;
}

.page-header h1 { margin: 0; font-size: 22px; }
.page-header .sub { color: var(--gray-500); font-size: 13px; margin-top: 4px; }

.card {
  background: var(--white);
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 20px;
}

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--gray-300); }
th { color: var(--gray-500); font-weight: 600; text-transform: uppercase; font-size: 11px; letter-spacing: 0.04em; }
tbody tr:hover { background: var(--gray-100); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.btn {
  display: inline-block;
  padding: 9px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--black);
  background: var(--black);
  color: var(--white);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { background: var(--gray-900); }
.btn.secondary { background: var(--white); color: var(--black); }
.btn.secondary:hover { background: var(--gray-100); }
.btn.danger { background: var(--white); color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: #fbe9e8; }
.btn.small { padding: 5px 10px; font-size: 12px; }

form .field { margin-bottom: 16px; }
form label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
form input[type=text], form input[type=email], form input[type=tel],
form input[type=date], form input[type=number], form textarea, form select {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  background: var(--white);
}
form textarea { resize: vertical; min-height: 70px; }
.row { display: flex; gap: 16px; flex-wrap: wrap; }
.row > .field { flex: 1 1 220px; }

.items-table th, .items-table td { padding: 8px; }
.items-table input { border: 1px solid var(--gray-300); border-radius: 4px; padding: 6px 8px; font-size: 13px; }
.items-table td.qty, .items-table td.price { text-align: right; }
.items-table td.qty input, .items-table td.price input { width: 100px; text-align: right; }
.items-table td.remove { width: 32px; text-align: center; }

.totals-box { max-width: 340px; margin-left: auto; font-size: 14px; }
.totals-box .line { display: flex; justify-content: space-between; padding: 6px 0; }
.totals-box .line.grand { border-top: 2px solid var(--black); margin-top: 6px; padding-top: 10px; font-weight: 700; font-size: 16px; }
.totals-box input { width: 110px; text-align: right; padding: 6px 8px; border: 1px solid var(--gray-300); border-radius: 4px; }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--gray-300);
  color: var(--gray-900);
}
.badge.draft { background: var(--gray-300); color: var(--gray-700); }
.badge.sent { background: #dbe6ff; color: #1a4fb4; }
.badge.accepted, .badge.paid { background: #d6f0da; color: var(--success); }
.badge.converted { background: #e6dcfa; color: #5a2ea6; }
.badge.overdue, .badge.expired, .badge.void { background: #fbe0de; color: var(--danger); }

.actions { display: flex; gap: 10px; flex-wrap: wrap; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--gray-500); }
.empty-state a { font-weight: 600; }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; margin-bottom: 20px; }
.detail-grid .block h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--gray-500); }
.detail-grid .block p { margin: 2px 0; font-size: 14px; }

.flash { padding: 12px 16px; border-radius: var(--radius); margin-bottom: 20px; font-size: 14px; }
.flash.success { background: #d6f0da; color: var(--success); }
.flash.error { background: #fbe0de; color: var(--danger); }

.signature-note { font-size: 12px; color: var(--gray-500); margin-top: 6px; }

.combobox { position: relative; }
.combobox input.combobox-input.invalid { border-color: var(--danger); }
.dropdown-list {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  max-height: 260px;
  overflow-y: auto;
  background: var(--white);
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  z-index: 20;
  margin-top: 4px;
}
.dropdown-item { padding: 8px 12px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; }
.dropdown-item:hover { background: var(--gray-100); }
.dropdown-item strong { font-size: 13px; }
.dropdown-item span { font-size: 12px; color: var(--gray-500); }
.dropdown-empty { padding: 10px 12px; font-size: 13px; color: var(--gray-500); }

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ---------- Responsive / mobile ---------- */

@media (max-width: 860px) {
  .app { flex-direction: column; min-height: 0; }

  .sidebar {
    width: 100%;
    padding: 14px 16px;
    flex-direction: column;
  }
  .sidebar .brand { padding: 0; border-bottom: none; margin-bottom: 0; }

  .nav-toggle-label { display: flex; }

  .sidebar nav,
  .sidebar .logout-form {
    display: none;
    margin-top: 12px;
    padding-top: 0;
  }
  .nav-toggle-checkbox:checked ~ nav,
  .nav-toggle-checkbox:checked ~ .logout-form {
    display: flex;
    flex-direction: column;
  }
  .sidebar nav a { border-left: none; border-radius: var(--radius); }

  .main { padding: 20px 16px; max-width: none; }
}

@media (max-width: 640px) {
  .card { padding: 16px; }
  .auth-card { padding: 28px 22px; }

  .detail-grid { grid-template-columns: 1fr; gap: 20px; }

  .totals-box { max-width: none; }

  /* Stack read-only tables into cards: one row per record, label above value. */
  table.responsive-table thead { display: none; }
  table.responsive-table, table.responsive-table tbody,
  table.responsive-table tr, table.responsive-table td {
    display: block;
    width: 100%;
  }
  table.responsive-table tr {
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    padding: 4px 12px;
    margin-bottom: 10px;
  }
  table.responsive-table td {
    border-bottom: none;
    padding: 6px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    text-align: right;
  }
  table.responsive-table td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--gray-500);
    text-align: left;
  }
  table.responsive-table td.actions { justify-content: flex-end; }
  table.responsive-table td.actions::before { display: none; }

  /* Stack the editable line-item table into one card per item. Each row becomes
     a flex container so description/qty/price/remove can wrap onto their own
     lines without the inline-block whitespace-gap trap. */
  .items-table, .items-table tbody { display: block; width: 100%; }
  .items-table thead { display: none; }
  .items-table tr {
    display: flex;
    flex-wrap: wrap;
    column-gap: 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    padding: 10px 12px;
    margin-bottom: 10px;
  }
  .items-table td { display: block; padding: 6px 0; border-bottom: none; }
  .items-table td::before {
    content: attr(data-label);
    display: block;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--gray-500);
    margin-bottom: 4px;
  }
  .items-table td:first-child { flex: 1 1 100%; }
  .items-table td.qty, .items-table td.price { flex: 1 1 calc(50% - 6px); }
  .items-table td.qty input, .items-table td.price input { width: 100%; text-align: left; }
  .items-table td.remove { flex: 1 1 100%; text-align: right; }
  .items-table td.remove::before { display: none; }

  .totals-box .line { gap: 12px; }
}
