/* ============================================================
   DESIGN SYSTEM — matches review-response-automator exactly
   ============================================================ */
:root {
  --bg: #020617;
  --surface: rgba(30, 41, 59, 0.7);
  --surface-solid: #1e293b;
  --border: rgba(148, 163, 184, 0.1);
  --border-hover: rgba(148, 163, 184, 0.25);
  --ink: #f8fafc;
  --muted: #94a3b8;
  --brand: #22d3ee;
  --brand-glow: rgba(34, 211, 238, 0.5);
  --brand-deep: #0891b2;
  --brand-tint: rgba(34, 211, 238, 0.08);
  --accent: #a78bfa;
  --radius: 12px;
  --radius-sm: 6px;
  --font-head: "Outfit", sans-serif;
  --font-body: "DM Sans", sans-serif;
  --font-mono: "JetBrains Mono", "Courier New", monospace;
  --topbar-h: 64px;
  color-scheme: dark;
}

/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-font-smoothing: antialiased; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  line-height: 1.6;
  min-height: 100vh;
  overflow: hidden;
}

h1, h2, h3, h4 { font-family: var(--font-head); line-height: 1.2; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
button { cursor: pointer; font-family: var(--font-body); }
input, textarea, select { font-family: var(--font-body); }

/* ============================================================
   TOPBAR
   ============================================================ */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  background: rgba(2, 6, 23, 0.9);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  display: flex;
  align-items: center;
}

.container {
  width: 100%;
  max-width: 1600px;
  margin: 0 auto;
  padding: 0 1.5rem;
}

.nav { display: flex; align-items: center; justify-content: space-between; }

.logo {
  font-family: var(--font-head);
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--ink);
  text-decoration: none;
}
.logo:hover { color: var(--brand); text-decoration: none; }

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.2rem;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 0.9rem;
  border: none;
  transition: opacity 0.15s, transform 0.1s, box-shadow 0.15s;
  white-space: nowrap;
  cursor: pointer;
}
.btn:hover { opacity: 0.88; transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.4; pointer-events: none; }

.btn-primary { background: var(--brand); color: #020617; font-weight: 700; }
.btn-primary:hover { box-shadow: 0 0 16px var(--brand-glow); }

.btn-secondary { background: var(--surface-solid); color: var(--ink); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--brand); }

.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--ink); border-color: var(--border-hover); }

.btn-sm { padding: 0.35rem 0.8rem; font-size: 0.8rem; }

/* ============================================================
   BUILDER LAYOUT
   ============================================================ */
.builder-layout {
  display: grid;
  grid-template-columns: 460px 1fr;
  height: calc(100vh - var(--topbar-h));
  margin-top: var(--topbar-h);
  overflow: hidden;
}

/* ============================================================
   FORM PANEL (left)
   ============================================================ */
.form-panel {
  background: rgba(15, 23, 42, 0.8);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.form-panel::-webkit-scrollbar { width: 4px; }
.form-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.form-section {
  padding: 1.25rem 1.25rem 0;
}
.form-section:last-child { padding-bottom: 1.5rem; }

/* Doc type toggle */
.doc-type-toggle {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
}

.doc-type-btn {
  flex: 1;
  padding: 0.65rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
  font-family: var(--font-head);
  transition: all 0.15s;
}
.doc-type-btn:hover { border-color: var(--brand); color: var(--ink); }
.doc-type-btn.active {
  background: var(--brand-tint);
  border-color: var(--brand);
  color: var(--brand);
}

/* Section label */
.section-label {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.75rem;
}

/* Field group */
.field-group {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.field-label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.form-row {
  display: flex;
  gap: 0.6rem;
}
.form-row > * { flex: 1; }

/* Inputs */
.field-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--ink);
  font-size: 0.875rem;
  padding: 0.5rem 0.75rem;
  outline: none;
  transition: border-color 0.15s;
}
.field-input:focus { border-color: var(--brand); }
.field-input::placeholder { color: var(--muted); }
.field-input[type="date"]::-webkit-calendar-picker-indicator { filter: invert(0.5); }

.field-textarea { resize: vertical; min-height: 64px; line-height: 1.5; }

/* Line item rows */
.line-item-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.85rem;
  margin-bottom: 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  position: relative;
}

.line-item-top {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
}

