/* Title font, stored with the app so it works offline (SIL Open Font License, see /fonts/OFL.txt). */
@font-face {
  font-family: "Libre Caslon Text";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("/fonts/libre-caslon-text-700-latin.woff2") format("woff2");
}

:root {
  --paper: #edf4fb;
  --panel: #f9fcff;
  --ink: #26252d;
  --muted: #5d6a78;
  --line: #d2e1ee;
  --accent: #2f4b7c;
  --accent-ink: #ffffff;
  --accent-soft: #dceaf7;
  --sienna: #b4532f;
  --stage: #4b4b50;
  --radius: 10px;
  --topbar-h: 52px;
  --viewbar-h: 48px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, select, input { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }

/* ---------- Top bar ---------- */
.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0 16px;
  padding-top: env(safe-area-inset-top);
  /* the watercolor wash, stretched to fill the whole bar */
  background: var(--panel) url('/img/title-bg.png') center / 100% 100% no-repeat;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1, .gate-form h1 {
  font-family: "Libre Caslon Text", "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  font-weight: 700; color: #2b4470; letter-spacing: .2px;
}
.brand h1 { font-size: 22px; margin: 0; white-space: nowrap; line-height: 1.1; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.app-version { margin-left: 34px; font-size: 13px; color: #3a5275; font-weight: 500; white-space: nowrap; }
.topbar .account-name { color: #2f4566; }
.topbar .account { border-left-color: rgba(43,68,112,.25); }

/* ---------- Account / sign-in ---------- */
.account { display: flex; align-items: center; gap: 8px; flex-shrink: 0; margin-left: 6px; padding-left: 12px; border-left: 1px solid var(--line); }
.account-name { background: none; border: 0; padding: 4px 2px; color: var(--muted); font-size: 13.5px; cursor: pointer; white-space: nowrap; max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.account-name:hover { color: var(--ink); text-decoration: underline; }
body.locked #app { display: none; }
.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; background: var(--paper); z-index: 10; }
.gate-form { width: 100%; max-width: 360px; display: grid; gap: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; box-shadow: 0 8px 30px rgba(30,60,100,.08); }
.gate-brand { display: flex; align-items: center; gap: 12px; }
.gate-logo { width: 40px; height: 40px; flex-shrink: 0; object-fit: fill; border-radius: 10px; box-shadow: 0 1px 3px rgba(30,60,100,.25); }
.gate-form h1 { font-size: 1.35rem; margin: 0; }
.gate-form p { margin: 0; color: var(--muted); }
.gate-form input { min-height: 44px; padding: 0 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.gate-form .btn { min-height: 44px; }
.pw-field { position: relative; display: grid; }
.pw-field input { padding-right: 46px; }
.pw-toggle {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 38px; height: 38px; display: grid; place-items: center;
  border: 0; border-radius: 6px; background: transparent; color: var(--muted); cursor: pointer;
}
.pw-toggle:hover { color: var(--ink); background: var(--accent-soft); }
.pw-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.pw-toggle svg { width: 20px; height: 20px; }
.pw-toggle .eye-closed, .pw-toggle[aria-pressed="true"] .eye-open { display: none; }
.pw-toggle[aria-pressed="true"] .eye-closed { display: block; }
/* hide Edge's built-in reveal button so there is only one eye */
.pw-field input::-ms-reveal { display: none; }
@media (max-width: 700px) {
  /* Phones: the account line moves to its own row under the title. */
  .topbar { height: auto; min-height: var(--topbar-h); flex-wrap: wrap; row-gap: 2px; padding-bottom: 6px; padding-top: calc(6px + env(safe-area-inset-top)); }
  .topbar-actions { display: contents; }
  .topbar-actions > .btn { order: 1; }
  .brand { flex: 1; }
  .topbar::after { content: ''; order: 1; flex-basis: 100%; } /* line break after the camera/open buttons */
  #btnInstall { order: 2; margin-left: auto; min-height: 28px; padding: 0 10px; font-size: 13.5px; }
  .account { order: 3; border-left: 0; padding-left: 0; margin-left: 8px; }
  #btnInstall[hidden] + .account { margin-left: auto; }
  .account .btn.small { min-height: 28px; }
}
@media (max-width: 480px) {
  .brand img { display: none; }
  .app-version { margin-left: 12px; font-size: 12px; }
  .brand h1 { font-size: 19px; }
  .topbar { gap: 8px; padding-left: 12px; padding-right: 12px; }
  .topbar-actions > .btn { padding: 0 10px; }
}
.gate-error { color: #b42318 !important; min-height: 1.2em; font-size: 14px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 8px;
  background: #fff; cursor: pointer; user-select: none; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn:hover:not(:disabled) { border-color: #a9c3da; background: #f1f7fd; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { background: #263e68; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 13.5px; }
.btn.light { color: #fff; border-color: rgba(255,255,255,.35); }
.btn:focus-visible, .seg label:has(input:focus-visible), .tabs button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn { border: 0; background: transparent; font-size: 18px; width: 36px; height: 36px; border-radius: 8px; cursor: pointer; }
.icon-btn:hover { background: #e2eef9; }

/* ---------- View bar ---------- */
.viewbar {
  height: var(--viewbar-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
  overflow-x: auto; scrollbar-width: none;
}
.viewbar::-webkit-scrollbar { display: none; }

/* Segmented control built from radio inputs */
.seg { display: inline-flex; background: #dde9f5; border-radius: 9px; padding: 3px; gap: 2px; flex-shrink: 0; }
.seg label { position: relative; cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 6px 12px; border-radius: 7px; font-size: 14px; white-space: nowrap; color: var(--muted); }
.seg input:checked + span { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 550; }
.seg.small span { padding: 5px 10px; font-size: 13.5px; }
.seg.stack { display: flex; flex-direction: column; align-items: stretch; }
.seg.stack span { white-space: normal; }
.seg.dark { background: rgba(255,255,255,.12); }
.seg.dark span { color: rgba(255,255,255,.75); }
.seg.dark input:checked + span { background: rgba(255,255,255,.95); color: #111; }

.toggle { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; flex-shrink: 0; font-size: 14px; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }

/* ---------- Workspace ---------- */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  grid-template-rows: minmax(0, 1fr) auto;
  grid-template-areas: "stage panel" "info panel";
  height: calc(100dvh - var(--topbar-h) - var(--viewbar-h) - env(safe-area-inset-top));
}
.stage {
  grid-area: stage;
  position: relative;
  background: var(--stage);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  min-height: 0;
}
.stage-info {
  grid-area: info;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 14px;
  padding: 8px 16px;
  background: #3d3d42; color: #e8edf2; font-size: 13.5px;
}
.legend { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.legend button {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center;
  width: 40px; height: 34px; border-radius: 6px; border: 2px solid transparent; cursor: pointer;
  font-size: 11px; line-height: 1.1; padding: 0;
}
.legend button.on { border-color: #ff9a3d; box-shadow: 0 0 0 1px #ff9a3d; }
.legend .legend-clear { background: rgba(255,255,255,.12); color: #fff; border-color: rgba(255,255,255,.3); font-size: 12px; }

.panel {
  grid-area: panel;
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  display: flex; flex-direction: column;
}
.tabs {
  display: flex; flex-wrap: wrap; gap: 2px; padding: 8px 10px 0;
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; background: var(--panel); z-index: 1;
}
.tabs button {
  border: 0; background: transparent; padding: 8px 10px; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent; font-size: 14px;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--sienna); font-weight: 600; }
.tab-panel { padding: 14px 16px 24px; display: flex; flex-direction: column; gap: 14px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .field .label { font-size: 13.5px; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
.field output { color: var(--ink); font-variant-numeric: tabular-nums; font-weight: 600; }
.field.row { flex-direction: row; align-items: center; gap: 8px; }
.field.row input { width: 80px; }
.field.buttons { flex-direction: row; flex-wrap: wrap; gap: 8px; }
select, input[type="number"] {
  min-height: 38px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
input[type="range"] { width: 100%; accent-color: var(--accent); height: 28px; margin: 0; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.hint { margin: 0; font-size: 13px; color: var(--muted); }
details { border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; }
details[open] { display: flex; flex-direction: column; gap: 12px; padding-bottom: 12px; }
summary { cursor: pointer; font-size: 14px; }
.tips { background: #e6f0fa; border-radius: 8px; padding: 10px 12px; font-size: 13.5px; }
.tips h4 { margin: 0 0 4px; font-size: 13.5px; }
.tips ul { margin: 0; padding-left: 18px; }
.tips li { margin: 3px 0; }
#actualOptions { display: flex; flex-direction: column; gap: 12px; }

.swatches { display: flex; gap: 8px; }
.swatches label { cursor: pointer; }
.swatches input { position: absolute; opacity: 0; pointer-events: none; }
.swatches span { display: block; width: 30px; height: 30px; border-radius: 50%; border: 1px solid rgba(0,0,0,.25); }
.swatches input:checked + span { box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--accent); }

/* ---------- Stage content ---------- */
.empty { color: #eef3f8; text-align: center; max-width: 440px; padding: 24px; }
.empty h2 { font-weight: 600; font-size: 22px; margin: 0 0 12px; }
.flow { text-align: left; display: inline-block; margin: 0 0 18px; padding-left: 22px; color: #d3dce6; }
.flow li { margin: 3px 0; }
.empty-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.empty .btn:not(.primary) { background: rgba(255,255,255,.08); color: #fff; border-color: rgba(255,255,255,.3); }
.fine { font-size: 12.5px; color: #b3bdc8; margin-top: 16px; }

.canvas-wrap, .crop-wrap { position: relative; line-height: 0; box-shadow: 0 4px 24px rgba(0,0,0,.35); }
#viewCanvas { display: block; background: #fff; image-rendering: auto; }
#gridCanvas { position: absolute; inset: 0; cursor: zoom-in; touch-action: manipulation; }
#cropCanvas { display: block; }
.crop-wrap { overflow: hidden; }
#cropBox {
  position: absolute; border: 2px solid #fff; cursor: move; touch-action: none;
  box-shadow: 0 0 0 9999px rgba(20,20,24,.6);
}
#cropBox .thirds {
  position: absolute; inset: 0; pointer-events: none;
  background:
    linear-gradient(to right, transparent calc(33.333% - .5px), rgba(255,255,255,.5) calc(33.333% - .5px), rgba(255,255,255,.5) calc(33.333% + .5px), transparent calc(33.333% + .5px), transparent calc(66.666% - .5px), rgba(255,255,255,.5) calc(66.666% - .5px), rgba(255,255,255,.5) calc(66.666% + .5px), transparent calc(66.666% + .5px)),
    linear-gradient(to bottom, transparent calc(33.333% - .5px), rgba(255,255,255,.5) calc(33.333% - .5px), rgba(255,255,255,.5) calc(33.333% + .5px), transparent calc(33.333% + .5px), transparent calc(66.666% - .5px), rgba(255,255,255,.5) calc(66.666% - .5px), rgba(255,255,255,.5) calc(66.666% + .5px), transparent calc(66.666% + .5px));
}
#cropBox .handle { position: absolute; width: 26px; height: 26px; background: #fff; border-radius: 50%; box-shadow: 0 1px 4px rgba(0,0,0,.5); }
#cropBox .handle[data-h="nw"] { left: -13px; top: -13px; cursor: nwse-resize; }
#cropBox .handle[data-h="ne"] { right: -13px; top: -13px; cursor: nesw-resize; }
#cropBox .handle[data-h="sw"] { left: -13px; bottom: -13px; cursor: nesw-resize; }
#cropBox .handle[data-h="se"] { right: -13px; bottom: -13px; cursor: nwse-resize; }

.crop-done { position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); box-shadow: 0 2px 10px rgba(0,0,0,.4); }
.busy {
  position: absolute; top: 12px; right: 12px;
  display: flex; align-items: center; gap: 8px;
  background: rgba(20,20,24,.75); color: #fff; padding: 6px 12px; border-radius: 20px; font-size: 13px;
}
.spinner { width: 14px; height: 14px; border: 2px solid rgba(255,255,255,.35); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: absolute; left: 50%; bottom: 16px; transform: translate(-50%, 20px);
  background: rgba(20,20,24,.9); color: #fff; padding: 8px 14px; border-radius: 8px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Dialogs ---------- */
dialog { border: 0; padding: 0; border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
dialog::backdrop { background: rgba(15,15,20,.6); }

dialog.camera { width: 100vw; height: 100dvh; max-width: none; max-height: none; border-radius: 0; background: #000; color: #fff; }
dialog.camera[open] { display: flex; flex-direction: column; }
.cam-stage { position: relative; flex: 1; min-height: 0; }
.cam-stage video, .cam-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.cam-msg { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 24px; text-align: center; margin: 0; }
.cam-controls { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); }
.cam-controls select { background: #222; color: #fff; border-color: #444; }
.cam-buttons { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 16px; width: 100%; max-width: 480px; }
.cam-buttons .btn:first-child { justify-self: start; }
.cam-buttons .btn:last-child { justify-self: end; }
.shutter { width: 68px; height: 68px; border-radius: 50%; border: 4px solid #fff; background: #fff; box-shadow: inset 0 0 0 3px #000; cursor: pointer; }
.shutter:active { transform: scale(.95); }

dialog.msg { width: min(420px, calc(100vw - 32px)); background: var(--panel); color: var(--ink); }
dialog.msg form { padding: 20px 22px 18px; display: grid; gap: 12px; }
dialog.msg h2 { margin: 0; font-size: 18px; font-weight: 650; }
dialog.msg p { margin: 0; color: var(--muted); line-height: 1.5; }
dialog.msg input { min-height: 40px; padding: 0 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
dialog.msg input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.msg-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.btn.danger { background: #b42318; border-color: #b42318; color: #fff; }
.btn.danger:hover:not(:disabled) { background: #912018; }
dialog.cell { width: min(760px, calc(100vw - 24px)); background: var(--panel); }
dialog.cell[open] { display: flex; flex-direction: column; }
dialog.cell header { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 0 16px; }
dialog.cell h2 { margin: 0; font-size: 20px; }
.cell-sub { margin: 2px 16px 8px; color: var(--muted); font-size: 13.5px; }
#cellCanvas { display: block; width: 100%; background: var(--stage); }
.cell-nav { display: flex; gap: 6px; padding: 10px 12px 12px; flex-wrap: wrap; }
.cell-nav .btn { min-width: 44px; font-size: 18px; }
.cell-nav select { margin-left: auto; }

.error-page { max-width: 520px; margin: 60px auto; padding: 0 16px; }

#printArea { display: none; }

/* ---------- Phone layout ---------- */
@media (max-width: 820px) {
  .workspace {
    display: flex; flex-direction: column; height: auto;
  }
  .stage { height: 58dvh; min-height: 300px; flex: none; }
  .panel { border-left: 0; border-top: 1px solid var(--line); overflow: visible; }
  .tabs { top: 0; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  .tabs button { flex-shrink: 0; }
  .topbar-actions .btn span { display: none; }
  .topbar-actions .btn { padding: 0 11px; }
  .brand h1 { font-size: 16px; }
}
@media (max-width: 380px) {
  .seg span { padding: 6px 9px; }
}

/* ---------- Print ---------- */
@media print {
  body { background: #fff; }
  #app > *:not(#printArea) { display: none !important; }
  #printArea { display: block; color: #000; font: 10pt/1.3 system-ui, sans-serif; }
  .print-page { break-after: page; page-break-after: always; display: flex; flex-direction: column; align-items: center; }
  .print-page:last-child { break-after: auto; page-break-after: auto; }
  .print-head { width: 100%; display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; margin-bottom: 0.08in; }
  .print-head h1 { font-size: 13pt; margin: 0; }
  .print-head .meta { font-size: 9pt; color: #444; text-align: right; }
  .print-page img { display: block; }
  .scalebar { display: inline-flex; align-items: center; gap: 6px; font-size: 8pt; }
  .scalebar i { display: inline-block; width: 2in; height: 0.08in; border: 0.5pt solid #000; border-top: 0; }
  .plan-row { display: flex; gap: 0.2in; width: 100%; justify-content: space-between; align-items: flex-start; }
  .plan-row figure { margin: 0; flex: 1; display: flex; flex-direction: column; align-items: center; }
  .plan-row figcaption { font-size: 9pt; margin-top: 0.05in; color: #333; }
  .plan-row img { max-width: 100%; max-height: 4.9in; object-fit: contain; border: 0.5pt solid #bbb; }
  .print-legend { display: flex; gap: 2px; margin-top: 4px; }
  .print-legend span { flex: 0 1 0.32in; min-width: 0; height: 0.22in; font-size: 7pt; display: flex; align-items: center; justify-content: center; border: 0.5pt solid #999; }
  .notes { width: 100%; margin-top: 0.15in; border-top: 0.5pt solid #999; }
  .notes div { border-bottom: 0.5pt solid #ccc; height: 0.32in; font-size: 8pt; color: #666; padding-top: 2px; }
  .tile { outline: 0.5pt dashed #aaa; }
  .cutout { display: block; overflow: hidden; }
  .tile-label { width: 100%; display: flex; justify-content: space-between; font-size: 8.5pt; margin-bottom: 0.06in; }
}
