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

:root {
  --primary: #6a5acd;
  --primary-light: #ede9ff;
  --surface: #ffffff;
  --bg: #f0f2f7;
  --border: #e2e4eb;
  --text: #1a1d2e;
  --muted: #6b7080;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow: 0 4px 16px rgba(0,0,0,0.1);
  --radius: 10px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ─── DESKTOP LAYOUT ─────────────────────────── */
@media (min-width: 851px) {
  body { display: flex; height: 100vh; overflow: hidden; }
  .container { display: flex; width: 100%; height: 100%; }

  .sidebar {
    width: 400px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    padding: 2rem;
  }

  .preview-pane {
    flex: 1;
    overflow-y: auto;
    background: var(--bg);
    padding: 2rem;
    display: flex;
    justify-content: center;
    align-items: flex-start;
  }

  /* Mobile-only elements hidden on desktop */
  .mobile-top-bar,
  #preview-btn-mobile,
  .mobile-exit-bar { display: none !important; }
}

/* ─── MOBILE LAYOUT ──────────────────────────── */
@media (max-width: 850px) {
  body { display: block; }
  .container { display: block; }

  /* Mobile top bar */
  .mobile-top-bar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0.9rem 1.2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: var(--shadow-sm);
  }
  .mobile-top-bar h1 {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--primary);
  }

  /* Sidebar: full page, scrollable */
  .sidebar {
    background: var(--surface);
    padding: 1.2rem;
    padding-bottom: 6rem; /* room for sticky button */
  }
  .sidebar h1 { display: none; } /* replaced by top bar */

  /* Preview pane: hidden by default on mobile */
  .preview-pane {
    display: none;
    background: var(--bg);
    padding: 0;
  }

  /* When preview is active */
  body.preview-active .sidebar { display: none; }
  body.preview-active .preview-pane {
    display: block;
    min-height: 100vh;
  }
  body.preview-active .mobile-exit-bar { display: flex; }
  body.preview-active .mobile-top-bar { display: none; }

  /* Invoice preview: full width on mobile */
  .invoice-preview {
    width: 100% !important;
    min-height: auto !important;
    margin: 0 !important;
    box-shadow: none !important;
    border-radius: 0 !important;
    padding: 1.5rem !important;
    font-size: 0.88rem !important;
  }

  /* Sticky preview button at bottom of editor */
  #preview-btn-mobile {
    display: block;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 99;
    margin: 0;
    border-radius: 0;
    padding: 1rem;
    font-size: 1rem;
    background: var(--primary);
    color: white;
    border: none;
    cursor: pointer;
    font-weight: 700;
    font-family: 'DM Sans', sans-serif;
    letter-spacing: 0.3px;
  }

  /* Sticky exit bar when previewing */
  .mobile-exit-bar {
    display: none;
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0.8rem 1rem;
    gap: 0.6rem;
    align-items: center;
    box-shadow: var(--shadow-sm);
  }
  .mobile-exit-bar .btn-back {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 0.65rem 1rem;
    border-radius: var(--radius);
    font-weight: 600;
    font-family: 'DM Sans', sans-serif;
    font-size: 0.9rem;
    cursor: pointer;
    flex-shrink: 0;
  }
  .mobile-exit-bar .btn-pdf {
    flex: 1;
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.65rem 1rem;
    border-radius: var(--radius);
    font-weight: 700;
    font-family: 'DM Sans', sans-serif;
    font-size: 0.9rem;
    cursor: pointer;
    text-align: center;
  }
}

/* ─── SIDEBAR COMPONENTS ─────────────────────── */
.sidebar h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 1.5rem;
}

.section-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin-bottom: 0.7rem;
  margin-top: 1.4rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--border);
}
.section-label:first-of-type { margin-top: 0; }

.form-group { margin-bottom: 0.9rem; }
.form-row { display: flex; gap: 0.8rem; }
.form-row .form-group { flex: 1; margin-bottom: 0; }

label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

