/* ---- Reset & base ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f4f4f5;
  --surface: #fff;
  --border: #e4e4e7;
  --border-light: #ececee;
  --text: #18181b;
  --text-secondary: #52525b;
  --text-muted: #71717a;
  --text-dim: #a1a1aa;
  --accent: #0f766e;
  --accent-light: #e6f7f5;
  --accent-border: #99d5cf;
  --danger: #dc2626;
  --radius: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Inter, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  /* Let the iframe/page handle a single scrollbar. */
  overflow: auto;
}

.hidden { display: none !important; }
.muted  { color: var(--text-muted); }
.req    { color: var(--danger); }

/* ---- App shell ---- */
.app {
  max-width: 960px;
  margin: 0 auto;
  background: var(--surface);
  min-height: 100vh;
  box-shadow: 0 0 0 1px var(--border);
}

/* ---- Loader ---- */
.page-loader {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; height: 100vh; gap: 14px; color: var(--text-muted);
}
.spinner {
  width: 28px; height: 28px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Error ---- */
.error-banner {
  padding: 14px 20px;
  background: #fef2f2; color: var(--danger);
  border-left: 3px solid var(--danger);
  font-weight: 500;
}

/* ---- Patient strip ---- */
.patient-strip {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.patient-identity { display: flex; align-items: baseline; gap: 10px; }
.patient-identity strong { font-size: 15px; }
.patient-identity .muted { font-size: 13px; }
.badge {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  padding: 4px 12px; border-radius: 100px;
  background: var(--accent-light); color: var(--accent);
}

/* ---- Workspace (two-col) ---- */
.workspace {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 0;
  min-height: calc(100vh - 53px);
}

/* ---- Catalog (left) ---- */
.catalog {
  padding: 20px 24px;
  /* Allow the whole page to scroll (no left-only scrollbar). */
  overflow-y: visible;
  height: auto;
}
.catalog h2 { font-size: 15px; font-weight: 700; margin-bottom: 16px; color: var(--text); }

/* ---- Presets ---- */
.presets-groups {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 20px;
}
.presets-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 12px 16px;
  background: var(--accent-light);
  border-radius: var(--radius);
  border: 1px solid var(--accent-border);
}
.presets-group.presets-ht {
  background: #f4f6f8;
  border-color: var(--border);
}
.presets-label {
  font-size: 13px; font-weight: 600; color: var(--text-secondary);
  margin-right: 4px;
}
.preset-btn {
  padding: 6px 16px;
  background: var(--surface);
  border: 1.5px solid var(--accent-border);
  border-radius: 100px;
  font-size: 13px; font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.preset-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--surface); }
.preset-btn.active {
  border-color: var(--accent);
  color: #fff;
  background: var(--accent);
  font-weight: 600;
}
.product-view-controls {
  min-height: 24px;
  margin: -4px 0 8px;
  display: flex;
  justify-content: flex-end;
}
.toggle-products-btn {
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 2px;
}
.toggle-products-btn-expand {
  color: #014be4;
}
.toggle-products-btn:hover {
  color: var(--text-secondary);
}
.toggle-products-btn-expand:hover {
  color: #014be4;
  text-decoration: underline;
}

/* ---- Product table ---- */
.product-section-heading td {
  padding: 26px 12px 10px;
  border: none;
  background: var(--surface);
}
.product-section-heading span {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-dim);
}
.product-section-heading span::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--border);
}
.product-row-surgery td {
  border-top-width: 1px;
}
.product-row-surgery td.col-name {
  font-weight: 600;
}
.product-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}

.product-table th {
  text-align: left;
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-dim);
  padding: 0 12px 10px;
}
.col-name { width: auto; }
.product-table .col-price { width: 80px; text-align: center; }
.product-table .col-actions { width: 110px; text-align: center; }
.product-table .col-discount { width: 90px; text-align: center; }
.product-table .col-total { width: 80px; text-align: right; }

