/* Sticker Print Studio — styles
   1. Design tokens   2. Base   3. Top bar   4. Layout   5. Preview   6. Settings panel
   7. Controls        8. Messages   9. Sticker (screen + print)   10. Print output */

/* 1. Design tokens */
:root {
  --bg: #ffffff;
  --panel: #fafaf9;
  --canvas: #f2f2f0;
  --line: #e5e5e2;
  --line-strong: #cfcfca;
  --text: #1a1a1a;
  --text-2: #6b6b66;
  --accent: #111111;
  --accent-hover: #333333;
  --accent-text: #ffffff;
  --warn-bg: #fdf3e1;  --warn-text: #7a4b00;  --warn-line: #f0d9a8;
  --error-bg: #fbe9e9; --error-text: #8a1f1f; --error-line: #f1c6c6;
  --ok-text: #1d6b3a;
  --radius: 8px;
  --radius-input: 6px;
  --space: 8px;
  --font-ui: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --panel-width: 320px;
  --topbar-height: 56px;
}

/* 2. Base */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font-ui);
  font-size: 13px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* 3. Top bar */
.topbar {
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  gap: calc(var(--space) * 3);
  padding: 0 calc(var(--space) * 3);
  border-bottom: 1px solid var(--line);
}
.app-name { margin: 0; font-size: 15px; font-weight: 600; white-space: nowrap; }
.load-form { flex: 1; display: flex; gap: var(--space); max-width: 640px; }
.load-form .input { flex: 1; }
.load-field { flex: 1; position: relative; }
.load-error {
  position: absolute; left: 2px; top: calc(100% + 3px);
  font-size: 12px; color: var(--error-text); white-space: nowrap;
}
.load-error.is-note { color: var(--text-2); }
.topbar-link { margin-left: auto; color: var(--text-2); text-decoration: none; font-size: 12px; white-space: nowrap; }
.topbar-link:hover { color: var(--text); text-decoration: underline; }

/* 4. Layout */
.layout {
  display: grid;
  grid-template-columns: 1fr var(--panel-width);
  height: calc(100% - var(--topbar-height));
  min-height: 600px;
}

/* 5. Preview */
.preview {
  display: flex;
  flex-direction: column;
  gap: calc(var(--space) * 2);
  padding: calc(var(--space) * 3);
  background: var(--canvas);
  min-width: 0;
  overflow: hidden;
}
.stage {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: auto;
}
/* margin:auto centres the page, but when the page is bigger than the stage
   (Actual size, A4) it starts at the top-left so every part can be scrolled to. */
.stage-box { position: relative; flex: none; margin: auto; }
.view-row { display: flex; align-items: center; gap: 12px; }
.calibrate-note { color: var(--text-2); }

