:root {
  color-scheme: light;
  --ink: #17221d;
  --muted: #68726d;
  --paper: #f3f0e8;
  --card: #fffefb;
  --green: #177a65;
  --green-soft: #dceee8;
  --line: #d7d8d1;
  --gold: #dda936;
  --danger: #a53f39;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.debug-only { display: none !important; }
.debug-mode .debug-only { display: block !important; }
.debug-mode .examples { display: flex !important; }
.debug-mode .state-pill { display: inline-block !important; }
.debug-mode .button.debug-only { display: inline-flex !important; }
html, body { margin: 0; min-height: 100%; background: var(--paper); color: var(--ink); }
body { min-height: 100dvh; display: flex; flex-direction: column; }
button, input, label { font: inherit; }
button, label { -webkit-tap-highlight-color: transparent; }
button:disabled { opacity: .43; cursor: not-allowed; }
button { transition: background-color .18s ease, border-color .18s ease, color .18s ease, transform .12s ease; }
button:not(:disabled):active { transform: scale(.985); }

.topbar {
  min-height: 66px; padding: max(12px, env(safe-area-inset-top)) clamp(16px, 4vw, 32px) 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  background: var(--ink); color: white;
}
.topbar strong, .topbar small { display: block; }
.topbar strong { font-size: 17px; }
.topbar small { margin-top: 2px; color: #cad3ce; font-size: 12px; }
.state-pill { padding: 7px 10px; border: 1px solid #506058; border-radius: 999px; color: #dbe2de; font-size: 12px; white-space: nowrap; }
.state-pill.ready { background: #165d4e; border-color: #438c79; }
.state-pill.error { background: #6d332f; border-color: #9a5e58; }
.topbar-actions { display: flex; align-items: center; gap: 9px; }
.topbar-actions > button { min-height: 40px; padding: 8px 12px; border: 1px solid #506058; border-radius: 10px; background: transparent; color: white; cursor: pointer; }

.workspace { width: min(1220px, 100%); margin: 0 auto; padding: 18px; display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 18px; flex: 1; }
.stage-card, .controls { background: var(--card); border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 10px 28px rgba(23,34,29,.07); }
.stage-card { position: relative; min-height: 640px; overflow: hidden; display: grid; place-items: center; }
.photo-actions { position: absolute; z-index: 8; top: 12px; right: 12px; }
.photo-actions .button { min-height: 42px; padding: 9px 13px; background: rgba(255,255,255,.94); box-shadow: 0 4px 16px rgba(23,34,29,.14); }
.inspection-controls { position: absolute; z-index: 7; inset-inline: 12px; bottom: 12px; display: grid; grid-template-columns: auto minmax(80px, 1fr) auto; align-items: center; gap: 12px; padding: 9px 12px; border: 1px solid rgba(215,216,209,.9); border-radius: 14px; background: rgba(255,254,251,.94); box-shadow: 0 4px 18px rgba(23,34,29,.16); backdrop-filter: blur(10px); }
.inspection-zoom-label { display: flex; gap: 7px; align-items: baseline; color: var(--muted); font-size: 13px; white-space: nowrap; }
.inspection-zoom-label output { min-width: 29px; color: var(--ink); font-weight: 750; }
.inspection-controls input[type="range"] { min-width: 80px; margin: 0; }
.inspection-controls button { min-height: 36px; padding: 6px 9px; border: 1px solid #cbd6d1; border-radius: 9px; background: white; color: #35665a; font-size: 12px; font-weight: 650; white-space: nowrap; cursor: pointer; }
.inspection-toast { position: absolute; z-index: 9; top: 14px; left: 50%; transform: translateX(-50%); padding: 10px 15px; border: 1px solid #c9ddd4; border-radius: 999px; background: rgba(220,238,232,.96); color: #174f41; font-size: 14px; font-weight: 750; box-shadow: 0 4px 14px rgba(23,34,29,.16); }
.empty-state { max-width: 550px; padding: 42px 22px; text-align: center; }
h1 { margin: 0 0 10px; font: 600 clamp(28px, 5vw, 42px)/1.1 Georgia, serif; }
.empty-state > p { margin: 0 0 24px; color: var(--muted); font-size: 16px; }
.button-row { display: flex; justify-content: center; flex-wrap: wrap; gap: 10px; }
.examples { margin-top: 24px; display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 8px; }
.examples span { width: 100%; color: var(--muted); font-size: 13px; }
.examples button { min-height: 42px; border: 1px solid var(--line); border-radius: 10px; background: white; padding: 8px 11px; color: #31564b; cursor: pointer; }

.button { min-height: 48px; padding: 12px 17px; border-radius: 12px; border: 1px solid transparent; display: inline-flex; align-items: center; justify-content: center; font-weight: 680; cursor: pointer; }
.primary { background: var(--ink); color: white; }
.secondary { background: white; color: var(--ink); border-color: #aeb6b1; }
.quiet { background: #f7f6f1; color: #425149; border-color: var(--line); }
.full { width: 100%; }

.editor, .canvas-wrap { width: 100%; height: 100%; min-height: 640px; }
.canvas-wrap { position: relative; display: grid; place-items: center; overflow: hidden; background: #d8d8d3; touch-action: pan-y; }
canvas { position: absolute; max-width: 100%; max-height: 100%; width: auto; height: auto; }
#photoCanvas { z-index: 1; }
#materialCanvas { z-index: 2; pointer-events: none; }
#maskCanvas { z-index: 3; pointer-events: none; }
#interactionCanvas { z-index: 4; touch-action: none; }

.busy { position: absolute; z-index: 10; inset: 0; padding: 28px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; text-align: center; background: rgba(23,34,29,.9); color: white; }
.busy[hidden] { display: none; }
.busy span { color: #d6ddd9; }
.spinner { width: 34px; height: 34px; border: 3px solid #718078; border-top-color: #dfbd7a; border-radius: 50%; animation: spin .85s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.controls { align-self: start; padding: 16px; }
.status-card { padding: 14px; border-radius: 14px; background: var(--green-soft); }
.status-card strong { display: block; margin-top: 2px; }
.status-card p { margin: 4px 0 0; color: #36574e; font-size: 13px; line-height: 1.4; }
.progress { margin-top: 12px; padding: 10px 6px; display: grid; grid-template-columns: auto 1fr auto 1fr auto; align-items: center; gap: 6px; }
.progress > div { display: grid; justify-items: center; gap: 4px; min-width: 58px; color: #929a96; }
.progress > div span { width: 28px; height: 28px; display: grid; place-items: center; border: 1px solid #b9bfbc; border-radius: 50%; background: white; font-size: 12px; font-weight: 750; }
.progress > div strong { font-size: 11px; font-weight: 650; }
.progress > i { height: 2px; margin-bottom: 18px; background: #d6d9d7; }
.progress > div[data-state="active"] { color: var(--green); }
.progress > div[data-state="active"] span { border-color: var(--green); box-shadow: inset 0 0 0 2px var(--green-soft); }
.progress > div[data-state="completed"] { color: #315f4f; }
.progress > div[data-state="completed"] span { border-color: var(--green); background: var(--green); color: white; }
.stage-panel { margin-top: 12px; padding: 14px; border: 1px solid var(--line); border-radius: 14px; background: white; }
.stage-panel.current-step { border-color: var(--green); box-shadow: inset 0 0 0 1px var(--green); }
.stage-heading { display: flex; align-items: flex-start; gap: 11px; margin-bottom: 12px; }
.stage-heading h2 { margin-bottom: 3px; }
.stage-heading p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.4; }
.stage-number { flex: 0 0 auto; width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: var(--green); color: white; font-weight: 750; }
.optional-stage { background: #fbfbf8; }
.candidate-section { margin-top: 14px; padding-top: 14px; border-top: 1px solid #e7e7e1; }
.candidate-section h3 { margin: 0 0 10px; font-size: 14px; }
.selection-actions { margin-top: 12px; }
h2 { margin: 0 0 11px; font-size: 15px; }
.tool-grid { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 8px; }
.tool { min-height: 52px; border: 1px solid var(--line); border-radius: 11px; background: white; color: var(--ink); cursor: pointer; }
.tool.active { border-color: var(--green); background: var(--green-soft); color: #105b4b; box-shadow: inset 0 0 0 1px var(--green); }
.tool[aria-pressed="true"]::after { content: "✓"; margin-left: 4px; font-weight: 800; }
.tool.danger { color: var(--danger); }
.tool.danger.active { border-color: var(--danger); background: #fae8e6; box-shadow: inset 0 0 0 1px var(--danger); }
.hint { margin: 9px 0 13px; color: var(--muted); font-size: 13px; line-height: 1.45; }
.range-label { margin-top: 14px; display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; }
input[type="range"] { width: 100%; margin: 12px 0 0; accent-color: var(--green); }
.action-stack { display: grid; gap: 9px; }
.optional-copy { margin: 13px 0 8px; color: var(--muted); font-size: 12px; line-height: 1.4; }
.stateful { gap: 8px; }
.state-icon { width: 19px; display: inline-grid; place-items: center; font-weight: 800; }
.stateful[data-state="active"] { background: #e3f1ed; border-color: var(--green); color: #105b4b; box-shadow: inset 0 0 0 1px var(--green); }
.stateful[data-state="completed"] { background: #e5f2e7; border-color: #579268; color: #245d35; }
.stateful[data-state="processing"] { background: #eceeea; color: #4e5953; border-color: #9ca49f; cursor: progress; }
.correction-tools { margin-top: 4px; }
.correction-note { margin: 8px 0 14px; color: var(--muted); font-size: 12px; line-height: 1.4; }
.tool-help { min-height: 19px; margin: 8px 0 2px; color: #4f5e57; font-size: 13px; line-height: 1.4; }
.fit-image-button { display: block; margin: 2px 0 0 auto; padding: 7px 2px; border: 0; background: transparent; color: #35665a; font-size: 12px; font-weight: 650; cursor: pointer; }
.correction-confirm { margin-top: 14px; }

.zones-panel { margin-top: 12px; padding: 14px; border: 1px solid #c9d7d1; border-radius: 14px; background: #f5faf8; }
.zones-heading { display: flex; align-items: center; justify-content: space-between; }
.zones-heading h2 { margin-bottom: 2px; }
.zones-heading p { margin: 0; color: var(--muted); font-size: 12px; }
.zones-list { display: grid; gap: 8px; margin-top: 11px; }
.zone-item { padding: 10px; border: 1px solid var(--line); border-radius: 11px; background: white; }
.zone-item.selected { border-color: var(--green); box-shadow: inset 0 0 0 1px var(--green); }
.zone-summary { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.zone-summary strong { font-size: 13px; }
.zone-summary span { color: var(--muted); font-size: 12px; }
.zone-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; margin-top: 9px; }
.zone-actions button { min-height: 42px; padding: 7px; border: 1px solid var(--line); border-radius: 9px; background: #f8f8f5; color: var(--ink); font-size: 12px; cursor: pointer; }
.zone-actions button.delete-zone { color: var(--danger); border-color: #e2c3c0; }

.material-grid { display: grid; gap: 8px; margin-bottom: 12px; }
.catalog-filters { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 10px; }
.catalog-filters label { display: grid; gap: 4px; font-size: 12px; font-weight: 700; }
.catalog-filters select { min-width: 0; padding: 9px; border: 1px solid var(--line); border-radius: 8px; background: white; color: var(--ink); }
.catalog-status { margin: 0 0 10px; color: #396257; font-size: 12px; }
.texture-pending { width: 52px; height: 52px; display: grid; place-items: center; border-radius: 8px; background: #eee; color: #777; }
.material-choice:disabled { opacity: .6; cursor: not-allowed; }
.account-dialog { width: min(94vw, 480px); max-height: 90vh; overflow: auto; border: 1px solid var(--line); border-radius: 18px; padding: 24px; background: #fff; color: var(--ink); box-shadow: 0 18px 50px #0004; }
.account-dialog::backdrop { background: #14251f99; }
.account-dialog h2 { margin: 0 34px 12px 0; }
.dialog-close { float: right; border: 0; background: transparent; font-size: 28px; cursor: pointer; }
.account-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.account-tabs button { border: 1px solid var(--line); border-radius: 8px; background: #f4f1ea; padding: 8px; cursor: pointer; }
.account-form { display: grid; gap: 11px; }
.account-form label { display: grid; gap: 4px; font-size: 13px; font-weight: 600; }
.account-form input,.account-form select { width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; }
.account-form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.account-form .account-check { display: flex; align-items: flex-start; gap: 8px; line-height: 1.45; }
.account-check input { width: auto; margin-top: 2px; }
.account-message,#projectMessage,#projectsMessage { min-height: 18px; font-size: 13px; color: #365f52; }
.project-list-item { display: grid; gap: 7px; padding: 12px; border: 1px solid var(--line); border-radius: 10px; margin: 8px 0; }
.project-list-item .project-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.project-list-item button { border: 1px solid var(--line); background: #f7f7f4; border-radius: 8px; padding: 7px 10px; cursor: pointer; }
.material-choice { width: 100%; display: grid; grid-template-columns: 52px 1fr auto; align-items: center; gap: 10px; padding: 9px; border: 1px solid var(--line); border-radius: 11px; background: white; color: var(--ink); text-align: left; cursor: pointer; }
.material-choice.selected { border: 2px solid var(--green); background: var(--green-soft); }
.material-choice img { width: 52px; height: 52px; border-radius: 8px; object-fit: cover; }
.material-choice strong, .material-choice small { display: block; }
.material-choice small { margin-top: 3px; color: #396257; font-size: 11px; }
.material-choice b { color: var(--green); font-size: 18px; }
.post-actions { margin-top: 12px; }
.limit-copy { margin: 0; padding: 9px; border-radius: 9px; background: #f7ede0; color: #76532a; text-align: center; font-size: 12px; }
.correction-actions { margin-top: 18px; padding-top: 12px; border-top: 1px solid var(--line); }
.correction-actions h2 { margin-bottom: 5px; color: #7a837e; font-size: 11px; letter-spacing: .1em; text-transform: uppercase; }
.correction-actions > div { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.text-action { min-height: 44px; padding: 8px 5px; border: 0; background: transparent; color: #53615a; font-size: 12px; cursor: pointer; }
.danger-text { color: var(--danger); }
.destructive, .destructive-outline { color: white; background: var(--danger); border-color: var(--danger); }
.destructive-outline { background: white; color: var(--danger); }

.confirm-dialog { width: min(420px, calc(100% - 32px)); padding: 0; border: 0; border-radius: 16px; box-shadow: 0 20px 60px rgba(0,0,0,.28); }
.confirm-dialog::backdrop { background: rgba(13,21,17,.62); }
.confirm-dialog form { padding: 20px; }
.confirm-dialog h2 { margin: 0 0 8px; font-size: 18px; line-height: 1.35; }
.confirm-dialog p { margin: 0 0 18px; color: var(--muted); font-size: 14px; line-height: 1.45; }
.confirm-dialog h2 + .dialog-actions { margin-top: 18px; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }

.candidate-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.candidate { padding: 4px; border: 2px solid transparent; border-radius: 11px; background: #f2f2ed; cursor: pointer; }
.candidate.selected { position: relative; border-color: var(--green); background: var(--green-soft); box-shadow: inset 0 0 0 1px var(--green); }
.candidate.selected::after { content: "✓"; position: absolute; top: 8px; right: 8px; width: 26px; height: 26px; display: grid; place-items: center; border-radius: 50%; background: var(--green); color: white; font-weight: 800; box-shadow: 0 2px 6px rgba(0,0,0,.18); }
.candidate canvas { position: static; display: block; width: 100%; height: auto; border-radius: 7px; }
.candidate span { display: block; padding: 6px 2px 3px; font-size: 12px; font-weight: 650; }

.metrics { margin-top: 16px; padding: 14px; border: 1px solid var(--line); border-radius: 12px; background: #fbfbf8; }
.metrics dl { margin: 0; display: grid; gap: 8px; }
.metrics dl div { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; font-size: 13px; }
.metrics dt { color: var(--muted); }
.metrics dd { margin: 0; font-weight: 680; text-align: right; }
footer { padding: 13px 18px max(13px, env(safe-area-inset-bottom)); text-align: center; color: var(--muted); font-size: 12px; }

/* Flujo guiado: una vista y una tarea principal. */
.flow-progress { width: min(520px, calc(100% - 32px)); margin: 12px auto 0; }
.photo-preview-actions { position: absolute; z-index: 8; inset-inline: 14px; bottom: 14px; display: grid; grid-template-columns: 1fr 1fr; gap: 9px; padding: 10px; border-radius: 14px; background: rgba(255,255,255,.94); box-shadow: 0 8px 24px rgba(23,34,29,.2); backdrop-filter: blur(10px); }
.photo-preview-actions[hidden] { display: none; }
.back-action { min-height: 42px; margin: -4px 0 10px; padding: 7px 2px; border: 0; background: transparent; color: #35665a; font-weight: 700; cursor: pointer; }
.result-heading { padding: 14px; border-radius: 13px; background: var(--green-soft); }
.result-heading strong { display: block; }
.result-heading p, .finished-state p { margin: 4px 0 0; color: #36574e; font-size: 13px; }
.finished-state { padding: 18px; border: 1px solid #bcd4ca; border-radius: 13px; background: #f2f8f5; text-align: center; }
.material-grid { padding-bottom: 74px; }
#materialButton { position: sticky; z-index: 6; bottom: max(8px, env(safe-area-inset-bottom)); box-shadow: 0 8px 24px rgba(23,34,29,.22); }
.selection-actions { position: sticky; z-index: 6; bottom: max(8px, env(safe-area-inset-bottom)); padding: 9px; border-radius: 13px; background: rgba(255,255,255,.95); box-shadow: 0 8px 24px rgba(23,34,29,.18); backdrop-filter: blur(9px); }

body[data-view="photo"] .workspace { grid-template-columns: minmax(0, 900px); justify-content: center; }
body[data-view="photo"] .photo-actions,
body[data-view="photo"] #controls,
body[data-view="material"] .photo-actions,
body[data-view="zone"] .photo-actions { display: none !important; }
body:not([data-view="zone"]) #interactionCanvas { pointer-events: none; }
body[data-view="zone"] #interactionCanvas { pointer-events: auto; touch-action: pan-y; }
body[data-adjusting="true"] #interactionCanvas { touch-action: none; }
body[data-view="result"] #interactionCanvas { pointer-events: auto; touch-action: none; }
body[data-view="result"] .canvas-wrap { touch-action: none; }
body[data-adjusting="true"] .status-card { display: none; }
body[data-view="zone"] #step1 .stage-heading,
body[data-view="zone"] #step2 .stage-heading,
body[data-view="material"] .status-card,
body[data-view="result"] .status-card { display: none; }
body[data-view="zone"] .zones-panel,
body[data-view="material"] .zones-panel { display: none !important; }
body[data-view="result"] #step1,
body[data-view="result"] #step2,
body[data-view="result"] #step3,
body[data-view="result"] #step4 { display: none !important; }
body[data-view="photo"] .correction-actions,
body[data-view="zone"] .correction-actions,
body[data-view="material"] .correction-actions { display: none; }

@media (max-width: 820px) {
  .workspace { display: block; padding: 0; }
  .stage-card, .controls { border-radius: 0; border-inline: 0; box-shadow: none; }
  .flow-progress { margin-block: 6px; padding-block: 6px; }
  .stage-card { min-height: 50dvh; }
  .editor, .canvas-wrap { min-height: 50dvh; }
  .controls { padding-bottom: max(18px, env(safe-area-inset-bottom)); }
  body[data-view="material"] .stage-card { display: none; }
  body[data-view="material"] .controls { min-height: calc(100dvh - 126px); }
  body[data-view="result"] .stage-card { min-height: 52dvh; }
  body[data-view="result"] .editor,
  body[data-view="result"] .canvas-wrap { min-height: 52dvh; }
  body[data-view="zone"] .controls { padding-top: 10px; }
  body[data-view="zone"] .status-card { position: absolute; z-index: 7; left: 12px; right: 12px; top: 10px; background: rgba(220,238,232,.94); backdrop-filter: blur(8px); }
  body[data-view="zone"] .workspace { position: relative; }
  body[data-view="zone"] .stage-panel { margin-top: 0; }
  .material-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .material-choice { grid-template-columns: 46px 1fr auto; }
  .material-choice img { width: 46px; height: 46px; }
}

@media (max-width: 390px) {
  .topbar { padding-inline: 12px; }
  .topbar small { display: none; }
  .state-pill { max-width: 130px; overflow: hidden; text-overflow: ellipsis; }
  .zone-actions { grid-template-columns: 1fr; }
  .inspection-controls { inset-inline: 8px; bottom: 8px; grid-template-columns: auto minmax(56px, 1fr) auto; gap: 8px; padding: 8px; }
  .inspection-controls button { padding-inline: 7px; font-size: 11px; }
}
