:root {
  --bg: #f4f7f8;
  --ink: #17333c;
  --muted: #607980;
  --teal: #007f78;
  --teal-dark: #006660;
  --teal-soft: #e7f1f2;
  --white: #ffffff;
  --red: #c62828;
  --border: #d5e2e4;
  --radius-card: 20px;
  --radius-btn: 15px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  line-height: 1.4;
}

.app-shell {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  padding-top: calc(6px + var(--safe-top));
  padding-bottom: calc(10px + var(--safe-bottom));
  padding-left: 16px;
  padding-right: 16px;
}

.loading-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--muted);
}

.loading-screen img {
  border-radius: 18px;
  box-shadow: 0 4px 16px rgba(23, 51, 60, 0.12);
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  padding-bottom: 6px;
}

.header-btn {
  width: 50px;
  height: 50px;
  flex-shrink: 0;
  border: none;
  border-radius: var(--radius-btn);
  background: var(--white);
  color: var(--teal);
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 3px rgba(23, 51, 60, 0.06);
}

.header-btn:active {
  transform: scale(0.96);
  background: var(--teal-soft);
}

.header-caption {
  flex: 1;
  text-align: center;
  font-size: 15px;
  color: var(--muted);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Scrollable body */
.app-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 0 24px;
}

.page-title {
  margin: 0 0 6px;
  font-size: 26px;
  font-weight: 700;
  color: var(--ink);
  line-height: 1.25;
}

.page-note {
  margin: 4px 0 8px;
  font-size: 14px;
  color: var(--muted);
  white-space: pre-line;
}

.section-subtitle {
  margin: 14px 0 6px;
  font-size: 20px;
  font-weight: 700;
  color: var(--ink);
}

.accent-text {
  font-size: 18px;
  font-weight: 600;
  color: var(--teal);
  margin: 4px 0 8px;
}

.record-line {
  font-size: 15px;
  padding: 4px 0;
  color: var(--ink);
}

.record-line.red {
  color: var(--red);
  font-weight: 600;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 52px;
  padding: 10px 14px;
  margin-top: 10px;
  border: none;
  border-radius: var(--radius-btn);
  font-size: 17px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  background: var(--white);
  color: var(--teal);
  box-shadow: 0 1px 3px rgba(23, 51, 60, 0.07);
  transition: transform 0.08s ease, background-color 0.12s ease;
}

.btn.primary {
  background: var(--teal);
  color: var(--white);
}

.btn.danger {
  color: var(--red);
}

