/*
 * Editor chrome for the quote builder — everything the client never sees.
 * The document itself is styled by document.css and is deliberately untouched
 * by anything here, so the preview cannot drift from the print output.
 */

@font-face {
  font-family: "beIN UI";
  src: url("../fonts/bein-normal.ttf") format("truetype");
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: "beIN UI";
  src: url("../fonts/bein-black.woff2") format("woff2"),
       url("../fonts/bein-black.woff") format("woff");
  font-weight: 900;
  font-display: swap;
}

:root {
  --brand: #7152a2;
  --brand-dark: #302044;
  --brand-deep: #181020;
  --brand-soft: #eee9f6;
  --brand-pale: #f7f4fa;
  --ink: #1b1720;
  --muted: #66606c;
  --line: #dfd9e7;
  --surface: #fbfafc;
  --radius: .85rem;
  --shadow-sm: 0 8px 24px -18px rgba(48, 32, 68, .45);
  --mobile-actions-h: 4.75rem;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--surface);
  color: var(--ink);
  font-family: "beIN UI", Tahoma, Arial, sans-serif;
  font-synthesis: none;
  line-height: 1.7;
  overscroll-behavior-y: contain;
}

button, input, textarea { font: inherit; color: inherit; }
button, a { touch-action: manipulation; }
button { -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 3px solid var(--brand); outline-offset: 2px; }

