:root {
  --page-bg: #f5f7f8;
  --panel-bg: #ffffff;
  --ink: #1f2933;
  --muted: #5f6b76;
  --line: #cfd8df;
  --line-strong: #8d9aa5;
  --accent: #176f75;
  --accent-dark: #105a60;
  --danger: #b42318;
  --danger-bg: #fff1f0;
  --table-head: #e8eff1;
  --soft: #eef3f4;
}

* {
  box-sizing: border-box;
}

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

.app-header {
  padding: 28px 24px 18px;
  background: #ffffff;
  border-bottom: 1px solid var(--line);
}

.app-header h1 {
  margin: 0 0 6px;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 0;
}

.app-header p {
  margin: 0;
  color: var(--muted);
}

.app-shell {
  width: min(1280px, calc(100% - 32px));
  margin: 24px auto 48px;
}

.app-error-message,
.quote-save-status,
.storage-status {
  margin: 0;
  font-weight: 700;
}

.app-error-message {
  padding: 12px 14px;
  border: 1px solid #b42318;
  background: #fff1f0;
  color: #8a1c14;
}

.quote-save-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.quote-save-status {
  color: #166534;
}

.quote-save-status.is-error {
  color: #b42318;
}

.storage-status {
  flex-basis: 100%;
  color: var(--muted);
  font-size: 14px;
  text-align: right;
}

.storage-status.is-warning {
  color: #9a6700;
}

.storage-status.is-error {
  color: #b42318;
}

.editor-panel,
.preview-panel {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
}

.section-heading {
  margin-bottom: 16px;
}

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

.section-heading h2 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0;
}

.export-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.export-message {
  min-height: 24px;
  margin: -6px 0 12px;
  color: var(--muted);
  font-weight: 700;
}

.export-message.is-error {
  color: var(--danger);
}

.export-message.is-success {
  color: var(--accent-dark);
}

.basic-grid,
.item-fields {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: #26323d;
  font-size: 15px;
  font-weight: 700;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 10px 11px;
  color: var(--ink);
  background: #ffffff;
  font: inherit;
  font-weight: 400;
}

textarea {
  resize: vertical;
}

input:focus,
textarea:focus {
  outline: 3px solid rgba(23, 111, 117, 0.18);
  border-color: var(--accent);
}

.item-calculation-field {
  display: flex;
  min-height: 70px;
  flex-direction: column;
  gap: 6px;
  color: #26323d;
  font-size: 15px;
  font-weight: 700;
}

.item-subtotal {
  display: flex;
  min-height: 44px;
  align-items: center;
  justify-content: flex-end;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 11px;
  background: var(--soft);
  color: var(--ink);
  font-weight: 700;
}

.item-subtotal.is-invalid,
.item-calculation-message {
  color: var(--danger);
}

.item-calculation-message {
  min-height: 18px;
  margin: 0;
  font-size: 13px;
  font-weight: 400;
}

button {
  border: 1px solid transparent;
  border-radius: 6px;
  min-height: 44px;
  padding: 9px 14px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.primary-button {
  background: var(--accent);
  color: #ffffff;
}

.primary-button:hover {
  background: var(--accent-dark);
}

.danger-button {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: #f2b8b5;
}

.secondary-button {
  background: var(--soft);
  color: #26323d;
  border-color: var(--line);
}

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

.item-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  background: #fbfcfc;
}

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

.item-title {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0;
}

.product-search {
  margin-bottom: 16px;
}

.product-search-results {
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: #ffffff;
}

.product-result-button {
  display: grid;
  width: 100%;
  grid-template-columns: 90px minmax(0, 1fr) 110px 150px;
  gap: 10px;
  align-items: center;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  min-height: 52px;
  padding: 12px;
  background: #ffffff;
  color: var(--ink);
  text-align: left;
  font-weight: 400;
}

.product-result-button:last-child {
  border-bottom: 0;
}

.product-result-button:hover,
.product-result-button:focus {
  background: var(--soft);
}

.product-result-brand,
.product-result-code,
.product-result-barcode {
  color: var(--muted);
  font-size: 14px;
}

.product-result-name {
  font-weight: 700;
  word-break: break-word;
}

.product-no-results {
  margin: 0;
  padding: 10px 12px;
  color: var(--muted);
}

.wide-field {
  grid-column: span 2;
}

.plan-field {
  grid-column: span 2;
}

.full-field {
  grid-column: span 3;
}

.settings-panel {
  margin-bottom: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-bg);
}

.settings-panel details {
  overflow: hidden;
}

.settings-panel summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 16px 20px;
  cursor: pointer;
  font-size: 20px;
  font-weight: 700;
}