/* ---- Product row (parent) ---- */
.product-row td {
  padding: 14px 12px;
  background: var(--surface);
  border-top: 2px solid var(--border);
  vertical-align: middle;
}
.product-row td.col-name { font-weight: 600; font-size: 14px; }
.product-row td.col-price {
  text-align: center; font-variant-numeric: tabular-nums;
  color: var(--text-muted); font-size: 13px;
}
.product-row td.col-actions { text-align: center; }
.product-row td.col-total { text-align: right; font-variant-numeric: tabular-nums; }

.product-desc {
  display: block;
  font-size: 12px; color: var(--text-dim); font-weight: 400;
  margin-top: 2px;
  max-width: 280px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.row-total { font-weight: 700; font-size: 15px; }
.row-total.zero { color: var(--text-dim); font-weight: 400; font-size: 14px; }
.row-discount {
  font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums;
  color: var(--accent);
}
.row-discount.zero { visibility: hidden; }

/* +/- controls */
.unit-controls {
  display: inline-flex; align-items: center; gap: 0;
  border: 1px solid var(--border); border-radius: 6px;
  overflow: hidden;
}
.unit-btn {
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface);
  border: none;
  font-size: 16px; font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background .1s;
  user-select: none;
}
.unit-btn:hover:not(:disabled) { background: var(--bg); }
.unit-btn:active:not(:disabled) { background: var(--border); }
.unit-btn:disabled { opacity: .35; cursor: not-allowed; }
.unit-count {
  width: 36px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 600;
  color: var(--text);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  background: var(--bg);
  font-variant-numeric: tabular-nums;
}
.unit-count.zero { color: var(--text-dim); font-weight: 400; }

/* ---- Item sub-rows ---- */
.item-row td {
  padding: 8px 12px;
  background: var(--bg);
  border-top: 1px solid var(--border-light);
  vertical-align: middle;
}
.item-row td.col-total { text-align: right; font-variant-numeric: tabular-nums; }
.item-row td.col-discount { text-align: center; }
.item-label {
  font-size: 12px;
  color: var(--text-dim);
  padding-left: 20px !important;
}

.item-total {
  font-size: 13px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.item-label-discount {
  color: var(--accent);
  font-weight: 600;
}

.item-total-discount {
  color: var(--accent);
  font-weight: 600;
}

.item-discount-link {
  border: none;
  background: none;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
  font-size: 11px;
  padding: 0;
}

.item-discount-editor {
  display: inline-block;
}

/* Discount input */
.discount-input {
  width: 72px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  text-align: center;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  background: var(--surface);
  transition: border-color .15s;
  -moz-appearance: textfield;
}
.discount-input::-webkit-outer-spin-button,
.discount-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.discount-active {
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
}
.discount-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, .15);
}

/* ---- Summary sidebar (right) ---- */
.summary {
  position: sticky; top: 0; align-self: start;
  padding: 0;
  background: #fafafa;
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  /* Allow the whole page to scroll (no right-only scrollbar). */
  height: auto;
  overflow-y: visible;
}

.summary-section {
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
}
.summary-section:last-of-type { border-bottom: none; }
.summary-section h3 {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-dim);
  margin-bottom: 12px;
}

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field label { font-size: 12px; font-weight: 600; color: var(--text-muted); }
.field select, .field input {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  background: var(--surface);
  color: var(--text);
  transition: border-color .15s;
}
.field select:focus, .field input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, .15);
}

/* Debug panel */
.debug-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
}
.debug-toggle input { accent-color: var(--accent); }

/* Custom styling for debug-section checkboxes: grey cross, no filled background */
#debug-section input[type="checkbox"] {
  position: relative;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border: 1px solid #9ca3af;
  border-radius: 3px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  cursor: pointer;
}

#debug-section input[type="checkbox"]::before,
#debug-section input[type="checkbox"]::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 9px;
  height: 2px;
  background: #6b7280; /* grey cross */
  opacity: 0;
  transform-origin: center;
}

#debug-section input[type="checkbox"]::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

#debug-section input[type="checkbox"]::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

#debug-section input[type="checkbox"]:checked::before,
#debug-section input[type="checkbox"]:checked::after {
  opacity: 1;
}

.debug-controls {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
}
.debug-btn {
  padding: 5px 8px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 12px;
  text-align: left;
  cursor: pointer;
}
.debug-btn:hover {
  background: var(--bg);
}