.btn:active:not(:disabled) {
  transform: scale(0.985);
  filter: brightness(0.96);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.compact {
  width: auto;
  min-height: 40px;
  margin-top: 0;
  padding: 6px 12px;
  font-size: 14px;
  border-radius: 12px;
}

.btn.inline-overview {
  width: auto;
  min-height: 36px;
  margin-top: 6px;
  padding: 5px 12px;
  font-size: 14px;
  border-radius: 11px;
  background: var(--teal-soft);
  color: var(--teal);
  box-shadow: none;
}

/* Cards */
.card {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 14px 14px 14px 16px;
  margin-top: 10px;
  background: var(--white);
  border-radius: var(--radius-card);
  box-shadow: 0 1px 4px rgba(23, 51, 60, 0.06);
  cursor: pointer;
  user-select: none;
}

.card:active {
  background: #edf4f4;
}

.card-icon {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-words {
  flex: 1;
  min-width: 0;
}

.card-heading {
  font-size: 20px;
  font-weight: 700;
  color: var(--ink);
  white-space: pre-line;
  word-break: break-word;
}

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

.card-chevron {
  font-size: 28px;
  color: var(--teal);
  line-height: 1;
  padding-left: 4px;
  flex-shrink: 0;
}

.tutorial-btn {
  flex-shrink: 0;
  border: 1px solid #f0c9c9;
  background: #fff5f5;
  color: var(--red);
  border-radius: 11px;
  padding: 8px 10px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

/* Grids & Rows */
.formation-grid {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.formation-grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.formation-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }

.formation-grid .btn {
  margin-top: 0;
  min-height: 58px;
  font-size: 16px;
  padding: 8px 6px;
}

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

.row-split > * {
  flex: 1;
  margin-top: 0 !important;
}

/* Car bar + Turn button */
.car-bar-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.car-strip {
  flex: 1;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px 0;
}

.car-strip::-webkit-scrollbar { display: none; }

.car-pill {
  min-width: 48px;
  height: 48px;
  border: none;
  border-radius: 13px;
  background: var(--white);
  color: var(--teal);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
  box-shadow: 0 1px 3px rgba(23, 51, 60, 0.06);
}

.car-pill.active {
  background: var(--teal);
  color: var(--white);
}

.turn-btn {
  width: 88px;
  height: 48px;
  flex-shrink: 0;
  border: none;
  border-radius: 13px;
  background: var(--white);
  color: var(--teal);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(23, 51, 60, 0.06);
}

.turn-btn.active {
  background: var(--teal);
  color: var(--white);
}

/* Direction context */
.direction-context {
  margin: 6px 0 10px;
}

.direction-ends {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  font-weight: 600;
  color: var(--teal);
  padding: 4px 2px 0;
}

/* Diagrams */
.part-diagram-wrap {
  position: relative;
  width: 100%;
  border-radius: 16px;
  overflow: hidden;
  background: var(--bg);
}

.part-diagram-wrap.white-bg,
.diagram-card-wrap,
.overview-diagram-wrap {
  background: var(--white);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(23, 51, 60, 0.06);
  margin: 8px 0;
}

.part-diagram-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.diagram-hit-btn {
  position: absolute;
  transform: translate(-50%, -50%);
  width: 48px;
  height: 48px;
  border-radius: 12px;
  border: 1px solid #9fbfba;
  background: var(--white);
  color: var(--teal);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  box-shadow: 0 2px 5px rgba(23, 51, 60, 0.1);
}

.diagram-hit-btn.large {
  width: 54px;
  height: 54px;
  font-size: 19px;
}

.diagram-hit-btn.selected {
  background: var(--teal);
  color: var(--white);
  border-color: var(--teal);
}

.front-diagram-wrap {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ref-image {
  display: block;
  width: 100%;
  object-fit: contain;
  border-radius: 12px;
  background: var(--white);
  margin: 8px 0;
}

/* Measurement Inputs */
.plough-layout {
  display: grid;
  grid-template-columns: 1fr 1.25fr 1fr;
  gap: 8px;
  align-items: center;
  margin: 8px 0;
}

.plough-col {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.measure-box {
  background: var(--white);
  border-radius: 12px;
  padding: 8px;
  box-shadow: 0 1px 3px rgba(23, 51, 60, 0.05);
}

.measure-box label {
  display: block;
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
}

.measure-box .spec {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

.input-field {
  width: 100%;
  min-height: 46px;
  padding: 8px 10px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: #fbfdfe;
  color: var(--ink);
  font-size: 18px;
  font-family: inherit;
  outline: none;
}

.input-field:focus {
  border-color: var(--teal);
  background: var(--white);
}

.input-field.large-num {
  font-size: 22px;
  min-height: 50px;
}

.input-field.red {
  color: var(--red);
  border-color: #e6a2a2;
  font-weight: 700;
}

.extra-field-group {
  margin-top: 10px;
  background: var(--white);
  border-radius: 12px;
  padding: 10px 12px;
}

.extra-field-group label {
  display: block;
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 6px;
}

.axle-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.air-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--white);
  border-radius: 12px;
  padding: 10px;
  margin-top: 8px;
}

.air-row-idx {
  width: 32px;
  font-size: 16px;
  font-weight: 700;
  color: var(--teal);
  padding-bottom: 12px;
}

.air-row-col {
  flex: 1;
  min-width: 0;
}

.air-row-col label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

.hose-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--white);
  border-radius: 12px;
  padding: 8px 12px;
  margin-top: 8px;
}

.hose-row-label {
  width: 72px;
  font-size: 16px;
  font-weight: 700;
  flex-shrink: 0;
}

/* Checklist rows */
.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  margin-top: 8px;
  background: var(--white);
  border-radius: 14px;
  box-shadow: 0 1px 3px rgba(23, 51, 60, 0.05);
}

.check-row label {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  min-height: 36px;
}

.check-row input[type="checkbox"] {
  width: 22px;
  height: 22px;
  accent-color: var(--teal);
  flex-shrink: 0;
}

.check-row.indent {
  margin-left: 18px;
}

/* History & Other rows */
.history-item-wrap {
  margin-top: 10px;
}

.history-actions {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.history-actions .btn {
  margin-top: 0;
  min-height: 42px;
  font-size: 14px;
}

.other-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  margin-top: 10px;
  background: var(--white);
  border-radius: 18px;
  box-shadow: 0 1px 4px rgba(23, 51, 60, 0.06);
}

.other-row-body {
  flex: 1;
  cursor: pointer;
}

/* Mail recipient row */
.recipient-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  margin-top: 8px;
  background: var(--white);
  border-radius: 14px;
}