.settings-summary-note {
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.settings-body {
  padding: 0 20px 20px;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.settings-wide-field {
  grid-column: span 2;
}

.settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
}

.settings-message {
  min-height: 20px;
  margin: 10px 0 0;
  color: var(--muted);
  font-weight: 700;
}

.settings-message.is-error {
  color: var(--danger);
}

.cloud-backup-settings {
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.cloud-backup-settings h3 {
  margin: 0 0 14px;
  font-size: 17px;
}

.backup-status,
.backup-last-success {
  min-height: 20px;
  margin: 10px 0 0;
  color: var(--muted);
  font-weight: 700;
}

.backup-status.is-error {
  color: var(--danger);
}

.backup-status.is-success {
  color: var(--accent-dark);
}

.history-panel {
  margin-bottom: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-bg);
}

.history-panel details {
  overflow: hidden;
}

.history-panel summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 58px;
  padding: 16px 20px;
  cursor: pointer;
  font-size: 20px;
  font-weight: 700;
}

.history-count {
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.history-message {
  min-height: 20px;
  margin: 0 20px 12px;
  color: var(--muted);
  font-weight: 700;
}

.history-message.is-error {
  color: var(--danger);
}

.history-list {
  display: grid;
  gap: 10px;
  padding: 0 20px 20px;
}

.history-empty {
  margin: 0;
  padding: 16px;
  border: 1px dashed var(--line-strong);
  color: var(--muted);
  text-align: center;
}

.history-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfc;
}

.history-card h3,
.history-card p {
  margin: 0;
}

.history-card h3 {
  font-size: 17px;
  overflow-wrap: anywhere;
}

.history-card p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 14px;
}

.history-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.history-actions button {
  min-height: 44px;
  padding: 7px 10px;
}