.debug-log {
  margin-top: 10px;
  max-height: 220px;
  overflow-y: auto;
  padding: 8px;
  border-radius: 6px;
  background: #111827;
  color: #e5e7eb;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, \"Liberation Mono\", \"Courier New\", monospace;
  font-size: 11px;
  border: 1px solid #4b5563;
}
.debug-entry {
  margin-bottom: 6px;
}
.debug-entry-title {
  color: #93c5fd;
  font-weight: 600;
  margin-bottom: 2px;
}

/* QR banner (top of sidebar after plan creation) */
.qr-banner {
  text-align: center;
  background: var(--accent-light);
}
.qr-banner-canvas canvas {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,.06);
}
.qr-banner-link {
  display: block;
  margin-top: 10px;
  color: var(--accent);
  font-size: 11px;
  word-break: break-all;
}

/* Pricing */
.pricing-dl { margin: 0; }
.pricing-dl .row {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 4px 0;
  font-size: 13px;
}
.pricing-dl .row dt { color: var(--text-muted); font-weight: 400; }
.pricing-dl .row dd { font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.pricing-dl .divider {
  border: none; border-top: 1px solid var(--border);
  margin: 8px 0;
}
.pricing-dl .row.highlight dd { color: var(--accent); font-size: 15px; font-weight: 700; }
.extra-discount-controls {
  margin-top: 10px;
}
.btn-link-like {
  border: none;
  background: none;
  padding: 0;
  font-size: 12px;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}
.btn-link-like:hover {
  color: #0d6d64;
}
.extra-discount-controls .field {
  margin-bottom: 0;
}

/* Create button */
.btn-create {
  width: calc(100% - 40px);
  margin: 16px 20px 0;
  padding: 12px;
  background: var(--accent); color: #fff; border: none; border-radius: var(--radius);
  font-size: 15px; font-weight: 600; cursor: pointer;
  transition: background .15s;
}
.btn-create:hover:not(:disabled) { background: #0d6d64; }
.btn-create:disabled { opacity: .3; cursor: not-allowed; }

/* ---- Creation overlay ---- */
.creation-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  backdrop-filter: blur(2px);
}
.overlay-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 36px 44px;
  min-width: 420px; max-width: 520px;
  box-shadow: 0 24px 64px rgba(0,0,0,.2);
}

.steps-container { display: flex; flex-direction: column; gap: 4px; }
.step {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 0; font-size: 14px;
}
.step-dot {
  width: 24px; height: 24px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; flex-shrink: 0;
}
.step.pending  .step-dot { border: 2px solid var(--border); color: transparent; }
.step.active   .step-dot { border: 2px solid var(--accent); color: var(--accent); animation: pulse 1.2s ease infinite; }
.step.done     .step-dot { background: var(--accent); color: #fff; }
.step.error    .step-dot { background: var(--danger); color: #fff; }
.step.pending  { color: var(--text-dim); }
.step.active   { color: var(--text); font-weight: 500; }
.step.done     { color: var(--text-muted); }
.step.error    { color: var(--danger); }
.step-error-msg { font-size: 13px; color: var(--danger); margin-top: 4px; padding-left: 34px; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: .35; }
}

.qr-result { text-align: center; padding-top: 8px; }
.qr-result h2 { font-size: 18px; margin-bottom: 6px; color: var(--accent); }
.qr-result p  { margin-bottom: 20px; }
.qr-result canvas {
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,.08);
}
.doc-link {
  display: block; margin: 14px 0 20px;
  color: var(--accent); font-size: 13px; word-break: break-all;
}
.btn-secondary {
  padding: 9px 28px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 14px; font-weight: 500;
  cursor: pointer;
}
.btn-secondary:hover { background: var(--bg); }

/* ---- Responsive ---- */
@media (max-width: 800px) {
  .workspace { grid-template-columns: 1fr; }
  body { overflow: auto; }
  .app { height: auto; overflow: visible; }
  .summary {
    position: static; height: auto;
    border-left: none; border-top: 1px solid var(--border);
  }
  .btn-create { margin: 20px; }
  .overlay-card { min-width: auto; margin: 16px; width: calc(100% - 32px); }
}