.recipient-info {
  flex: 1;
  min-width: 0;
  font-size: 15px;
  white-space: pre-line;
  word-break: break-all;
}

.recipient-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Tabs */
.tabs-row {
  display: flex;
  gap: 6px;
  margin: 8px 0 10px;
}

.tabs-row .btn {
  margin-top: 0;
  min-height: 48px;
  font-size: 14px;
  padding: 8px 4px;
}

/* Preview canvas */
.preview-frame {
  background: var(--white);
  border-radius: 12px;
  padding: 6px;
  box-shadow: 0 2px 10px rgba(23, 51, 60, 0.12);
  cursor: zoom-in;
  margin: 8px 0;
}

.preview-frame canvas {
  display: block;
  width: 100%;
  height: auto;
}

/* Footer */
.app-footer {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
  padding-top: 8px;
}

.app-footer .btn {
  margin-top: 0;
  min-height: 52px;
}

/* PWA install banner */
.install-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  margin-top: 12px;
  background: var(--teal-soft);
  border: 1px solid #b8d8d6;
  border-radius: 14px;
  font-size: 14px;
  color: var(--ink);
}

/* Modals */
.modal-root {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(16, 36, 44, 0.52);
  padding: 16px;
}

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

.modal-dialog {
  width: 100%;
  max-width: 440px;
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  background: var(--white);
  border-radius: 22px;
  padding: 20px;
  box-shadow: 0 12px 36px rgba(10, 26, 32, 0.28);
  overflow: hidden;
}

.modal-dialog.fullscreen {
  max-width: 96vw;
  max-height: 94dvh;
  padding: 12px;
}

.modal-title {
  margin: 0 0 10px;
  font-size: 20px;
  font-weight: 700;
  color: var(--ink);
}

.modal-body {
  overflow-y: auto;
  flex: 1;
  font-size: 15px;
  color: var(--ink);
  white-space: pre-line;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.modal-actions .btn {
  width: auto;
  margin-top: 0;
  min-height: 44px;
  padding: 8px 16px;
  font-size: 15px;
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.menu-list .btn {
  margin-top: 0;
  justify-content: flex-start;
  background: var(--bg);
  color: var(--ink);
  box-shadow: none;
}

/* Zoom viewer */
.zoom-stage {
  flex: 1;
  overflow: auto;
  background: #e5ecef;
  border-radius: 12px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8px;
  touch-action: pan-x pan-y pinch-zoom;
}

.zoom-controls {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
}

/* Toast */
.toast-root {
  position: fixed;
  left: 50%;
  bottom: calc(74px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 1100;
  pointer-events: none;
  max-width: 90vw;
}

.toast-msg {
  background: rgba(23, 51, 60, 0.92);
  color: var(--white);
  padding: 10px 18px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22);
  animation: fadeIn 0.16s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.red-span { color: var(--red); font-weight: 600; }
.shortcut-row {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}
.shortcut-row .btn { margin-top: 0; min-height: 40px; font-size: 14px; }
.shortcut-row .btn.shortcut-main { flex: 1; justify-content: flex-start; background: var(--bg); color: var(--ink); box-shadow: none; }
.shortcut-row .btn.shortcut-del { width: auto; padding: 0 12px; background: #fff5f5; color: var(--red); box-shadow: none; }
