/* =============================================================================
   Horneado — Design system
   App web mobile-first (casi no se usa en desktop).
   El HTML lo genera MVC (cascaron); el contenido lo hidrata el JS via fetch.
   ========================================================================== */

/* ----- Tokens ------------------------------------------------------------- */
:root {
  /* Marca */
  --c-purple:        #6C4CE0;
  --c-purple-dark:   #5A3ED0;
  --c-grad-a:        #7B54E8;
  --c-grad-b:        #A25CE0;

  /* Acentos */
  --c-teal:          #17A589;
  --c-teal-soft:     #E5F7F4;
  --c-teal-border:   #16B6BE;
  --c-green:         #2FA84F;
  --c-amber:         #F5A623;
  --c-amber-soft:    #FFF8E6;
  --c-red:           #E8583C;

  /* Neutros */
  --c-bg:            #ECEBFB;
  --c-surface:       #FFFFFF;
  --c-surface-2:     #F3F2FD;
  --c-ink:           #2E2A5B;
  --c-ink-soft:      #6E6A97;
  --c-line:          #E3E1F4;

  /* Formas */
  --r-card:   22px;
  --r-btn:    14px;
  --r-input:  12px;
  --r-pill:   999px;

  --shadow-card: 0 10px 30px -12px rgba(76, 60, 160, .28);
  --shadow-btn:  0 6px 16px -6px rgba(76, 60, 160, .45);

  --space:    16px;
  --header-h: 60px;
  --nav-h:    64px;

  --font: "Fredoka", "Quicksand", "Baloo 2", ui-rounded, "Segoe UI", system-ui,
          -apple-system, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --c-bg:        #17162A;
    --c-surface:   #211F3A;
    --c-surface-2: #2A2848;
    --c-ink:       #EDECFA;
    --c-ink-soft:  #A7A3D0;
    --c-line:      #34325A;
    --c-amber-soft:#2E2A2E;
    --c-teal-soft: #1C3330;
    --shadow-card: 0 10px 30px -12px rgba(0, 0, 0, .5);
  }
}

/* ----- Reset / base ----------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--c-ink);
  background: var(--c-bg);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

h1, h2, h3, h4 { margin: 0 0 .4em; font-weight: 600; line-height: 1.2; }
a { color: var(--c-purple); text-decoration: none; }
button { font-family: inherit; }

/* ----- Layout / app shell -------------------------------------------------- */
.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.app__header {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: var(--header-h);
  padding: env(safe-area-inset-top) var(--space) 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: #fff;
  background: linear-gradient(115deg, var(--c-grad-a), var(--c-grad-b));
  box-shadow: 0 4px 20px -8px rgba(76, 60, 160, .55);
}
.app__header a { color: #fff; }
.app__brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 1.15rem; }
.app__brand-logo { font-size: 1.35rem; }

/* Nav desktop (poco usado): visible solo en pantallas anchas */
.app__nav-desktop { display: none; }

.app__main {
  flex: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: var(--space);
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + var(--space));
}

/* Bottom nav — patron mobile */
.app__nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  background: var(--c-surface);
  border-top: 1px solid var(--c-line);
}
.app__nav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: .72rem;
  color: var(--c-ink-soft);
}
.app__nav a.is-active { color: var(--c-purple); font-weight: 600; }
.app__nav a .ico { font-size: 1.25rem; line-height: 1; }

