:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --primary: #2563eb;
  --primary-ink: #ffffff;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 8px 24px rgba(15, 23, 42, .06);
  --radius: 14px;
}

* { box-sizing: border-box; }
/* The hidden attribute must always win, even over .drawer/.lightbox display:flex */
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  padding-bottom: 40px;
}

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 14px;
  padding: 12px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 17px; white-space: nowrap; }
.logo { color: var(--primary); }
.brand-logo { height: 30px; width: auto; display: block; }
.brand-name { font-weight: 700; color: var(--muted); letter-spacing: .01em; }
.login-logo { display: block; height: 58px; width: auto; margin: 0 auto 12px; }
.search { flex: 1; }
.search input {
  width: 100%; max-width: 480px;
  padding: 9px 14px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--bg); font-size: 14px; outline: none;
}
.search input:focus { border-color: var(--primary); background: var(--surface); }

.btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: 9px 14px; border-radius: 10px; font-size: 14px; font-weight: 600;
  cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #cbd5e1; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: #1d4ed8; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-danger { color: #b91c1c; border-color: #fecaca; background: #fef2f2; }
.btn-sm { padding: 6px 10px; font-size: 13px; }

.account { display: flex; align-items: center; gap: 8px; }
.whoami { color: var(--muted); font-size: 13px; font-weight: 600; }

/* ---- tabs ---- */
.tabs {
  display: flex; align-items: center; gap: 6px;
  padding: 12px 18px 4px;
}
.tab {
  border: none; background: transparent; color: var(--muted);
  padding: 7px 14px; border-radius: 999px; font-weight: 600; font-size: 14px; cursor: pointer;
}
.tab.active { background: var(--ink); color: #fff; }
.stats { margin-left: auto; color: var(--muted); font-size: 13px; }

/* ---- list ---- */
.grid {
  display: grid; gap: 14px; padding: 14px 18px;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
.empty { text-align: center; color: var(--muted); padding: 60px 20px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; transition: transform .08s, box-shadow .15s;
  display: flex; flex-direction: column;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.card-thumb {
  height: 140px; background: #e2e8f0 center/cover no-repeat;
  display: flex; align-items: center; justify-content: center; color: #94a3b8; font-size: 28px;
}
.card-body { padding: 12px 14px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.card-row { display: flex; justify-content: space-between; align-items: start; gap: 8px; }
.card-title { margin: 0; font-size: 15.5px; }
.card-client, .card-place { margin: 0; color: var(--muted); font-size: 13px; }
.card-place::before { content: "📍 "; }
.card-foot { margin-top: auto; padding-top: 8px; display: flex; justify-content: space-between; align-items: center; }
.card-total { font-weight: 700; font-size: 15px; }
.card-meta { color: var(--muted); font-size: 12px; }

/* ---- status badges ---- */
.badge {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
  padding: 3px 8px; border-radius: 999px; white-space: nowrap;
}
.badge.draft       { background: #e2e8f0; color: #475569; }
.badge.sent        { background: #dbeafe; color: #1d4ed8; }
.badge.lost        { background: #f3e8ff; color: #7e22ce; }
.badge.accepted    { background: #dcfce7; color: #15803d; }
.badge.in_progress { background: #fef9c3; color: #a16207; }
.badge.completed   { background: #d1fae5; color: #047857; }

/* ---- drawer ---- */
.overlay { position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 30; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 40;
  width: min(560px, 100%); background: var(--surface);
  box-shadow: -8px 0 30px rgba(15,23,42,.18);
  display: flex; flex-direction: column;
  animation: slidein .18s ease;
}
@keyframes slidein { from { transform: translateX(20px); opacity: .6; } to { transform: none; opacity: 1; } }
.drawer header {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 18px; border-bottom: 1px solid var(--line);
}
.drawer header h2 { font-size: 16px; margin: 0; flex: 1; }
.drawer .body { padding: 18px; overflow-y: auto; flex: 1; }
.drawer .footer {
  border-top: 1px solid var(--line); padding: 12px 18px;
  display: flex; gap: 8px; flex-wrap: wrap; align-items: center;
}

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 5px; text-transform: uppercase; letter-spacing: .03em; }
.field input, .field textarea, .field select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  font-size: 14px; font-family: inherit; background: var(--surface); color: var(--ink);
}
.field textarea { resize: vertical; min-height: 70px; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 0 10px; }

/* quick-add category buttons */
.quick-add { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.quick-add button {
  border: 1px solid var(--line); background: var(--bg); color: var(--ink);
  padding: 6px 11px; border-radius: 999px; font-size: 12.5px; font-weight: 600; cursor: pointer;
}
.quick-add button:hover { border-color: var(--primary); color: var(--primary); }

/* line items */
.li-head, .li-row { display: grid; grid-template-columns: 1fr 48px 62px 72px 68px 24px; gap: 6px; align-items: center; }
.li-head { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; margin-bottom: 6px; }
.li-row { margin-bottom: 8px; }
.li-row input, .li-row select { padding: 8px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: 13px; width: 100%; background: var(--surface); }
.li-row select { padding: 8px 4px; }
.li-row .li-line { text-align: right; font-weight: 600; font-size: 13px; }
.li-remove { background: none; border: none; color: #ef4444; font-size: 18px; cursor: pointer; }

/* totals (subtotal / VAT / grand total) */
.totals { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); }
.tot-row { display: flex; justify-content: space-between; align-items: center; font-size: 14px; padding: 3px 0; color: var(--muted); }
.tot-row.grand { font-weight: 700; font-size: 18px; color: var(--ink); margin-top: 4px; padding-top: 8px; border-top: 1px solid var(--line); }
.vat-ctl { display: flex; align-items: center; gap: 6px; }
.vat-ctl label { display: flex; align-items: center; gap: 5px; font-weight: 600; }
.vat-rate { width: 52px; padding: 5px 6px; border: 1px solid var(--line); border-radius: 7px; font-size: 13px; }
.vat-rate:disabled { opacity: .45; }
.vat-ctl .pct { margin-left: -3px; }

/* photos */
.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.photo { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--bg); }
.photo img { width: 100%; height: 100%; object-fit: cover; cursor: pointer; display: block; }
.photo .del { position: absolute; top: 4px; right: 4px; background: rgba(15,23,42,.7); color: #fff; border: none; border-radius: 999px; width: 22px; height: 22px; cursor: pointer; font-size: 13px; line-height: 1; }
.add-photo {
  display: flex; align-items: center; justify-content: center; aspect-ratio: 1;
  border: 2px dashed var(--line); border-radius: 10px; color: var(--muted); cursor: pointer; font-size: 26px;
}
.add-photo:hover { border-color: var(--primary); color: var(--primary); }
.add-photo input { display: none; }

.meta-line { color: var(--muted); font-size: 12.5px; margin: 4px 0 0; }

/* status picker in footer */
.status-select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; font-size: 13px; font-weight: 600; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); z-index: 60; display: flex; align-items: center; justify-content: center; padding: 30px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; z-index: 70; box-shadow: var(--shadow); opacity: 0; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

@media (max-width: 560px) {
  .topbar { flex-wrap: wrap; }
  .search { order: 3; flex-basis: 100%; }
  .li-head, .li-row { grid-template-columns: 1fr 38px 50px 56px 52px 20px; gap: 4px; }
  .li-row input, .li-row select { padding: 7px 4px; font-size: 12px; }
  #new-btn { font-size: 13px; padding: 9px 12px; }
}