input[type="text"],
input[type="date"],
input[type="number"],
textarea,
select {
  width: 100%;
  padding: 0.65rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.93rem;
  font-family: 'DM Sans', sans-serif;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}
textarea { resize: vertical; line-height: 1.5; }

input[type="color"] {
  padding: 0.2rem;
  height: 42px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid var(--border);
}

.currency-pills {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.3rem;
}
.currency-pills label {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 0.5rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--muted);
  transition: all 0.15s;
  margin: 0;
}
.currency-pills input[type="radio"] { display: none; }
.currency-pills input[type="radio"]:checked + span {
  /* handled via JS class */
}
.currency-pills label.active {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary);
}

/* Items table in editor */
.items-editor { width: 100%; border-collapse: collapse; }
.items-editor th {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  padding: 0 0 0.5rem;
  text-align: left;
}
.items-editor td { padding: 0.3rem 0; vertical-align: middle; }
.items-editor input[type="text"],
.items-editor input[type="number"] {
  padding: 0.55rem 0.7rem;
  font-size: 0.9rem;
}
.items-editor td:last-child { width: 32px; padding-left: 0.4rem; }

.btn-remove {
  background: #fee2e2;
  color: #dc2626;
  border: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}
.btn-remove:hover { background: #fca5a5; }

.btn {
  display: block;
  width: 100%;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  border: none;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
  margin-top: 0.6rem;
}
.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: #5a4bbf; }
.btn-ghost {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--border); }

/* ─── INVOICE PREVIEW ────────────────────────── */
.invoice-preview {
  width: 210mm;
  min-height: 297mm;
  background: white;
  padding: 2.5rem;
  box-shadow: var(--shadow);
  border-radius: 6px;
  font-family: 'DM Sans', sans-serif;
  color: #1a1d2e;
  flex-shrink: 0;
  position: relative;
}

.inv-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 2.2rem;
  gap: 1rem;
  flex-wrap: wrap;
}
.inv-company h2 {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 0.4rem;
}
.inv-company p { font-size: 0.88rem; color: #555; line-height: 1.6; margin: 0; }

.inv-title-block { text-align: right; }
.inv-title-block h2 {
  font-size: 2rem;
  font-weight: 800;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-bottom: 0.6rem;
}
.inv-meta p { font-size: 0.88rem; color: #444; margin: 0.25rem 0; }

.inv-divider {
  height: 3px;
  background: linear-gradient(90deg, var(--primary) 0%, transparent 100%);
  border-radius: 2px;
  margin-bottom: 1.8rem;
}

.inv-bill-section {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.8rem;
  flex-wrap: wrap;
}
.inv-bill h3 {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--primary);
  margin-bottom: 0.5rem;
}
.inv-bill p { font-size: 0.88rem; color: #444; margin: 0.15rem 0; line-height: 1.5; }

.inv-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1.5rem;
}
.inv-table thead tr {
  background: var(--primary);
  color: white;
}
.inv-table th {
  padding: 0.75rem 1rem;
  font-size: 0.82rem;
  font-weight: 600;
  text-align: left;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.inv-table th:last-child { text-align: right; }
.inv-table td {
  padding: 0.85rem 1rem;
  font-size: 0.9rem;
  color: #333;
  border-bottom: 1px solid #f0f2f7;
}
.inv-table td:last-child { text-align: right; font-family: 'DM Mono', monospace; }
.inv-table tbody tr:hover { background: #fafbff; }

.inv-total-row {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 2rem;
}
.inv-total-box {
  background: var(--primary-light);
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  padding: 0.8rem 1.5rem;
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}
.inv-total-box .label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.inv-total-box .amount {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--primary);
  font-family: 'DM Mono', monospace;
}

.inv-payment {
  background: #fafbff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem 1.5rem;
  margin-top: 1rem;
}
.inv-payment h4 {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--primary);
  margin-bottom: 0.8rem;
}
.inv-payment-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem 1.5rem;
}
.inv-payment-grid p {
  font-size: 0.85rem;
  color: #444;
  margin: 0;
}
.inv-payment-grid p strong { color: var(--text); }