@media (min-width: 960px) {
  .app__nav { display: none; }
  .app__nav-desktop { display: flex; gap: 18px; align-items: center; }
  .app__nav-desktop a { color: #fff; opacity: .9; font-weight: 500; }
  .app__nav-desktop a.is-active { opacity: 1; text-decoration: underline; }
  .app__main { padding-bottom: var(--space); }
}

/* ----- Componentes ------------------------------------------------------- */

/* Card */
.card {
  display: block;
  background: var(--c-surface);
  border: 2px solid var(--c-line);
  border-radius: var(--r-card);
  padding: 18px;
  box-shadow: var(--shadow-card);
}
.card + .card { margin-top: var(--space); }

/* Card como link (listas) */
a.card { color: inherit; text-decoration: none; -webkit-tap-highlight-color: transparent; }
a.card:active { transform: scale(.995); }
.card__title { display: flex; align-items: center; gap: 8px; font-size: 1.05rem; }

.card--amber { background: var(--c-amber-soft); border-color: var(--c-amber); }
.card--teal  { background: var(--c-teal-soft);  border-color: var(--c-teal-border); }

/* Balance */
.balance { text-align: center; }
.balance__label {
  font-size: .8rem; letter-spacing: .08em; text-transform: uppercase;
  color: var(--c-ink-soft); font-weight: 600;
}
.balance__amount { font-size: 2.1rem; font-weight: 700; margin: 4px 0 14px; }
.balance--amber .balance__amount { color: var(--c-amber); }
.balance--teal  .balance__amount { color: var(--c-teal-border); }
.balance__actions { display: flex; gap: 10px; justify-content: center; }

/* Botones */
.btn {
  --btn-bg: var(--c-purple);
  appearance: none;
  border: 0;
  border-radius: var(--r-btn);
  padding: 12px 18px;
  min-height: 44px;               /* target tactil */
  font-size: 1rem;
  font-weight: 600;
  color: #fff;
  background: var(--btn-bg);
  box-shadow: var(--shadow-btn);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: transform .06s ease, filter .15s ease;
}
.btn:active { transform: translateY(1px) scale(.99); }
.btn:disabled { opacity: .55; box-shadow: none; cursor: default; }
.btn--block { display: flex; width: 100%; }
.btn--teal   { --btn-bg: var(--c-teal); }
.btn--red    { --btn-bg: var(--c-red); }
.btn--ghost  {
  --btn-bg: transparent;
  color: var(--c-purple);
  box-shadow: none;
  border: 2px solid var(--c-line);
}

/* Segmented / pill toggle */
.segmented { display: flex; gap: 10px; }
.segmented__opt {
  flex: 1;
  border: 0;
  border-radius: var(--r-btn);
  padding: 12px;
  font-weight: 600;
  color: var(--c-ink-soft);
  background: var(--c-surface-2);
  cursor: pointer;
}
.segmented__opt.is-active { color: #fff; background: var(--c-purple); }

/* Inputs */
.field { display: block; margin-bottom: 12px; }
.field__label {
  display: block;
  font-size: .78rem; letter-spacing: .06em; text-transform: uppercase;
  color: var(--c-ink-soft); font-weight: 600; margin-bottom: 6px;
}
.input {
  width: 100%;
  border: 1px solid var(--c-line);
  border-radius: var(--r-input);
  padding: 14px;
  font-size: 1.05rem;
  color: var(--c-ink);
  background: var(--c-surface-2);
}
.input:focus { outline: 2px solid var(--c-purple); outline-offset: 1px; }

/* Info strip (cotizacion) */
.strip {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--c-surface-2);
  border-radius: var(--r-input);
  padding: 10px 12px;
  font-size: .9rem;
  color: var(--c-ink-soft);
}

/* Note / resultado vacio */
.note {
  text-align: center;
  color: var(--c-ink-soft);
  background: var(--c-surface-2);
  border-radius: var(--r-input);
  padding: 14px;
}

/* Chips */
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 10px;
  border-radius: var(--r-pill);
  font-size: .8rem; font-weight: 600;
  background: var(--c-surface-2); color: var(--c-ink-soft);
}

/* Encabezado de detalle */
.detail-head {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: var(--space);
}
.detail-head__avatar { font-size: 2rem; }
.detail-head__name { font-size: 1.2rem; font-weight: 700; }
.detail-head__meta { font-size: .82rem; color: var(--c-ink-soft); }

/* Utilidades */
.stack > * + * { margin-top: 14px; }
.row { display: flex; gap: 10px; align-items: center; }
.row--between { justify-content: space-between; }
.text-soft { color: var(--c-ink-soft); }
.hidden { display: none !important; }

/* Estados de carga */
.skeleton {
  border-radius: var(--r-input);
  background: linear-gradient(90deg, var(--c-surface-2) 25%, var(--c-line) 37%, var(--c-surface-2) 63%);
  background-size: 400% 100%;
  animation: skeleton 1.2s ease-in-out infinite;
  min-height: 1em;
}
@keyframes skeleton { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }

