:root {
  --ink: #181713;
  --muted: #736d63;
  --soft: #f5f1e9;
  --paper: #fffdf8;
  --line: #ddd2bf;
  --gold: #b08a4a;
  --gold-dark: #7d5e28;
  --charcoal: #171612;
  --danger: #a83232;
  --success: #2d6b4f;
  --shadow: 0 22px 60px rgba(19, 17, 12, 0.16);
  --radius: 8px;
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", Arial, sans-serif;
  color: var(--ink);
  background:
    linear-gradient(120deg, rgba(176, 138, 74, 0.12), transparent 34%),
    linear-gradient(180deg, #fbf7ef 0%, #eee5d7 100%);
}

html,
body {
  max-width: 100%;
  overflow-x: hidden;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 18px 28px;
  color: #f8f2e7;
  background: linear-gradient(120deg, #11100d, #29241b);
  border-bottom: 1px solid rgba(176, 138, 74, 0.55);
  box-shadow: 0 12px 30px rgba(17, 16, 13, 0.22);
}

.topbar h1,
.preview-toolbar h2 {
  margin: 2px 0 0;
  font-size: 24px;
  letter-spacing: 0;
}

.eyebrow {
  margin: 0;
  font-size: 11px;
  line-height: 1.4;
  letter-spacing: 0.14em;
  color: var(--gold);
  text-transform: uppercase;
}

.topbar-actions,
.preview-actions,
.inline-control,
.history-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

.mobile-view-switch,
.mobile-bottom-actions {
  display: none;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(390px, 470px) minmax(560px, 1fr);
  gap: 24px;
  padding: 24px;
}

.editor-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: calc(100vh - 112px);
  overflow: auto;
  padding-right: 4px;
}

.panel-section,
.preview-toolbar {
  background: rgba(255, 253, 248, 0.92);
  border: 1px solid rgba(176, 138, 74, 0.2);
  border-radius: var(--radius);
  box-shadow: 0 12px 38px rgba(19, 17, 12, 0.08);
}

.panel-section {
  padding: 16px;
}

.panel-section.compact {
  padding-bottom: 12px;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
}

.section-heading h2 {
  margin: 0;
  font-size: 16px;
}

.field-grid {
  display: grid;
  gap: 10px;
}

.field-grid.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.field-grid.four {
  grid-template-columns: 0.9fr 1fr 1fr auto;
  align-items: end;
}

.field-grid.item-controls {
  grid-template-columns: minmax(70px, 0.9fr) minmax(88px, 1fr) minmax(88px, 1fr) auto auto;
  align-items: end;
}

.field,
.field-label {
  display: grid;
  gap: 6px;
}

.field,
.check-field {
  margin-bottom: 10px;
}

.helper-copy {
  margin: -2px 0 12px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
}

.field span,
.field-label,
.check-field span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  min-height: 39px;
  padding: 9px 10px;
  color: var(--ink);
  background: #fffaf1;
  border: 1px solid #d8cbb6;
  border-radius: 6px;
  outline: none;
  transition: border-color 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

textarea {
  resize: vertical;
  line-height: 1.55;
}

input:focus,
select:focus,
textarea:focus {
  background: #fffdf8;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(176, 138, 74, 0.16);
}

.primary-button,
.secondary-button,
.icon-button {
  min-height: 38px;
  border-radius: 6px;
  font-weight: 700;
  white-space: nowrap;
}

.primary-button {
  padding: 0 16px;
  color: #fffaf0;
  background: linear-gradient(135deg, #a67b36, #6f501f);
}

.secondary-button {
  padding: 0 14px;
  color: var(--charcoal);
  background: #eee4d4;
  border: 1px solid #d5c5aa;
}

.icon-button {
  display: grid;
  width: 38px;
  place-items: center;
  color: var(--charcoal);
  background: #eee4d4;
  border: 1px solid #d5c5aa;
  font-size: 20px;
}

.icon-button.danger {
  color: #fff;
  background: var(--danger);
  border-color: var(--danger);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 0 10px;
  border-radius: 999px;
  color: var(--gold-dark);
  background: #f4e8d3;
  font-size: 12px;
  font-weight: 800;
}

.status-pill.online {
  color: var(--success);
  background: #e3f0e7;
}

.status-pill.error {
  color: var(--danger);
  background: #f7dddd;
}

.history-tools {
  margin-top: 12px;
}

.history-list {
  display: grid;
  gap: 8px;
  max-height: 210px;
  margin-top: 10px;
  overflow: auto;
}

.history-card {
  width: 100%;
  padding: 10px;
  text-align: left;
  background: #fffaf1;
  border: 1px solid #dfd1bb;
  border-radius: 6px;
}

.history-card strong,
.history-card span {
  display: block;
}

.history-card strong {
  margin-bottom: 3px;
  font-size: 13px;
}

.history-card span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.items-editor {
  display: grid;
  gap: 10px;
}

.item-card {
  padding: 12px;
  background: #fbf4e8;
  border: 1px solid #dfd1bb;
  border-radius: var(--radius);
}

.item-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.item-index {
  color: var(--gold-dark);
  font-size: 13px;
}

.check-field {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 39px;
  padding-bottom: 1px;
}

.check-field input {
  width: 18px;
  min-height: 18px;
  accent-color: var(--gold-dark);
}

.discount-check span {
  color: var(--danger);
}

.preview-area {
  min-width: 0;
}

.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
  padding: 14px 16px;
}

.quote-page {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(100%, 794px);
  min-height: 1123px;
  margin: 0 auto 40px;
  padding: 58px 58px 44px;
  overflow: hidden;
  background: #fff;
  border: 1px solid rgba(27, 25, 21, 0.08);
  box-shadow: 0 22px 54px rgba(28, 24, 18, 0.14);
}

.quote-page::before {
  display: none;
}

.quote-mark {
  position: static;
  order: 0;
  margin: 0 0 26px;
  color: #171717;
  font-family: "Noto Serif TC", "Noto Sans TC", "Microsoft JhengHei", serif;
  font-size: 34px;
  font-weight: 800;
  line-height: 1.16;
  letter-spacing: 2px;
  text-transform: none;
}

.quote-header {
  position: relative;
  order: 1;
  display: grid;
  grid-template-columns: 1fr 198px;
  gap: 28px;
  padding-bottom: 18px;
  border-bottom: 1px solid #202020;
}

.brand-kicker,
.block-label {
  margin: 0 0 7px;
  color: #383838;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.brand-block h2 {
  margin: 0;
  font-family: "Noto Sans TC", "Microsoft JhengHei", Arial, sans-serif;
  font-size: 22px;
  font-weight: 900;
  line-height: 1.25;
  letter-spacing: 0;
}

.brand-block p:last-child {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.quote-meta {
  display: grid;
  align-content: start;
  gap: 10px;
}

.quote-meta p {
  margin: 0;
  padding: 8px 0;
  border-bottom: 1px solid #d7d7d7;
}

.quote-meta span,
.total-band span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.quote-meta strong {
  display: block;
  margin-top: 3px;
  font-size: 13px;
}

.parties-grid {
  order: 4;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 28px;
  padding: 28px 0 0;
}

.parties-grid h3 {
  margin: 0 0 8px;
  font-size: 17px;
}

.parties-grid p {
  margin: 3px 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.total-band {
  order: 2;
  display: grid;
  grid-template-columns: 1fr 210px;
  gap: 20px;
  align-items: end;
  margin: 18px 0 10px;
  padding: 0 0 12px;
  color: #171717;
  background: transparent;
  border-bottom: 1px solid #202020;
  border-left: 0;
}

.total-band.price-list-mode {
  grid-template-columns: 1fr;
}

.total-band p {
  margin: 0 0 4px;
  color: #3d3d3d;
  font-size: 14px;
  font-weight: 800;
}

.total-band strong {
  font-size: 16px;
}

.total-band div:last-child {
  text-align: right;
}

.total-band div:last-child strong {
  display: block;
  margin-top: 3px;
  font-family: Arial, "Noto Sans TC", sans-serif;
  font-size: 31px;
  font-weight: 900;
}

.quote-table {
  order: 3;
  width: 100%;
  border-collapse: collapse;
}

.quote-table th {
  padding: 10px 9px;
  color: #fffaf0;
  background: #171717;
  font-size: 11px;
  text-align: left;
}

.quote-table td {
  padding: 10px 9px;
  border-bottom: 1px solid #dedede;
  font-size: 12px;
  line-height: 1.45;
  vertical-align: top;
}

.quote-table tbody tr:nth-child(even) td {
  background: #f6f3ed;
}

.number-cell {
  width: 105px;
  text-align: right;
  white-space: nowrap;
}

.quote-table .description-cell {
  font-weight: 700;
}

.notes-area {
  order: 5;
  margin-top: 24px;
  padding: 14px 16px;
  background: #f6f6f6;
  border: 1px solid #dedede;
  border-radius: 6px;
}

.notes-area p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-line;
}

.quote-footer {
  position: absolute;
  right: 58px;
  bottom: 42px;
  left: 58px;
  display: grid;
  grid-template-columns: 1fr 1.35fr;
  gap: 24px;
  align-items: end;
  padding-top: 20px;
  border-top: 1px solid #202020;
}

.quote-footer p {
  margin: 3px 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.quote-footer .thanks {
  margin-bottom: 8px;
  color: var(--ink);
  font-family: Arial, "Noto Sans TC", sans-serif;
  font-size: 17px;
  font-weight: 900;
}

.payment-box {
  padding: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
}

@media (max-width: 1100px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .editor-panel {
    max-height: none;
  }

  .quote-page {
    width: min(100%, 760px);
  }
}

@media (max-width: 720px) {
  body {
    padding-bottom: 76px;
  }

  .topbar {
    align-items: center;
    flex-direction: row;
    padding: 12px 14px;
  }

  .topbar h1 {
    font-size: 20px;
  }

  .topbar-actions {
    display: none;
  }

  .mobile-view-switch {
    position: sticky;
    top: 72px;
    z-index: 18;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    width: 100%;
    max-width: 100vw;
    padding: 10px 14px;
    background: rgba(246, 239, 227, 0.94);
    border-bottom: 1px solid rgba(176, 138, 74, 0.22);
    backdrop-filter: blur(12px);
  }

  .mobile-tab {
    min-width: 0;
    min-height: 42px;
    color: var(--charcoal);
    background: #fffaf1;
    border: 1px solid #d8cbb6;
    border-radius: 6px;
    font-weight: 900;
  }

  .mobile-tab.active {
    color: #fffaf0;
    background: #171612;
    border-color: #171612;
  }

  .mobile-bottom-actions {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
    width: 100%;
    max-width: 100vw;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    background: rgba(23, 22, 18, 0.96);
    border-top: 1px solid rgba(176, 138, 74, 0.44);
    box-shadow: 0 -12px 28px rgba(17, 16, 13, 0.18);
  }

  .mobile-bottom-actions button {
    min-width: 0;
    padding: 0 4px;
    min-height: 44px;
    color: #fffaf0;
    background: #2b2923;
    border: 1px solid rgba(255, 250, 240, 0.12);
    border-radius: 6px;
    font-size: 14px;
    font-weight: 900;
  }

  .mobile-bottom-actions button:nth-child(3) {
    background: linear-gradient(135deg, #a67b36, #6f501f);
    border-color: #a67b36;
  }

  .mobile-bottom-actions button:nth-child(4) {
    color: #171612;
    background: #fffaf0;
  }

  .preview-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .preview-actions,
  .inline-control,
  .history-tools {
    flex-wrap: wrap;
  }

  .inline-control,
  .history-tools {
    align-items: stretch;
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
  }

  .inline-control input,
  .history-tools input {
    min-width: 0;
    width: 100%;
  }

  .workspace {
    display: block;
    max-width: 100vw;
    overflow-x: hidden;
    padding: 14px;
  }

  body[data-mobile-view="editor"] .preview-area {
    display: none;
  }

  body[data-mobile-view="preview"] .editor-panel {
    display: none;
  }

  body[data-mobile-view="preview"] .preview-area {
    display: block;
  }

  .editor-panel {
    gap: 12px;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
    padding-right: 0;
  }

  .panel-section {
    width: 100%;
    max-width: 100%;
    padding: 14px;
  }

  .field-grid.two,
  .field-grid.four,
  .field-grid.item-controls,
  .quote-header,
  .parties-grid,
  .total-band,
  .quote-footer {
    grid-template-columns: 1fr;
  }

  input,
  select,
  textarea,
  .primary-button,
  .secondary-button,
  .icon-button {
    min-height: 44px;
    font-size: 16px;
  }

  .history-list {
    max-height: 190px;
  }

  .item-card {
    padding: 14px;
  }

  .quote-page {
    min-height: auto;
    width: 100%;
    margin-bottom: 92px;
    padding: 30px 18px 28px;
  }

  .quote-mark {
    display: block;
    margin-bottom: 20px;
    font-size: 25px;
  }

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

  .quote-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .quote-table th,
  .quote-table td {
    min-width: 92px;
  }

  .quote-table th:first-child,
  .quote-table td:first-child {
    min-width: 210px;
  }

  .quote-footer {
    position: static;
    margin-top: 28px;
  }

  .number-cell {
    width: auto;
  }
}

@page {
  size: A4;
  margin: 0;
}

@media print {
  body {
    background: #fff;
  }

  .topbar,
  .editor-panel,
  .preview-toolbar,
  .mobile-view-switch,
  .mobile-bottom-actions {
    display: none !important;
  }

  .app-shell,
  .workspace,
  .preview-area {
    display: block;
    min-height: 0;
    padding: 0;
    margin: 0;
    background: #fff;
  }

  .quote-page {
    width: 210mm;
    min-height: 297mm;
    margin: 0;
    padding: 15mm 15mm 12mm;
    border: 0;
    box-shadow: none;
  }

  .quote-page::before {
    height: 4mm;
  }

  .quote-footer {
    right: 15mm;
    bottom: 12mm;
    left: 15mm;
  }
}