/* ---- login ---------------------------------------------------------------- */
.login-body {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background:
    radial-gradient(circle at 15% 12%, rgba(203, 188, 226, .3), transparent 42%),
    var(--brand-dark);
}
.login-card {
  width: min(100%, 25rem);
  border-radius: 1.35rem;
  background: #fff;
  padding: 2rem 1.75rem 2.25rem;
  box-shadow: 0 40px 90px -50px rgba(24, 16, 32, .85);
}
.login-logo { display: block; width: 9.5rem; margin-inline: auto; filter: brightness(0) saturate(100%) invert(31%) sepia(24%) saturate(1300%) hue-rotate(232deg) brightness(92%) contrast(88%); }
.login-strip {
  height: .55rem;
  margin: 1.15rem -1.75rem 1.5rem;
  background: url("../brand/pattern-line-purple.png") center / auto 100% repeat-x;
  opacity: .55;
}
.login-card h1 { margin: 0; color: var(--brand-dark); font-size: 1.35rem; font-weight: 900; text-align: center; }
.login-sub { margin: .35rem 0 1.5rem; color: var(--muted); font-size: .9rem; text-align: center; }
.login-card label { display: block; margin-bottom: .35rem; color: var(--brand-dark); font-size: .85rem; font-weight: 900; }
.login-card input {
  width: 100%;
  min-height: 3rem;
  margin-bottom: 1.1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--brand-pale);
  padding: 0 .9rem;
}
.login-card input:focus { border-color: var(--brand); background: #fff; }
.login-card button {
  width: 100%;
  min-height: 3rem;
  border: 0;
  border-radius: var(--radius);
  background: var(--brand);
  color: #fff;
  font-weight: 900;
  cursor: pointer;
  transition: background-color .2s ease;
}
.login-card button:hover { background: var(--brand-dark); }
.login-error {
  margin: 0 0 1.1rem;
  border: 1px solid #f0c7c7;
  border-inline-start: 4px solid #b3261e;
  border-radius: var(--radius);
  background: #fdf3f3;
  padding: .75rem .9rem;
  color: #7c1d16;
  font-size: .85rem;
  line-height: 1.7;
}
.login-error code { display: block; margin-top: .4rem; font-size: .78rem; word-break: break-all; }

/* ---- app shell ------------------------------------------------------------ */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: .6rem;
  border-block-end: 1px solid var(--line);
  background: linear-gradient(135deg, var(--brand-dark), var(--brand-deep));
  padding: .7rem clamp(.75rem, 2vw, 1.25rem);
  color: #fff;
}
.header-brand { display: flex; align-items: center; gap: .7rem; min-width: 0; }
.header-brand img { width: 6.5rem; flex: none; }
.header-brand span { display: none; color: rgba(255, 255, 255, .72); font-size: .78rem; font-weight: 900; }
.app-header .spacer { flex: 1; }
.app-actions { display: flex; align-items: center; gap: .55rem; }
.app-user { display: inline-flex; align-items: center; gap: .45rem; color: rgba(255, 255, 255, .68); font-size: .82rem; white-space: nowrap; }
.app-user a { border-inline-start: 1px solid rgba(255, 255, 255, .2); padding-inline-start: .45rem; color: #e2d7f1; }

.seg { display: inline-flex; border: 1px solid rgba(255, 255, 255, .22); border-radius: 999px; padding: .2rem; }
.seg button {
  min-height: 2.2rem;
  border: 0;
  border-radius: 999px;
  background: transparent;
  padding: 0 .95rem;
  color: rgba(255, 255, 255, .7);
  font-size: .85rem;
  font-weight: 900;
  cursor: pointer;
  transition: background-color .2s ease, color .2s ease;
}
.seg button[aria-pressed="true"] { background: #fff; color: var(--brand-dark); }

.btn {
  min-height: 2.5rem;
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  border: 1px solid rgba(255, 255, 255, .25);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, .08);
  padding: 0 .9rem;
  color: #fff;
  font-size: .85rem;
  font-weight: 900;
  cursor: pointer;
  transition: background-color .2s ease, border-color .2s ease, transform .15s ease;
}
.btn:hover { background: rgba(255, 255, 255, .18); }
.btn:active { transform: translateY(1px); }
.btn--primary { border-color: transparent; background: #fff; color: var(--brand-dark); }
.btn--primary:hover { background: #e9e2f4; }
.btn-icon {
  width: 1.1rem;
  height: 1.1rem;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app {
  display: grid;
  grid-template-columns: minmax(0, 27rem) minmax(0, 1fr);
  align-items: start;
  min-height: calc(100dvh - 3.9rem);
}

/* ---- editor --------------------------------------------------------------- */
.editor {
  position: sticky;
  top: 3.9rem;
  max-height: calc(100dvh - 3.9rem);
  overflow-y: auto;
  border-inline-end: 1px solid var(--line);
  background: #f7f5f9;
  padding: 1.1rem clamp(.8rem, 1.6vw, 1.15rem) 4rem;
}
.editor-intro {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: .25rem .75rem;
  padding: .25rem .15rem .9rem;
}
.eyebrow { display: block; color: var(--brand); font-size: .7rem; font-weight: 900; }
.editor-intro h1 { margin: 0; color: var(--brand-dark); font-size: 1.25rem; line-height: 1.45; }
.editor-intro p { grid-column: 1 / -1; margin: .2rem 0 0; color: var(--muted); font-size: .76rem; line-height: 1.6; }
.draft-status {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  border: 1px solid #d7cde5;
  border-radius: 999px;
  background: #fff;
  padding: .25rem .55rem;
  color: var(--brand);
  font-size: .68rem;
  font-weight: 900;
  white-space: nowrap;
}
.draft-status::before { content: ""; width: .4rem; height: .4rem; border-radius: 50%; background: #2f8f63; }

.form-section {
  display: block;
  margin: 0 0 .85rem;
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: #fff;
  padding: 1rem;
  box-shadow: var(--shadow-sm);
}
.section-heading { display: flex; align-items: flex-start; gap: .7rem; margin-bottom: 1rem; }
.section-number {
  width: 2rem;
  height: 2rem;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: .65rem;
  background: var(--brand-soft);
  color: var(--brand);
  font-size: .82rem;
  font-weight: 900;
}
.section-heading h2 { margin: 0; color: var(--brand-dark); font-size: 1rem; line-height: 1.5; }
.section-heading p { margin: .1rem 0 0; color: var(--muted); font-size: .73rem; line-height: 1.55; }
.form-extra { margin-top: .75rem; }

.form-section--optional { padding: 0; overflow: hidden; }
.form-section--optional > summary {
  min-height: 4rem;
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .85rem 1rem;
  list-style: none;
  cursor: pointer;
}
.form-section--optional > summary::-webkit-details-marker { display: none; }
.summary-copy { flex: 1; min-width: 0; }
.summary-copy strong, .summary-copy small { display: block; }
.summary-copy strong { color: var(--brand-dark); font-size: .95rem; }
.summary-copy small { color: var(--muted); font-size: .7rem; }
.form-section--optional > summary > svg { width: 1.3rem; height: 1.3rem; fill: none; stroke: var(--brand); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; transition: transform .2s ease; }
.form-section--optional[open] > summary > svg { transform: rotate(180deg); }
.optional-content { border-block-start: 1px solid var(--line); padding: 1rem; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.grid--one { grid-template-columns: 1fr; }

.field { display: block; min-width: 0; }
.field > span {
  display: block;
  margin-bottom: .25rem;
  color: var(--muted);
  font-size: .77rem;
  font-weight: 900;
  letter-spacing: .01em;
}
.field > span small { color: #8e8499; font-size: .68rem; font-weight: 400; }
.field input, .field textarea, .row input {
  width: 100%;
  min-height: 2.85rem;
  border: 1px solid var(--line);
  border-radius: .6rem;
  background: var(--brand-pale);
  padding: .5rem .75rem;
  font-size: .9rem;
  transition: border-color .18s ease, background-color .18s ease, box-shadow .18s ease;
}
.field textarea { min-height: 6rem; resize: vertical; line-height: 1.8; }
.field input:focus, .field textarea:focus, .row input:focus { border-color: var(--brand); background: #fff; box-shadow: 0 0 0 3px rgba(113, 82, 162, .1); outline: none; }
.field input::placeholder, .row input::placeholder { color: #a49daf; }

.hint { margin: .4rem 0 0; color: var(--muted); font-size: .74rem; line-height: 1.65; }
.hint--top {
  margin: 0 0 1.5rem;
  border: 1px solid var(--line);
  border-inline-start: 3px solid var(--brand);
  border-radius: .6rem;
  background: var(--brand-pale);
  padding: .7rem .9rem;
  font-size: .78rem;
}
.hint--top b { color: var(--brand-dark); }

/* ---- repeatable rows ------------------------------------------------------ */
.rows { display: grid; gap: .55rem; }
.row {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  padding: .75rem;
}
.row-head { display: flex; align-items: center; gap: .5rem; margin-bottom: .7rem; }
.row-n {
  flex: none;
  width: 1.8rem;
  height: 1.8rem;
  display: grid;
  place-items: center;
  border-radius: .4rem;
  background: var(--brand-soft);
  color: var(--brand);
  font-size: .72rem;
  font-weight: 900;
}
.row-kind { flex: 1; min-width: 0; color: var(--brand-dark); font-size: .82rem; }
.row-actions { display: flex; align-items: center; gap: .35rem; }
.row-drag, .row-del, .line-del {
  flex: none;
  width: 2.25rem;
  height: 2.25rem;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: .45rem;
  background: #fff;
  color: var(--muted);
  cursor: pointer;
  transition: color .18s ease, border-color .18s ease;
}
.row-drag svg, .row-del svg, .line-del svg { width: 1.1rem; height: 1.1rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.row-drag:hover { color: var(--brand); border-color: var(--brand); }
.row-del:hover, .line-del:hover { color: #b3261e; border-color: #e3b7b3; }
.row-drag:disabled { opacity: .35; cursor: not-allowed; }
.row > .field { margin-top: 0; }
.row-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; margin-top: .6rem; }

.lines { display: grid; gap: .45rem; margin-top: .7rem; }
.line { display: flex; gap: .35rem; }
.line input { min-height: 2.65rem; font-size: .86rem; }
.line-add {
  justify-self: start;
  min-height: 2.65rem;
  border: 1px dashed var(--line);
  border-radius: .45rem;
  background: transparent;
  padding: 0 .7rem;
  color: var(--brand);
  font-size: .78rem;
  font-weight: 900;
  cursor: pointer;
}
.line-add:hover { border-color: var(--brand); background: var(--brand-pale); }

.add {
  width: 100%;
  min-height: 2.9rem;
  margin-top: .55rem;
  border: 1px dashed var(--brand);
  border-radius: var(--radius);
  background: var(--brand-pale);
  color: var(--brand);
  font-weight: 900;
  cursor: pointer;
}
.add:hover { background: var(--brand-soft); }

/* ---- running totals ------------------------------------------------------- */
.summary {
  position: sticky;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  margin: 1.5rem -1.15rem -4rem;
  border-block-start: 1px solid var(--line);
  background: var(--line);
}
.summary div { background: #fff; padding: .55rem .5rem; text-align: center; }
.summary span { display: block; color: var(--muted); font-size: .68rem; font-weight: 900; }
.summary b { color: var(--brand-dark); font-size: .92rem; font-weight: 900; direction: ltr; unicode-bidi: isolate; }
.summary div:last-child { background: var(--brand); }
.summary div:last-child span { color: #dccef0; }
.summary div:last-child b { color: #fff; }

/* ---- preview -------------------------------------------------------------- */
.preview {
  position: sticky;
  top: 3.9rem;
  height: calc(100dvh - 3.9rem);
  overflow: hidden auto;
  background: #eae7ef;
  padding: 1rem .75rem 3rem;
}
.stage { display: flex; justify-content: center; }
.sheets { transform-origin: top center; }
.sheets .sheet { box-shadow: 0 18px 45px -25px rgba(24, 16, 32, .55); }
.sheets .sheet + .sheet { margin-top: 1rem; }

/* ---- mobile --------------------------------------------------------------- */
.panel-switch { display: none; }

@media (max-width: 62rem) {
  .panel-switch { display: inline-flex; }
  .app { grid-template-columns: 1fr; }

  .app-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: .55rem .75rem;
    padding: max(.6rem, env(safe-area-inset-top)) max(.75rem, env(safe-area-inset-right)) .65rem max(.75rem, env(safe-area-inset-left));
  }
  .header-brand { min-width: 0; }
  .header-brand img { width: 5.25rem; }
  .header-brand span { display: inline; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .app-header .spacer { display: none; }
  .panel-switch { grid-column: 1 / -1; width: 100%; min-height: 2.85rem; }
  .app-header .seg button { min-height: 2.45rem; flex: 1; padding: 0 .75rem; font-size: .82rem; }
  .app-user { justify-self: end; font-size: .74rem; }
  .user-name { display: none; }

  .app-actions {
    position: fixed;
    z-index: 40;
    right: 0;
    bottom: 0;
    left: 0;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: .5rem;
    border-block-start: 1px solid var(--line);
    background: rgba(255, 255, 255, .97);
    padding: .5rem max(.75rem, env(safe-area-inset-right)) max(.5rem, env(safe-area-inset-bottom)) max(.75rem, env(safe-area-inset-left));
    box-shadow: 0 -12px 30px -22px rgba(48, 32, 68, .55);
    backdrop-filter: blur(14px);
  }
  .app-actions .btn {
    min-width: 0;
    min-height: 3.25rem;
    justify-content: center;
    border-color: var(--line);
    background: #fff;
    padding: 0 .45rem;
    color: var(--brand-dark);
    font-size: .76rem;
  }
  .app-actions .btn:hover { background: var(--brand-pale); }
  .app-actions .btn--primary { border-color: var(--brand); background: var(--brand); color: #fff; }
  .app-actions .btn--primary:hover { background: var(--brand-dark); }
  .app-actions .btn-icon { width: 1.15rem; height: 1.15rem; }

  .app { padding-bottom: calc(var(--mobile-actions-h) + env(safe-area-inset-bottom)); }
  .editor, .preview { position: static; max-height: none; height: auto; border-inline-end: 0; }
  .editor { padding: .8rem .75rem 1.25rem; }
  .preview { min-height: calc(100dvh - 7rem); padding-bottom: 2rem; }
  .summary {
    position: static;
    grid-template-columns: repeat(2, 1fr);
    gap: 1px;
    margin: 1rem 0 0;
    border: 1px solid var(--line);
    border-radius: 1rem;
    overflow: hidden;
  }
  .summary div { min-height: 3.5rem; display: grid; align-content: center; }
  body[data-view="preview"] .editor { display: none; }
  /*
   * The preview is parked off-screen rather than display:none. The paginator
   * decides page breaks by measuring the live sheets, and a display:none
   * subtree has no box to measure — hiding it that way silently produced a
   * mis-paginated document whenever the phone was on the data tab.
   */
  body[data-view="form"] .preview {
    position: fixed;
    top: 0;
    inset-inline-start: -300vw;
    width: 100vw;
    height: 100vh;
    visibility: hidden;
    pointer-events: none;
  }
}

@media (max-width: 30rem) {
  .grid { grid-template-columns: 1fr; }
  .editor-intro h1 { font-size: 1.2rem; }
  .form-section { padding: .9rem; }
  .form-section--optional { padding: 0; }
  .field input, .field textarea, .row input { min-height: 3rem; font-size: 1rem; }
  .row-drag, .row-del, .line-del { width: 2.75rem; height: 2.75rem; }
  .row-head { gap: .4rem; }
  .row-actions { gap: .3rem; }
  .line input { min-height: 2.75rem; }
  .add, .line-add { min-height: 2.75rem; }
  .hint { font-size: .78rem; }
  .app-actions .btn { flex-direction: column; gap: .08rem; line-height: 1.2; }
}

@media (max-width: 22rem) {
  .row-grid { grid-template-columns: 1fr; }
  .header-brand span { display: none; }
}

@media (max-width: 62rem) and (max-height: 30rem) and (orientation: landscape) {
  .app-header {
    grid-template-columns: auto minmax(15rem, 1fr) auto;
    gap: .65rem;
    padding: .4rem max(.75rem, env(safe-area-inset-right)) .4rem max(.75rem, env(safe-area-inset-left));
  }
  .header-brand img { width: 4.5rem; }
  .header-brand span { display: none; }
  .panel-switch { grid-column: 2; grid-row: 1; min-height: 2.5rem; }
  .app-header .seg button { min-height: 2.1rem; }
  .app-user { grid-column: 3; grid-row: 1; }
  .app-actions { padding-top: .35rem; padding-bottom: max(.35rem, env(safe-area-inset-bottom)); }
  .app-actions .btn { min-height: 2.8rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}

/* ---- print ---------------------------------------------------------------- */
@page { size: A4; margin: 0; }

@media print {
  html, body { width: auto; height: auto; margin: 0; padding: 0; background: #fff; }
  .app-header, .editor, .panel-switch { display: none !important; }
  .app { display: block; min-height: 0; }
  .preview {
    position: static !important;
    inset-inline-start: auto !important;
    width: auto !important;
    height: auto !important;
    overflow: visible;
    padding: 0;
    background: #fff;
    display: block !important;
    visibility: visible !important;
  }
  .stage { display: block; height: auto !important; }
  .sheets { transform: none !important; }
  .sheets .sheet {
    break-after: page;
    page-break-after: always;
    margin: 0 !important;
    box-shadow: none;
  }
  .sheets .sheet:last-child { break-after: auto; page-break-after: auto; }
  .sheet, .sheet * {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}
