:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --ink: #0f172a;
  --muted: #475569;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --accent: #059669;
  --warn-bg: #fffbeb;
  --warn-border: #fde68a;
  --warn-ink: #92400e;
  --radius: 12px;
  --shadow: 0 6px 24px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.55;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-badge {
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  font-size: 14px;
  padding: 6px 10px;
  border-radius: 8px;
  letter-spacing: 0.5px;
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

.brand p {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-outline {
  background: #fff;
  color: var(--ink);
  border-color: var(--border);
}
.btn-outline:hover {
  background: #f8fafc;
}

.btn-pro {
  background: linear-gradient(135deg, #f59e0b, #ea580c);
  color: #fff;
}
.btn-pro:hover {
  filter: brightness(1.05);
}

.btn-sm {
  padding: 6px 10px;
  font-size: 12px;
}

.btn-danger {
  background: #fef2f2;
  color: #dc2626;
  border-color: #fecaca;
}

.container {
  max-width: 1420px;
  margin: 20px auto;
  padding: 0 20px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(360px, 470px) minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}

.editor-column,
.preview-column {
  min-width: 0;
}

.workspace-nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 8px;
  margin-bottom: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.workspace-nav a {
  padding: 8px 12px;
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
}

.workspace-nav a:not(.btn) {
  color: var(--muted);
  border-radius: 8px;
}

.workspace-nav a:not(.btn):hover {
  background: var(--bg);
  color: var(--ink);
}

.workspace-nav .btn {
  margin-left: auto;
}

.preview-toolbar,
.preview-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.preview-toolbar {
  justify-content: space-between;
  margin-bottom: 12px;
}

.preview-toolbar h2 {
  margin: 0;
  font-size: 16px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.panel-title {
  margin: 0 0 12px;
  font-size: 15px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: #f1f5f9;
  padding: 4px;
  border-radius: 10px;
  margin-bottom: 14px;
}

.tab-btn {
  border: none;
  background: transparent;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.tab-btn.active {
  background: #fff;
  color: var(--primary);
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.08);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}

.form-group.full {
  grid-column: 1 / -1;
}

label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

input[type="text"],
input[type="number"],
input[type="date"],
select,
textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font-size: 13px;
  font-family: inherit;
  color: var(--ink);
  background: #fff;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--ink);
  cursor: pointer;
  margin: 6px 0;
}

.template-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.chip {
  background: #eff6ff;
  color: #1d4ed8;
  border: 1px solid #bfdbfe;
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.chip:hover {
  background: #dbeafe;
}

/* Reverse Calculator Box */
.reverse-box {
  background: linear-gradient(135deg, #eff6ff, #ecfdf5);
  border: 1px solid #bfdbfe;
  border-radius: 10px;
  padding: 12px;
  margin-top: 10px;
}

.reverse-box-title {
  font-size: 13px;
  font-weight: 700;
  color: #1e3a8a;
  cursor: pointer;
}

.reverse-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.reverse-row button {
  flex: none;
}

.reverse-result {
  margin-top: 8px;
  font-size: 12px;
  color: #0f172a;
  background: rgba(255, 255, 255, 0.85);
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid #dbeafe;
}

.tip-banner {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-ink);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 12px;
  margin-bottom: 12px;
}

/* Line Items Editor */
.item-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 8px;
  background: #f8fafc;
}

.item-row-top {
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
}

.item-row-bottom {
  display: grid;
  grid-template-columns: minmax(0, 0.8fr) minmax(0, 0.8fr) minmax(0, 1.5fr) minmax(0, 1.2fr);
  gap: 6px;
  align-items: end;
}

.item-row-bottom label,
.item-subtotal {
  display: grid;
  gap: 4px;
}

.item-subtotal {
  padding: 0 2px 4px;
  color: var(--muted);
  font-size: 12px;
  text-align: right;
  overflow-wrap: anywhere;
}

.item-subtotal strong {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

/* Live KPI Summary Strip */
.kpi-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.kpi-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.kpi-label {
  font-size: 12px;
  color: var(--muted);
}

.kpi-value {
  font-size: 20px;
  font-weight: 800;
  color: var(--ink);
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.kpi-value.accent {
  color: #047857;
}

.kpi-card:last-child {
  background: #ecfdf5;
  border-color: #a7f3d0;
}

.kpi-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

/* A4 Paper Preview */
.paper-wrap {
  overflow-x: auto;
  padding-bottom: 12px;
}

.a4-paper {
  background: #fff;
  width: 100%;
  max-width: 820px;
  min-height: 1040px;
  margin: 0 auto;
  padding: 40px 44px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  box-shadow: 0 12px 36px rgba(15, 23, 42, 0.08);
  position: relative;
  display: flex;
  flex-direction: column;
}

.doc-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  border-bottom: 3px solid var(--ink);
  padding-bottom: 16px;
  margin-bottom: 20px;
  gap: 16px;
}

.doc-title-block h2 {
  margin: 0;
  font-size: 26px;
  letter-spacing: 2px;
  color: var(--ink);
}

.doc-title-en {
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.doc-meta {
  text-align: right;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.7;
}

.doc-meta strong {
  color: var(--ink);
}

.parties-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 22px;
  font-size: 13px;
}

.party-box {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 12px 14px;
}

.party-box h4 {
  margin: 0 0 6px;
  font-size: 12px;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.party-line {
  margin: 2px 0;
  color: var(--ink);
}

.doc-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 18px;
  font-size: 13px;
}

.doc-table th {
  background: #0f172a;
  color: #fff;
  text-align: left;
  padding: 9px 10px;
  font-weight: 600;
}

.doc-table th.num,
.doc-table td.num {
  text-align: right;
  white-space: nowrap;
}

.doc-table td {
  padding: 9px 10px;
  border-bottom: 1px solid #e2e8f0;
  vertical-align: top;
}

.item-desc-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.totals-section {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
  align-items: flex-start;
}

.bank-box {
  flex: 1;
  font-size: 12px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 12px 14px;
}

.bank-box h4 {
  margin: 0 0 6px;
  font-size: 12px;
  color: var(--ink);
}

.totals-table {
  width: 300px;
  border-collapse: collapse;
  font-size: 13px;
}

.totals-table td {
  padding: 6px 8px;
  border-bottom: 1px solid #e2e8f0;
}

.totals-table td:last-child {
  text-align: right;
  font-weight: 600;
}

.totals-table tr.grand-total td {
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
  border-top: 2px solid var(--ink);
  border-bottom: 2px solid var(--ink);
  padding: 9px 8px;
  background: #f8fafc;
}

.totals-table tr.deduct td {
  color: #b91c1c;
}

.terms-box {
  font-size: 12px;
  color: #334155;
  border-top: 1px solid #e2e8f0;
  padding-top: 12px;
  margin-bottom: 18px;
  white-space: pre-line;
}

.id-paste-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-bottom: 18px;
}

.id-paste-box {
  border: 1.5px dashed #94a3b8;
  border-radius: 8px;
  height: 112px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #64748b;
  background: #fcfdff;
}

.sign-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin-top: auto;
  padding-top: 16px;
}

.sign-box {
  border-top: 1px solid #94a3b8;
  padding-top: 8px;
  font-size: 12px;
  color: var(--muted);
}

.watermark-footer {
  margin-top: 16px;
  text-align: center;
  font-size: 11px;
  color: #94a3b8;
  border-top: 1px dashed #e2e8f0;
  padding-top: 8px;
}

/* SEO FAQ Section */
.seo-section {
  max-width: 1100px;
  margin: 36px auto 48px;
  padding: 0 20px;
}

.faq-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
  margin-top: 14px;
}

.faq-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
}

