:root {
  --coral: #FF6B6B;
  --pink: #FF8FAB;
  --turquoise: #2EC4B6;
  --teal-light: #CBF3F0;
  --cream: #FFF9F0;
  --dark: #2D2D2D;
  --mid: #6b6b6b;
  --border: #e0d5cc;
  --gold: #FFB347;
  --gold-dark: #B8860B;
  --card: #ffffff;
  --danger: #e35555;
  --ok: #2EC4B6;
}

* { box-sizing: border-box; }

/* The hidden attribute must always win, even over elements below that set
   their own `display` (e.g. .modal-backdrop, .celebration, .badge-count) -
   otherwise those overlays render regardless of their hidden state. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--cream);
  color: var(--dark);
  font-family: 'Nunito', sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  background: radial-gradient(circle at 15% 10%, rgba(255,143,171,0.12) 0%, transparent 40%),
              radial-gradient(circle at 85% 60%, rgba(46,196,182,0.10) 0%, transparent 40%),
              var(--cream);
  background-attachment: fixed;
  padding-bottom: 90px;
  min-height: 100vh;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 16px 8px;
}

h1, h2, h3 { margin: 0 0 4px; font-family: 'Baloo 2', sans-serif; }
p { color: var(--mid); margin: 0; }

.app-header { text-align: center; margin-bottom: 14px; position: relative; }
.brand-title {
  font-family: 'Pacifico', cursive;
  font-size: 2.1rem;
  color: var(--coral);
  margin: 0;
  line-height: 1.2;
}
.brand-verse { font-family: 'Nunito', sans-serif; font-weight: 700; color: var(--mid); }
.tagline { font-size: 0.85rem; font-weight: 700; color: var(--turquoise); text-transform: uppercase; letter-spacing: 0.06em; }

.logout-link {
  display: block;
  margin: 6px auto 0;
  background: none;
  border: none;
  color: var(--mid);
  font-size: 0.75rem;
  text-decoration: underline;
  cursor: pointer;
  font-family: inherit;
}

/* Tab bar */
.tab-bar {
  display: flex;
  gap: 6px;
  background: white;
  border-radius: 16px;
  padding: 6px;
  border: 1.5px solid var(--border);
  margin-bottom: 16px;
  position: sticky;
  top: 8px;
  z-index: 20;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.tab-btn {
  border: none;
  background: transparent;
  padding: 10px 10px;
  border-radius: 12px;
  font-family: 'Nunito', sans-serif;
  font-weight: 800;
  font-size: 0.78rem;
  color: var(--mid);
  cursor: pointer;
  position: relative;
  flex: 1 0 auto;
  white-space: nowrap;
}
.tab-btn.active {
  background: linear-gradient(135deg, var(--coral), var(--pink));
  color: white;
}
.badge-count {
  display: inline-block;
  background: var(--gold-dark);
  color: white;
  font-size: 0.68rem;
  padding: 1px 6px;
  border-radius: 999px;
  margin-left: 4px;
}

/* View headers */
.view-header {
  display: flex;
  align-items: center;
  gap: 12px;
  border-radius: 20px;
  padding: 16px 18px;
  margin-bottom: 14px;
}
.view-header-icon { font-size: 1.6rem; }
.header-logo { width: 52px; height: 52px; object-fit: contain; display: block; }
.view-sub { font-size: 0.82rem; }
.hdr-templates { background: linear-gradient(120deg, #FFE8E8, #FFF9F0); }
.hdr-orders { background: linear-gradient(120deg, #FFE1EC, #FFF6D8); }
.hdr-pipeline { background: linear-gradient(120deg, #E3F7F5, #FFF9F0); }
.hdr-budget { background: linear-gradient(120deg, #FFF3D6, #FFF9F0); }
.hdr-invoices { background: linear-gradient(120deg, #F0E8FF, #FFF9F0); }
.hdr-leads { background: linear-gradient(120deg, #E0F0FF, #FFF9F0); }

.subtab-bar { display: flex; gap: 8px; margin-bottom: 14px; }
.subtab-btn {
  flex: 1;
  border: 1.5px solid var(--border);
  background: white;
  border-radius: 12px;
  padding: 9px;
  font-weight: 800;
  font-size: 0.82rem;
  color: var(--mid);
  cursor: pointer;
}
.subtab-btn.active { border-color: var(--turquoise); color: var(--turquoise); background: var(--teal-light); }

.filter-row { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.chip {
  padding: 7px 14px;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: white;
  font-weight: 700;
  font-size: 0.82rem;
  cursor: pointer;
  color: var(--mid);
}
.chip.active { background: var(--turquoise); border-color: var(--turquoise); color: white; }

/* Cards */
.card {
  background: var(--card);
  border-radius: 20px;
  padding: 20px;
  margin-bottom: 14px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.05);
  border: 1.5px solid var(--border);
  position: relative;
  overflow: hidden;
}
.card::before {
  content: '';
  position: absolute; top: 0; left: 0; right: 0; height: 4px;
  background: linear-gradient(90deg, var(--coral), var(--pink), var(--turquoise));
}
.stack { display: flex; flex-direction: column; gap: 0; }

.item-title { font-family: 'Baloo 2', sans-serif; font-size: 1.05rem; font-weight: 700; color: var(--dark); }
.item-sub { font-size: 0.82rem; color: var(--mid); margin-top: 2px; }
.item-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.item-meta { font-size: 0.8rem; color: var(--mid); margin-top: 8px; line-height: 1.6; }
.item-meta strong { color: var(--dark); }

.pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.pill-new { background: #FFE1EC; color: var(--coral); }
.pill-reviewed { background: var(--teal-light); color: var(--turquoise); }
.pill-archived { background: #eee; color: var(--mid); }
.pill-quoted { background: #FFF3D6; color: var(--gold-dark); }
.pill-in_progress { background: #E3E8FB; color: #5b5fc7; }
.pill-ready { background: var(--teal-light); color: var(--turquoise); }
.pill-delivered { background: #E1F7E1; color: #2e9e4f; }
.pill-paid { background: #E1F7E1; color: #2e9e4f; }
.pill-unpaid { background: #FFE8E8; color: var(--coral); }

.materials-table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 0.85rem; }
.materials-table th { text-align: left; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--mid); padding: 4px 6px; border-bottom: 1.5px solid var(--border); }
.materials-table td { padding: 6px; border-bottom: 1px solid #f0ece8; }
.materials-table input { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 5px 7px; font-family: inherit; font-size: 0.85rem; }
.materials-table .num-col { width: 90px; }
.cogs-line { display: flex; justify-content: space-between; margin-top: 10px; padding-top: 10px; border-top: 1.5px dashed var(--border); font-weight: 800; }
.cogs-line .margin { color: var(--turquoise); font-weight: 700; font-size: 0.85rem; }

.btn {
  font-family: 'Nunito', sans-serif;
  font-weight: 800;
  border: none;
  border-radius: 12px;
  padding: 11px 18px;
  cursor: pointer;
  font-size: 0.9rem;
}
.btn-primary { background: linear-gradient(135deg, var(--coral), var(--pink)); color: white; box-shadow: 0 4px 16px rgba(255,107,107,0.3); }
.btn-secondary { background: var(--teal-light); color: var(--turquoise); }
.btn-danger { background: #FFE8E8; color: var(--danger); }
.btn-ghost { background: transparent; color: var(--mid); border: 1.5px solid var(--border); }
.btn-add { width: 100%; background: white; border: 2px dashed var(--turquoise); color: var(--turquoise); margin-bottom: 20px; }
.btn-sm { padding: 6px 12px; font-size: 0.78rem; border-radius: 9px; }
.btn-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

.add-form { display: flex; flex-direction: column; gap: 12px; }
.add-form input, .add-form select, .add-form textarea {
  width: 100%;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: 12px;
  font-family: 'Nunito', sans-serif;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--dark);
  background: var(--cream);
  outline: none;
}
.add-form label { font-size: 0.78rem; font-weight: 800; color: var(--mid); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: -6px; }
.add-form textarea { min-height: 70px; resize: vertical; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.status-msg { text-align: center; font-size: 0.85rem; margin-top: 10px; min-height: 1em; color: var(--mid); }
.status-msg.error { color: var(--danger); font-weight: 700; }
.helper-note { font-size: 0.75rem; color: var(--mid); text-align: center; margin-top: 12px; line-height: 1.5; }

/* Table wrap (budget) */
.table-wrap { overflow-x: auto; background: white; border-radius: 16px; border: 1.5px solid var(--border); margin-bottom: 14px; }
.budget-table { width: 100%; border-collapse: collapse; font-size: 0.82rem; min-width: 720px; }
.budget-table th { text-align: left; padding: 10px 8px; background: var(--teal-light); color: var(--dark); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.03em; position: sticky; top: 0; }
.budget-table td { padding: 6px 8px; border-bottom: 1px solid #f0ece8; }
.budget-table input { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 5px 7px; font-family: inherit; font-size: 0.82rem; }
.budget-table textarea.wrap-field {
  width: 100%; min-width: 110px; border: 1px solid var(--border); border-radius: 8px; padding: 5px 7px;
  font-family: inherit; font-size: 0.82rem; resize: none; overflow: hidden; white-space: normal;
  word-break: break-word; line-height: 1.4; display: block;
}
.budget-table .outstanding-pos { color: var(--danger); font-weight: 800; }
.budget-table .outstanding-zero { color: var(--ok); font-weight: 800; }
.budget-table tfoot td { font-weight: 800; background: #FFF9F0; }

/* Dashboard stat grid */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 14px; }
.stat-card {
  background: white; border: 1.5px solid var(--border); border-radius: 16px; padding: 14px;
}
.stat-label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--mid); font-weight: 800; }
.stat-value { font-family: 'Baloo 2', sans-serif; font-size: 1.4rem; font-weight: 700; margin-top: 4px; }
.stat-value.positive { color: var(--ok); }
.stat-value.negative { color: var(--coral); }
.stat-card.wide { grid-column: 1 / -1; }
.stat-card.hero { background: linear-gradient(135deg, var(--coral), var(--pink)); border: none; }
.stat-card.hero .stat-label, .stat-card.hero .stat-value { color: white; }

.breakeven-row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #f0ece8; font-size: 0.88rem; }
.breakeven-row:last-child { border-bottom: none; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: flex-end;
  justify-content: center; z-index: 200; padding: 0;
}
@media (min-width: 640px) {
  .modal-backdrop { align-items: center; }
}
.modal-card {
  background: white; border-radius: 24px 24px 0 0; padding: 22px; width: 100%; max-width: 520px;
  max-height: 88vh; overflow-y: auto;
}
@media (min-width: 640px) {
  .modal-card { border-radius: 24px; }
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-header h3 { font-family: 'Baloo 2', sans-serif; color: var(--coral); }
.modal-close { background: none; border: none; font-size: 1.6rem; cursor: pointer; color: var(--mid); line-height: 1; }

/* Notification FAB */
.notif-fab {
  position: fixed; bottom: 20px; right: 20px; width: 52px; height: 52px; border-radius: 50%;
  background: linear-gradient(135deg, var(--coral), var(--pink)); color: white; display: flex;
  align-items: center; justify-content: center; font-size: 1.4rem; box-shadow: 0 6px 20px rgba(255,107,107,0.4);
  cursor: pointer; z-index: 30;
}
.notif-fab.subscribed { background: linear-gradient(135deg, var(--turquoise), #22a89f); }

/* Toast */
.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); background: var(--dark); color: white;
  padding: 10px 18px; border-radius: 999px; font-size: 0.85rem; font-weight: 700; z-index: 300;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}

/* Confetti celebration */
.celebration {
  position: fixed; inset: 0; z-index: 500; display: flex; align-items: center; justify-content: center;
  background: rgba(45,45,45,0.55);
}
#confetti-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.celebration-content { position: relative; text-align: center; background: white; padding: 36px 28px; border-radius: 28px; max-width: 340px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
.celebration-title {
  font-family: 'Pacifico', cursive; font-size: 2.4rem;
  background: linear-gradient(135deg, var(--coral), var(--gold), var(--turquoise));
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
  margin-bottom: 10px;
}
.celebration-sub { color: var(--mid); font-weight: 700; margin-bottom: 18px; }

.empty-state { text-align: center; padding: 30px 16px; color: var(--mid); font-weight: 600; font-size: 0.9rem; }

.details-toggle { background: none; border: none; color: var(--turquoise); font-weight: 800; font-size: 0.8rem; cursor: pointer; padding: 0; margin-top: 8px; }

/* Invoice */
.invoice-overlay {
  position: fixed; inset: 0; z-index: 600; background: #ECE9E4; overflow-y: auto; padding: 20px 16px 60px;
}
.invoice-toolbar { display: flex; gap: 10px; justify-content: center; max-width: 720px; margin: 0 auto 16px; }
.invoice-paper {
  background: white; max-width: 720px; margin: 0 auto; padding: 36px 32px; border-radius: 10px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.12); font-family: 'Nunito', sans-serif; color: var(--dark);
}
.invoice-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; border-bottom: 3px solid var(--coral); padding-bottom: 18px; margin-bottom: 22px; }
.invoice-head img { width: 110px; height: 110px; object-fit: contain; flex-shrink: 0; }
.invoice-brand-name { font-family: 'Pacifico', cursive; font-size: 1.5rem; color: var(--coral); }
.invoice-brand-sub { font-size: 0.78rem; color: var(--mid); line-height: 1.5; margin-top: 2px; }
.invoice-meta { text-align: right; font-size: 0.85rem; color: var(--mid); line-height: 1.6; }
.invoice-meta strong { color: var(--dark); font-family: 'Baloo 2', sans-serif; font-size: 1.05rem; display: block; }
.invoice-section-title { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--mid); font-weight: 800; margin-bottom: 6px; }
.invoice-bill-to { margin-bottom: 24px; font-size: 0.92rem; line-height: 1.6; }
.invoice-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
.invoice-table th { text-align: left; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--mid); padding: 8px 4px; border-bottom: 2px solid var(--border); }
.invoice-table td { padding: 10px 4px; border-bottom: 1px solid #f0ece8; font-size: 0.92rem; }
.invoice-table .num { text-align: right; white-space: nowrap; }
.invoice-totals { margin-left: auto; max-width: 300px; }
.invoice-totals-row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 0.92rem; }
.invoice-totals-row.grand { border-top: 2px solid var(--dark); margin-top: 6px; padding-top: 10px; font-weight: 800; font-size: 1.05rem; }
.invoice-status { display: inline-block; margin-top: 16px; padding: 8px 16px; border-radius: 999px; font-weight: 800; font-size: 0.85rem; }
.invoice-status.paid { background: #E1F7E1; color: #2e9e4f; }
.invoice-status.unpaid { background: #FFF3D6; color: var(--gold-dark); }
.invoice-payment-note { margin-top: 18px; padding: 14px 16px; background: #FFF9F0; border-left: 3px solid var(--gold); border-radius: 0 8px 8px 0; font-size: 0.85rem; line-height: 1.6; }
.invoice-footer { text-align: center; margin-top: 28px; padding-top: 18px; border-top: 1.5px dashed var(--border); font-size: 0.85rem; color: var(--mid); }

@media print {
  html, body { background: none !important; }
  /* Fully remove everything except the invoice from the render tree (rather
     than just hiding it with visibility) so nothing with `position: fixed`
     — the header, tab bar, modals, toast, FAB — can leak onto the printed
     page or repeat across pages, which is what caused blank/black extra
     pages and clipped content on iOS Safari's print/"Save to Files" path. */
  body > *:not(#invoice-view) { display: none !important; }
  #invoice-view {
    position: static !important; inset: auto !important; z-index: auto !important;
    background: none !important; padding: 0 !important; overflow: visible !important;
  }
  .invoice-paper { position: static !important; box-shadow: none !important; margin: 0 !important; }
  .no-print { display: none !important; }
}