/* Toast */
.toast-host {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 12px);
  z-index: 50;
  display: flex; flex-direction: column; gap: 8px;
  width: min(92vw, 420px);
}
.toast {
  background: var(--c-ink); color: #fff;
  border-radius: var(--r-btn);
  padding: 12px 14px;
  box-shadow: var(--shadow-card);
  font-size: .92rem;
}
.toast--error { background: var(--c-red); }
.toast--ok    { background: var(--c-teal); }

/* ----- Formularios / grids -------------------------------------------------- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }

.btn--sm { min-height: 36px; padding: 6px 12px; font-size: .9rem; box-shadow: none; }
.btn--xs { min-height: 30px; padding: 2px 8px; font-size: .85rem; box-shadow: none; }

.color-pick {
  width: 44px; height: 44px; padding: 0;
  border: 2px solid var(--c-line); border-radius: 10px; background: none; cursor: pointer;
  flex: none;
}

/* ----- Editor de piezas --------------------------------------------------- */
.item-row {
  display: grid;
  grid-template-columns: auto auto 1fr;
  grid-template-areas: "ord color grid" "act act act";
  gap: 8px; align-items: center;
  padding: 10px; border: 1px solid var(--c-line); border-radius: var(--r-input);
  background: var(--c-surface-2);
}
.item-row__ord {
  grid-area: ord;
  width: 26px; height: 26px; border-radius: 999px;
  display: grid; place-items: center;
  background: var(--c-purple); color: #fff; font-weight: 700; font-size: .85rem;
}
.item-row .color-pick { grid-area: color; }
.item-row__grid {
  grid-area: grid;
  /* mobile: [cant][id] / [ancho][prof][alto] */
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px;
}
.item-row__grid .input { padding: 8px 10px; font-size: .95rem; min-width: 0; }
.item-row__grid .input:nth-child(1) { grid-column: span 1; } /* cantidad */
.item-row__grid .input:nth-child(2) { grid-column: span 2; } /* id */
.item-row__actions { grid-area: act; display: flex; gap: 6px; justify-content: flex-end; }

@media (min-width: 560px) {
  .item-row { grid-template-columns: auto auto 1fr auto; grid-template-areas: "ord color grid act"; }
  .item-row__grid { grid-template-columns: 64px 72px repeat(3, 1fr); }
  .item-row__grid .input:nth-child(2) { grid-column: span 1; }
}

/* ----- Tabla de resultado ------------------------------------------------- */
.result-table { border: 2px solid var(--c-line); border-radius: var(--r-card); overflow: hidden; background: var(--c-surface); }
.result-row { display: grid; grid-template-columns: 1fr; border-top: 1px solid var(--c-line); }
.result-row:first-child { border-top: 0; }
.result-row--head { font-weight: 700; background: var(--c-surface-2); }
.result-row--head .result-cell { padding: 10px 14px; }
.result-cell { padding: 14px; border-top: 1px dashed var(--c-line); }
.result-cell:first-child { border-top: 0; }
.result-cell--visual { display: flex; flex-direction: column; align-items: center; gap: 6px; background: var(--c-surface-2); }

.result-nivel-tag {
  align-self: flex-start;
  font-size: .78rem; font-weight: 700; color: var(--c-purple);
  background: var(--c-surface); border: 1px solid var(--c-line);
  padding: 2px 10px; border-radius: 999px;
}
.result-frente { font-size: .72rem; color: var(--c-ink-soft); letter-spacing: .05em; }
.nivel-svg { width: 100%; max-width: 320px; height: auto; }

.result-text {
  margin: 0; white-space: pre-wrap; word-break: break-word;
  font-family: ui-monospace, "SFMono-Regular", "Consolas", monospace;
  font-size: .82rem; line-height: 1.5; color: var(--c-ink);
}

@media (min-width: 640px) {
  .result-row { grid-template-columns: minmax(240px, 1fr) 1.2fr; }
  .result-cell { border-top: 0; }
  .result-row--head { grid-template-columns: minmax(240px, 1fr) 1.2fr; }
}