.faq-card h3 {
  margin: 0 0 6px;
  font-size: 14px;
  color: var(--ink);
}

.faq-card p {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal-backdrop.open {
  display: flex;
}

.modal-card {
  background: #fff;
  border-radius: 14px;
  max-width: 460px;
  width: 100%;
  padding: 24px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.25);
}

.modal-card h3 {
  margin: 0 0 8px;
  font-size: 18px;
}

.pro-list {
  margin: 12px 0 16px;
  padding-left: 18px;
  font-size: 13px;
  color: #334155;
}

.pro-list li {
  margin-bottom: 6px;
}

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: #0f172a;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 200;
}

.toast.show {
  opacity: 1;
}

@media screen and (max-width: 1024px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }

  .workspace-nav {
    position: sticky;
    top: 0;
    z-index: 10;
  }

  #document-settings,
  #quote-items,
  #quote-parties,
  #quote-preview {
    scroll-margin-top: 72px;
  }
}

@media screen and (max-width: 600px) {
  .topbar {
    padding: 12px;
    gap: 8px;
  }

  .container {
    margin: 12px auto;
    padding: 0 12px;
  }

  .workspace-nav {
    padding: 5px;
    gap: 3px;
  }

  .workspace-nav a {
    padding: 8px 6px;
    font-size: 12px;
  }

  .panel {
    padding: 14px;
  }

  .form-grid {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
  }

  input[type="text"],
  input[type="number"],
  input[type="date"],
  select,
  textarea {
    font-size: 16px;
    min-width: 0;
  }

  .item-row-bottom {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1.7fr);
  }

  .item-subtotal {
    grid-column: 1 / -1;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 8px;
    padding: 2px 0 0;
  }

  .preview-actions {
    width: 100%;
  }

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

  .kpi-strip,
  .parties-grid {
    gap: 6px;
  }

  .kpi-card {
    padding: 8px;
  }

  .kpi-value {
    font-size: 15px;
  }

  .a4-paper {
    padding: 18px 12px;
    min-height: 0;
    overflow-wrap: anywhere;
  }

  .doc-header {
    flex-wrap: wrap;
    gap: 8px;
  }

  .doc-title-block h2 {
    font-size: 21px;
  }

  .doc-meta {
    text-align: left;
  }

  .party-box {
    padding: 8px;
  }

  .doc-table {
    table-layout: fixed;
    font-size: 11px;
  }

  .doc-table th,
  .doc-table td {
    padding: 6px 3px;
  }

  .doc-table th.num,
  .doc-table td.num {
    white-space: normal;
  }

  .totals-section {
    flex-direction: column-reverse;
    gap: 12px;
  }

  .bank-box,
  .totals-table {
    width: 100%;
  }
}

/* Print / Export A4 PDF */
@media print {
  @page {
    size: A4 portrait;
    margin: 10mm;
  }
  body {
    background: #fff !important;
  }
  .topbar,
  .editor-column,
  .kpi-strip,
  .seo-section,
  .modal-backdrop,
  .toast,
  .no-print {
    display: none !important;
  }
  .container,
  .workspace,
  .paper-wrap {
    margin: 0 !important;
    padding: 0 !important;
    display: block !important;
    max-width: none !important;
    overflow: visible !important;
  }
  .a4-paper {
    border: none !important;
    box-shadow: none !important;
    margin: 0 !important;
    padding: 8mm 10mm !important;
    max-width: none !important;
    min-height: 270mm !important;
  }
}