/* Calibration dialog */
.cal-dialog {
  width: min(720px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  color: var(--text);
}
.cal-dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.cal-dialog h2 { margin: 0 0 6px; font-size: 16px; font-weight: 600; }
.cal-dialog p { margin: 0 0 12px; color: var(--text-2); }
.cal-stage {
  display: flex; padding: 16px; margin-bottom: 12px;
  background: var(--canvas); border-radius: var(--radius); overflow: auto;
}
.cal-card {
  flex: none; margin: auto;
  border: 2px solid var(--accent); border-radius: 3.18mm;
  background: #fff;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-2); font-size: 12px;
}
/* Phones/tablets: an upright line the length of the card's short edge, with end marks */
.cal-card.is-edge {
  width: 28px;
  border: 0; border-radius: 0; background: none;
  border-left: 3px solid var(--accent);
  border-top: 3px solid var(--accent);
  border-bottom: 3px solid var(--accent);
}
.cal-controls { display: flex; align-items: center; gap: 8px; }
.cal-controls .range { min-width: 0; }
.cal-value { width: 52px; text-align: right; font-variant-numeric: tabular-nums; }
.cal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.cal-actions .link-btn { margin-right: auto; }
.stage-page {
  position: absolute; left: 0; top: 0;
  transform-origin: 0 0;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 4px 16px rgba(0, 0, 0, 0.06);
}
.preview-meta {
  display: flex; flex-direction: column; align-items: center; gap: var(--space);
  color: var(--text-2); font-size: 12px;
}
.empty {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: calc(var(--space) * 2);
  text-align: center; color: var(--text-2);
}
.empty h2 { margin: 0; font-size: 15px; font-weight: 600; color: var(--text); }
.empty ol { margin: 0; padding-left: 20px; text-align: left; line-height: 1.8; }

/* 6. Settings panel */
.panel {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
  min-height: 0;
}
.panel-scroll { flex: 1; overflow-y: auto; padding: calc(var(--space) * 2) calc(var(--space) * 2) 0; }
.group { padding: 4px 0 12px; margin-bottom: 8px; border-bottom: 1px solid var(--line); }
.group:last-child { border-bottom: 0; }
.group-title { margin: 0 0 var(--space); font-size: 12px; font-weight: 600; color: var(--text-2); }
.field { margin-bottom: 10px; }
.field:last-child { margin-bottom: 0; }
.label { display: block; margin-bottom: 4px; font-size: 12px; color: var(--text-2); }
.row { display: flex; gap: var(--space); align-items: center; }
.panel-foot {
  padding: calc(var(--space) * 2);
  border-top: 1px solid var(--line);
  background: var(--panel);
}

/* 7. Controls */
.input, .select, .textarea {
  width: 100%;
  height: 32px;
  padding: 0 10px;
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-input);
}
.textarea { height: auto; min-height: 72px; padding: 6px 10px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.input.is-invalid { border-color: var(--error-text); }
.input-num { width: 76px; flex: none; }
.btn {
  height: 32px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-input);
  background: var(--bg);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--text-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-wide { width: 100%; height: 40px; font-size: 14px; }
.btn-small { height: 28px; padding: 0 10px; font-size: 12px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.link-btn { padding: 0; border: 0; background: none; color: var(--text); text-decoration: underline; cursor: pointer; font-size: 12px; }
.seg { display: flex; border: 1px solid var(--line-strong); border-radius: var(--radius-input); overflow: hidden; background: var(--bg); }
.seg button { flex: 1; height: 30px; padding: 0 12px; border: 0; background: none; cursor: pointer; font-size: 12px; }
.seg button + button { border-left: 1px solid var(--line-strong); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.seg-inline { display: inline-flex; }
.seg-inline button { flex: none; white-space: nowrap; }
.range { flex: 1; accent-color: var(--accent); }
details.edit summary { cursor: pointer; font-weight: 600; font-size: 12px; list-style: none; }
details.edit summary::-webkit-details-marker { display: none; }
details.edit summary::after { content: " ›"; }
details.edit[open] summary::after { content: " ⌄"; }
details.edit[open] summary { margin-bottom: var(--space); }

.check { display: flex; gap: 8px; align-items: center; font-size: 12px; cursor: pointer; }
.check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
details.mini { margin-top: 6px; }
details.mini summary { cursor: pointer; font-size: 12px; color: var(--text-2); }
.emoji-list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px 12px; margin-top: 6px; font-size: 12px; }
.emoji-list code { font-family: ui-monospace, monospace; color: var(--text-2); }
.emoji-picker { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.emoji-picker button {
  width: 32px; height: 32px; padding: 0;
  border: 1px solid var(--line-strong); border-radius: var(--radius-input);
  background: var(--bg); cursor: pointer; font-size: 16px; line-height: 1;
}
.emoji-picker button:hover { border-color: var(--text-2); }
.emoji-picker button, .emoji-grid button, .emoji-cats button {
  font-family: "Noto Color Emoji", "Segoe UI Emoji", "Apple Color Emoji", sans-serif;
}
.emoji-more-btn { margin-top: 8px; }
.emoji-panel {
  margin-top: 8px; padding: 8px;
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--bg);
}
.emoji-search { height: 32px; }
.emoji-cats { display: flex; justify-content: space-between; margin: 6px 0; overflow-x: auto; }
.emoji-cats button { flex: none; width: 28px; height: 30px; padding: 0; border: 0; border-radius: var(--radius-input); background: none; cursor: pointer; font-size: 17px; }
.emoji-cats button:hover { background: var(--canvas); }
.emoji-scroll { max-height: 220px; overflow-y: auto; position: relative; }
.emoji-heading { position: sticky; top: 0; margin: 0; padding: 4px 0; background: var(--bg); font-size: 11px; font-weight: 600; color: var(--text-2); }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(32px, 1fr)); }
.emoji-grid button { height: 32px; padding: 0; border: 0; border-radius: var(--radius-input); background: none; cursor: pointer; font-size: 20px; line-height: 1; }
.emoji-grid button:hover { background: var(--canvas); }
.emoji-empty { padding: 8px 0; }