.service-select {
  flex: 1;
  background: rgba(2, 6, 23, 0.5);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--ink);
  font-size: 0.85rem;
  padding: 0.45rem 0.6rem;
  outline: none;
  transition: border-color 0.15s;
  cursor: pointer;
}
.service-select:focus { border-color: var(--brand); }
.service-select option { background: #1e293b; }

.suggest-btn {
  flex-shrink: 0;
  padding: 0.45rem 0.75rem;
  background: var(--brand-tint);
  border: 1px solid rgba(34, 211, 238, 0.3);
  border-radius: var(--radius-sm);
  color: var(--brand);
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  transition: all 0.15s;
}
.suggest-btn:hover { background: rgba(34, 211, 238, 0.15); }
.suggest-btn:disabled { opacity: 0.5; pointer-events: none; }

.remove-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.2rem;
  transition: color 0.1s;
}
.remove-btn:hover { color: #f87171; }

.line-item-desc {
  width: 100%;
  background: rgba(2, 6, 23, 0.3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--ink);
  font-size: 0.85rem;
  padding: 0.45rem 0.6rem;
  resize: none;
  outline: none;
  min-height: 44px;
  line-height: 1.5;
  transition: border-color 0.15s;
}
.line-item-desc:focus { border-color: var(--brand); }
.line-item-desc::placeholder { color: var(--muted); }

.line-item-numbers {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.num-input {
  width: 70px;
  background: rgba(2, 6, 23, 0.3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--ink);
  font-size: 0.85rem;
  padding: 0.4rem 0.5rem;
  outline: none;
  text-align: right;
  transition: border-color 0.15s;
}
.num-input:focus { border-color: var(--brand); }

.unit-input {
  width: 72px;
  background: rgba(2, 6, 23, 0.3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-size: 0.8rem;
  padding: 0.4rem 0.5rem;
  outline: none;
  transition: border-color 0.15s;
}
.unit-input:focus { border-color: var(--brand); color: var(--ink); }

.line-total {
  margin-left: auto;
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--ink);
  min-width: 70px;
  text-align: right;
}

.num-label {
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
}

/* Add item button */
.add-item-btn {
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.55rem;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-size: 0.875rem;
  transition: all 0.15s;
}
.add-item-btn:hover { border-color: var(--brand); color: var(--brand); }

/* Totals block */
.totals-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.85rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.totals-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  color: var(--muted);
}

.total-row {
  color: var(--ink);
  font-weight: 700;
  font-size: 1rem;
  border-top: 1px solid var(--border);
  padding-top: 0.4rem;
  margin-top: 0.2rem;
}

/* Action row */
.action-row {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.generate-btn { flex: 1; justify-content: center; }

/* Error */
.error-msg {
  background: rgba(248, 113, 113, 0.1);
  border: 1px solid rgba(248, 113, 113, 0.3);
  color: #f87171;
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.85rem;
  font-size: 0.85rem;
}

/* Spinner */
.spinner {
  display: inline-block;
  width: 13px; height: 13px;
  border: 2px solid rgba(2,6,23,0.3);
  border-top-color: #020617;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================
   PREVIEW PANEL (right)
   ============================================================ */
.preview-panel {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  background: rgba(2, 6, 23, 0.4);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.preview-panel::-webkit-scrollbar { width: 4px; }
.preview-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.preview-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 3rem;
}

.preview-doc-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 1.5rem;
  gap: 1rem;
}

.preview-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.preview-label {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

/* The actual document area — monospace, editable */
.preview-doc {
  flex: 1;
  width: 100%;
  min-height: 480px;
  background: #0f172a;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: #e2e8f0;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.7;
  padding: 1.75rem 2rem;
  resize: none;
  outline: none;
  white-space: pre;
  overflow-x: auto;
  transition: border-color 0.15s;
}
.preview-doc:focus { border-color: var(--brand); }

/* ============================================================
   PRINT STYLES
   ============================================================ */

/* Print-only element — hidden in screen mode */
#print-area {
  display: none;
}

@media print {
  /* Hide everything except the dedicated print area */
  body > * { display: none !important; }
  #print-area { display: block !important; }

  #print-area {
    font-family: "Courier New", monospace;
    font-size: 11pt;
    line-height: 1.6;
    color: black;
    background: white;
    white-space: pre-wrap;
    word-break: break-word;
    margin: 0;
    padding: 0;
  }
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 900px) {
  body { overflow: auto; }

  .builder-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    height: auto;
    overflow: visible;
  }

  .form-panel, .preview-panel { overflow: visible; }
  .preview-panel { border-top: 1px solid var(--border); }
  .preview-doc { min-height: 300px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