.uploaded-image-row {
  display: none;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.uploaded-image-row.has-image {
  display: flex;
}

.uploaded-image {
  width: 96px;
  height: 96px;
  object-fit: contain;
  border: 1px solid var(--line);
  background: #ffffff;
  border-radius: 6px;
}

.image-message {
  margin: 10px 0 0;
  color: var(--muted);
  font-weight: 700;
}

.quote-preview {
  width: min(100%, 1180px);
  margin: 0 auto;
  border: 1px solid var(--line-strong);
  background: #ffffff;
  padding: 24px;
}

.quote-title {
  margin: 0 0 16px;
  text-align: center;
  font-size: 28px;
  letter-spacing: 0;
}

.quote-plan {
  margin: -8px 0 16px;
  color: var(--accent-dark);
  text-align: center;
  font-size: 17px;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.quote-meta {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0;
  margin-bottom: 14px;
  border-top: 1px solid var(--line-strong);
  border-left: 1px solid var(--line-strong);
  font-size: 15px;
}

.quote-meta-cell {
  display: grid;
  grid-template-columns: 78px minmax(0, 1fr);
  min-height: 38px;
  border-right: 1px solid var(--line-strong);
  border-bottom: 1px solid var(--line-strong);
}

.quote-meta-label {
  display: flex;
  align-items: center;
  padding: 6px 7px;
  border-right: 1px solid var(--line-strong);
  background: var(--table-head);
  font-weight: 700;
  white-space: nowrap;
}

.quote-meta-value {
  display: flex;
  align-items: center;
  min-width: 0;
  padding: 6px 8px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.quote-meta-full {
  grid-column: span 3;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line-strong);
}

.quote-table {
  width: 100%;
  min-width: 1160px;
  border-collapse: collapse;
  table-layout: fixed;
}

.quote-table th,
.quote-table td {
  border: 1px solid var(--line-strong);
  padding: 8px 7px;
  text-align: center;
  vertical-align: middle;
  word-break: break-word;
  white-space: pre-wrap;
}

.quote-table th {
  background: var(--table-head);
  font-weight: 700;
}

.quote-table th:nth-child(3),
.quote-table td:nth-child(3),
.quote-table th:nth-child(8),
.quote-table td:nth-child(8),
.quote-table th:nth-child(10),
.quote-table td:nth-child(10) {
  text-align: left;
}

.quote-table th:nth-child(1) {
  width: 12%;
}

.quote-table th:nth-child(2) {
  width: 7%;
}

.quote-table th:nth-child(3) {
  width: 21%;
}

.quote-table th:nth-child(4) {
  width: 5%;
}

.quote-table th:nth-child(5) {
  width: 7%;
}

.quote-table th:nth-child(6) {
  width: 9%;
}

.quote-table th:nth-child(7) {
  width: 8%;
}

.quote-table th:nth-child(8) {
  width: 10%;
}

.quote-table th:nth-child(9) {
  width: 7%;
}

.quote-table th:nth-child(10) {
  width: 14%;
}

.quote-table tr.has-invalid-calculation td:nth-child(7) {
  color: var(--danger);
  font-weight: 700;
}

.quote-total-row {
  display: flex;
  min-height: 48px;
  align-items: center;
  justify-content: flex-end;
  gap: 18px;
  border: 1px solid var(--line-strong);
  border-top: 0;
  padding: 9px 14px;
  font-size: 17px;
  font-weight: 700;
}

.quote-total-row strong {
  min-width: 150px;
  text-align: right;
  font-size: 20px;
}

.preview-calculation-message {
  margin: 8px 0 0;
  color: var(--danger);
  text-align: right;
  font-weight: 700;
}

.quote-terms {
  display: grid;
  grid-template-columns: 110px minmax(0, 1fr);
  min-height: 54px;
  margin-top: 14px;
  border: 1px solid var(--line-strong);
}

.quote-terms h3,
.quote-terms p {
  margin: 0;
  padding: 9px 10px;
}

.quote-terms h3 {
  border-right: 1px solid var(--line-strong);
  background: var(--table-head);
  font-size: 15px;
}

.quote-terms p {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.empty-row td,
.empty-images {
  color: var(--muted);
  text-align: center;
}

.quote-images {
  margin-top: 22px;
}

.quote-images h3 {
  margin: 0 0 12px;
  font-size: 18px;
  letter-spacing: 0;
}

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

.image-card {
  border: 1px solid var(--line-strong);
  padding: 10px;
  background: #ffffff;
  min-height: 260px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.image-frame {
  width: 100%;
  height: 210px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffffff;
}

.image-frame img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
}

.image-caption {
  margin: 10px 0 0;
  text-align: center;
  font-weight: 700;
  word-break: break-word;
}

.jpg-export-host .quote-preview {
  width: 1120px !important;
  max-width: none !important;
  padding: 28px !important;
}

.jpg-export-host .table-wrap {
  overflow: visible !important;
}

.jpg-export-host .quote-table {
  min-width: 0 !important;
  width: 100% !important;
}

.jpg-export-host .image-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
}

@media (max-width: 900px) {
  .basic-grid,
  .item-fields,
  .image-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .product-result-button {
    grid-template-columns: 80px minmax(0, 1fr);
  }

  .product-result-code,
  .product-result-barcode {
    grid-column: span 1;
  }

  .wide-field {
    grid-column: span 2;
  }
}

@media print {
  @page {
    size: A4 landscape;
    margin: 8mm;
  }

  html,
  body {
    width: 297mm;
    background: #ffffff;
  }

  body * {
    visibility: hidden;
  }

  .quote-preview,
  .quote-preview * {
    visibility: visible;
  }

  .app-header,
  .app-error-message,
  .settings-panel,
  .editor-panel,
  .history-panel,
  .preview-panel > .section-heading,
  .export-message {
    display: none !important;
  }

  .app-shell,
  .preview-panel {
    width: auto;
    margin: 0;
    padding: 0;
    border: 0;
    background: #ffffff;
  }

  .quote-preview {
    position: absolute;
    left: 0;
    top: 0;
    width: 281mm;
    max-width: none;
    border: 0;
    padding: 0;
  }

  .table-wrap {
    overflow: visible;
    border: 1px solid var(--line-strong);
  }

  .quote-table {
    min-width: 0;
    width: 100%;
    font-size: 10px;
  }

  .quote-table thead {
    display: table-header-group;
  }

  .quote-table tr,
  .image-card,
  .quote-total-row,
  .quote-terms {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .quote-images {
    break-before: auto;
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100% - 20px, 1280px);
    margin-top: 12px;
  }

  .app-header,
  .editor-panel,
  .preview-panel,
  .quote-preview {
    padding: 16px;
  }

  .section-heading-row,
  .item-card-header {
    align-items: stretch;
    flex-direction: column;
  }

  .quote-save-actions,
  .export-actions {
    width: 100%;
    justify-content: stretch;
  }

  .quote-save-actions button,
  .export-actions button {
    flex: 1 1 140px;
  }

  .basic-grid,
  .item-fields {
    grid-template-columns: 1fr;
  }

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

  .product-result-button {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  .wide-field,
  .plan-field,
  .full-field,
  .settings-wide-field,
  .quote-meta-full {
    grid-column: auto;
  }

  .settings-grid {
    grid-template-columns: 1fr;
  }

  .settings-panel summary {
    min-height: 52px;
    padding: 14px 16px;
    font-size: 18px;
  }

  .settings-body {
    padding: 0 16px 16px;
  }

  .settings-summary-note {
    font-size: 13px;
  }

  .quote-meta {
    grid-template-columns: 1fr;
  }

  .quote-meta-cell {
    grid-template-columns: 84px minmax(0, 1fr);
  }

  .quote-terms {
    grid-template-columns: 92px minmax(0, 1fr);
  }

  .quote-total-row {
    justify-content: space-between;
  }

  .quote-total-row strong {
    min-width: 0;
  }

  .quote-title {
    font-size: 24px;
  }

  .image-card {
    min-height: 230px;
  }

  .image-frame {
    height: 180px;
  }

  .history-panel summary {
    min-height: 52px;
    padding: 14px 16px;
    font-size: 18px;
  }

  .history-message {
    margin: 0 16px 10px;
  }

  .history-list {
    padding: 0 16px 16px;
  }

  .history-card {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .history-actions {
    justify-content: stretch;
  }

  .history-actions button {
    flex: 1 1 0;
  }

  .storage-status {
    text-align: left;
  }
}