/* 8. Messages */
.hint { margin-top: 4px; font-size: 12px; color: var(--text-2); }
.hint.is-ok { color: var(--ok-text); }
.hint.is-error, .field-error { color: var(--error-text); }
.field-error { margin-top: 4px; font-size: 12px; }
.banner {
  display: flex; align-items: center; justify-content: space-between; gap: calc(var(--space) * 2);
  padding: 8px 12px;
  border: 1px solid; border-radius: var(--radius-input);
  font-size: 12px;
}
.banner.is-warn { background: var(--warn-bg); border-color: var(--warn-line); color: var(--warn-text); }
.banner.is-error { background: var(--error-bg); border-color: var(--error-line); color: var(--error-text); }
.banner .link-btn { color: inherit; font-weight: 600; }
.print-status { margin-top: var(--space); min-height: 18px; font-size: 12px; color: var(--text-2); }
.print-status.is-ok { color: var(--ok-text); }
.print-status.is-error { color: var(--error-text); }

/* 9. Sticker — one layout for preview, PDF and print.
   --w / --h: sticker size, --k: text scale, --font: chosen font (set by js/sticker.js) */
.sticker {
  --w: 58mm; --h: 38mm; --k: 1;
  --font: "Architects Daughter", cursive;
  position: relative;
  width: var(--w);
  height: var(--h);
  overflow: hidden;
  background: #fff;
  color: #000;
  font-family: var(--font);
}
.sticker .order-no {
  position: absolute; top: 0; right: 0;
  padding: calc(6px * var(--k));
  font-family: monospace; font-weight: 300;
  font-size: calc(9px * var(--k));
  line-height: 1.2;
}
.sticker .content-wrap { display: flex; align-items: center; height: 100%; }
.sticker .content {
  width: 100%;
  padding: 0 calc(7.5px * var(--k));
  font-size: calc(12px * var(--k));
  line-height: 1.4;
  overflow-wrap: anywhere;
}
.sticker p { margin: 0.5em 0; }
.sticker .greeting, .sticker .from { font-weight: 700; }
.sticker .message { white-space: pre-line; }
.sticker .emo { font-family: "Noto Color Emoji", "Segoe UI Emoji", "Apple Color Emoji", sans-serif; font-weight: 400; font-style: normal; }
.sticker .from { text-align: right; }
.sticker p:empty { display: none; }

.page { position: relative; background: #fff; overflow: hidden; }
.page.is-sheet .sticker { position: absolute; left: 10mm; top: 10mm; }
.page .cut-line {
  position: absolute; left: 10mm; top: 10mm;
  border: 0.25mm dashed #999;
  pointer-events: none;
}

/* 10. Print output — only #print-root is printed */
#print-root { display: none; }
@media print {
  html, body { height: auto; background: #fff; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block; }
  #print-root .page { break-after: page; }
}

/* 11. Phones and small tablets: one column, page scrolls, Print button stays at the bottom */
@media screen and (max-width: 860px) {
  html, body { height: auto; }
  .topbar {
    height: auto;
    flex-wrap: wrap;
    gap: 8px 12px;
    padding: 12px 16px;
  }
  .app-name { order: 1; }
  .topbar-link { order: 2; margin-left: auto; }
  .load-form { order: 3; flex-basis: 100%; max-width: none; }
  .load-error { position: static; margin-top: 4px; white-space: normal; }
  .layout { display: block; height: auto; min-height: 0; }
  .preview { padding: 16px; overflow: visible; }
  .stage { flex: none; height: clamp(200px, 62vw, 420px); overflow: auto; }
  .empty { padding: 24px 0; }
  .banner { flex-wrap: wrap; }
  .panel { border-left: 0; border-top: 1px solid var(--line); }
  .panel-scroll { overflow: visible; padding: 16px 16px 0; }
  .panel-foot {
    position: sticky; bottom: 0; z-index: 2;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    box-shadow: 0 -1px 0 var(--line), 0 -6px 16px rgba(0, 0, 0, 0.06);
  }
  .input, .select, .textarea { font-size: 16px; height: 40px; } /* 16px stops phones zooming in on tap */
  .textarea { height: auto; }
  .input-num { width: 84px; }
  .seg button { height: 38px; }
  .btn { height: 40px; }
  .btn-small { height: 36px; }
  .emoji-picker button { width: 40px; height: 40px; font-size: 18px; }
  .emoji-list { grid-template-columns: repeat(2, 1fr); }
  .cal-dialog { padding: 16px; }
  .cal-stage { padding: 12px; }
  .cal-actions .btn { flex: 1; }
}
