*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
/* Ensure the HTML hidden attribute always wins regardless of display rules */
[hidden] { display: none !important; }

/* ── Error toast ── */
.error-toast {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  opacity: 0;
  background: #c0392b;
  color: #fff;
  padding: 0.65rem 1.25rem;
  border-radius: 6px;
  font-size: 0.88rem;
  font-weight: 500;
  max-width: min(90vw, 380px);
  text-align: center;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  transition: transform 0.25s ease, opacity 0.25s ease;
  z-index: 200;
  pointer-events: none;
}
.error-toast.visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

html, body {
  min-height: 100%;
  min-height: 100dvh;
}

body {
  overflow-x: hidden;   /* no horizontal scroll */
  overflow-y: auto;     /* page scrolls freely in both modes */
}

:root {
  --bg:           #fff;
  --grid-minor:   rgba(100, 149, 237, 0.25);
  --grid-major:   rgba(100, 149, 237, 0.55);
  --grid-size:    24px;
  --grid-major-n: 4;

  --surface:      #fffef8;
  --border:       #d0cfc5;
  --text:         #1a1a1a;
  --muted:        #888;
  --accent:       #c0392b;
  --accent-hover: #a93226;
  --accent-light: #fdecea;
  --rule-line:    rgba(180, 200, 240, 0.6);
  --margin-line:  #e8a0a0;
  --paper-shadow: rgba(0,0,0,0.18);
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background-color: var(--bg);
  background-image:
    linear-gradient(var(--grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-major) 1px, transparent 1px),
    linear-gradient(var(--grid-minor) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-minor) 1px, transparent 1px);
  background-size:
    96px 96px,
    96px 96px,
    24px 24px,
    24px 24px;
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ── Page wrapper ──────────────────────────────────────────── */

.page {
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  padding: 0.75rem 0.75rem 0;
  padding-top: max(0.75rem, env(safe-area-inset-top));
  /* Generous bottom padding so the tear-off and graph paper are fully visible */
  padding-bottom: max(3rem, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

/* ── Header ─────────────────────────────────────────────────── */

.site-header {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex-shrink: 0;
}

.header-row {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
}

.site-title {
  font-family: 'Futura', 'Century Gothic', 'Trebuchet MS', sans-serif;
  font-style: normal;
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.02em;
  line-height: 1.15;
  white-space: nowrap;
}

.header-rule {
  height: 2px;
  background: linear-gradient(to right, var(--accent), transparent);
  margin-top: 0.25rem;
}

/* ── Printer badge ───────────────────────────────────────────── */

.badge {
  font-size: 0.7rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: #eee;
  color: var(--muted);
  white-space: nowrap;
  border: 1px solid #ddd;
}
.badge.ok   { background: #d4edda; color: #276535; border-color: #b8dfc3; }
.badge.err  { background: #fdecea; color: var(--accent); border-color: #f5c6c2; }
.badge.stub { background: #fff3cd; color: #856404; border-color: #ffeeba; }

/* (mode switch row removed) */

/* ── Paper card (receipt style) ──────────────────────────────── */

.paper-card {
  background: #fefdf5;
  /* Wide enough to contain 48ch textarea + horizontal padding */
  max-width: 520px;
  margin: 0 auto;
  width: 100%;
  border-radius: 2px;
  /* Tight horizontal padding so 48ch fits on more screens */
  padding: 1.25rem 0.75rem 2rem;
  position: relative;
  overflow: hidden;

  display: flex;
  flex-direction: column;
  /* Center all direct children so 48ch content sits in the middle on wide screens */
  align-items: center;

  /* ── Crumple shading ── */
  /* Dark creased corners + highlight ridges + fold shadows */
  background-image:
    /* Corner darks */
    radial-gradient(ellipse at 0%   0%,   rgba(0,0,0,0.13) 0%, transparent 35%),
    radial-gradient(ellipse at 100% 0%,   rgba(0,0,0,0.10) 0%, transparent 32%),
    radial-gradient(ellipse at 100% 100%, rgba(0,0,0,0.12) 0%, transparent 38%),
    radial-gradient(ellipse at 0%   100%, rgba(0,0,0,0.09) 0%, transparent 30%),
    /* Mid-page crease diagonals */
    radial-gradient(ellipse at 30% 40%, rgba(0,0,0,0.07) 0%, transparent 25%),
    radial-gradient(ellipse at 72% 62%, rgba(0,0,0,0.06) 0%, transparent 22%),
    /* Highlight ridges (paper catching light) */
    radial-gradient(ellipse at 20% 55%, rgba(255,255,255,0.70) 0%, transparent 20%),
    radial-gradient(ellipse at 60% 22%, rgba(255,255,255,0.55) 0%, transparent 18%),
    radial-gradient(ellipse at 80% 78%, rgba(255,255,255,0.45) 0%, transparent 16%);

  box-shadow:
    0 1px 3px rgba(0,0,0,0.12),
    0 4px 16px rgba(0,0,0,0.16),
    0 14px 44px rgba(0,0,0,0.10),
    6px 10px 28px rgba(0,0,0,0.08);
}


/* ── Receipt header / footer (display only) ──────────────────── */

/* Receipt chrome (header, footer, toolbar) pinned to 48ch in the receipt font */
.rcpt-header,
.rcpt-footer,
.card-toolbar {
  font-family: 'Courier New', Courier, monospace;
  width: 48ch;
  max-width: 100%;
}

/* Input sections fill the card; the textarea/caption inside control their own width */
#section-text:not([hidden]),
#section-photo:not([hidden]) {
  width: 100%;
  max-width: 100%;
}

.rcpt-header,
.rcpt-footer {
  font-size: 12px;
  color: var(--text);
  flex-shrink: 0;
}

.rcpt-hello {
  font-size: 1.5em;
  font-weight: bold;
  text-align: center;
  letter-spacing: 0.12em;
  margin-bottom: 0.2em;
  text-transform: uppercase;
}

.rcpt-sub {
  text-align: center;
  font-size: 0.95em;
  margin-bottom: 0.25em;
  color: #555;
}

/* Dashes are generated in JS to exactly CHARS_PER_LINE characters */
.rcpt-dash {
  color: #aaa;
  overflow: hidden;
  white-space: nowrap;
  margin: 0.15em 0;
  font-size: 12px;
}

.rcpt-txn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.1em 0;
  margin: 0.3em 0;
  font-size: 0.9em;
}

.rcpt-txn-grid span:nth-child(even) {
  text-align: right;
}

.rcpt-thankyou {
  text-align: center;
  margin: 0.25em 0 0.15em;
  letter-spacing: 0.04em;
}

.rcpt-merchant {
  text-align: center;
  font-weight: bold;
  letter-spacing: 0.1em;
  margin-bottom: 0.1em;
}

/* Sections — natural height in both modes */
#section-text:not([hidden]),
#section-photo:not([hidden]) {
  display: flex;
  flex-direction: column;
}

/* ── Toolbar row (pills + char count) ────────────────────────── */

.card-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.65rem;
  flex-shrink: 0;
}

.mode-pills {
  display: flex;
  gap: 0.4rem;
}

/* Receipt-stamp style pills */
.pill {
  background: transparent;
  border: 1.5px solid rgba(0,0,0,0.18);
  border-radius: 4px;
  padding: 0.2rem 0.6rem;
  font-size: 0.88rem;
  font-family: 'Courier New', Courier, monospace;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.pill:hover {
  color: var(--text);
  border-color: rgba(0,0,0,0.35);
}
.pill.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.pill svg { display: block; }

/* ── Char counter ────────────────────────────────────────────── */

.char-count {
  font-size: 0.75rem;
  color: var(--muted);
  font-family: 'Courier New', Courier, monospace;
}
.char-count.near-limit { color: #c77b00; }
.char-count.at-limit   { color: var(--accent); font-weight: 600; }

/* ── Text inputs ─────────────────────────────────────────────── */

textarea {
  /* width: 48ch measured in the textarea's own Courier New font — this is the
     exact character width of the physical receipt (CHARS_PER_LINE = 48).
     min() lets it shrink on narrow screens without horizontal overflow. */
  display: block;
  width: min(48ch, 100%);
  align-self: center;
  height: 10em;
  background: transparent;
  color: var(--text);
  border: none;
  border-top: 1px dashed rgba(0,0,0,0.12);
  padding: 0.75rem 0 0;
  /* 16px minimum — prevents iOS Safari from zooming on focus */
  font-size: 16px;
  font-family: 'Courier New', Courier, monospace;
  line-height: 1.5;
  resize: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

textarea:focus { outline: none; }



/* ── Print button ────────────────────────────────────────────── */

.btn-print {
  display: block;
  width: 60%;
  margin: 1rem auto 0.5rem;
  padding: 0.65rem 1rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  cursor: pointer;
  flex-shrink: 0;
  box-shadow: 0 2px 0 var(--accent-hover), 0 3px 8px rgba(192,57,43,0.3);
  transition: background 0.15s, box-shadow 0.15s, transform 0.1s;
}
.btn-print:hover:not(:disabled) {
  background: var(--accent-hover);
  box-shadow: 0 2px 0 #8b1f17, 0 4px 12px rgba(192,57,43,0.4);
}
.btn-print:active:not(:disabled) {
  transform: translateY(1px);
  box-shadow: 0 1px 0 var(--accent-hover);
}
.btn-print:disabled {
  background: #e0a09a;
  box-shadow: none;
  cursor: default;
}

/* ── Photo section ───────────────────────────────────────────── */

.ascii-photo-wrap {
  position: relative;
  width: 100%;
  display: flex;
  flex-direction: column;
}

/* ASCII-art placeholder box — font-size set by JS to fit container */
.ascii-placeholder {
  font-family: 'Courier New', Courier, monospace;
  line-height: 1.4;
  color: #aaa;
  white-space: pre;
  user-select: none;
  display: block;
  margin: 0.5rem auto 0;  /* auto centres the fixed-width block */
  overflow: hidden;
}

/* Image preview inside the ASCII wrap */
#image-preview-wrap {
  position: relative;
  width: 100%;
  margin-top: 0.5rem;
}
#image-preview {
  display: block;
  width: 100%;
  height: auto;
  border: 1px solid rgba(0,0,0,0.1);
}

/* ── Caption field ───────────────────────────────────────────── */

.caption-area {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin: 0.5rem 0 0.25rem;
}

#caption-input {
  flex: 1;
  background: transparent;
  border: none;
  border-bottom: 1px dashed rgba(0,0,0,0.2);
  padding: 0.25rem 0;
  /* 16px prevents iOS Safari from zooming on focus */
  font-size: 16px;
  font-family: 'Courier New', Courier, monospace;
  color: var(--text);
}
#caption-input::placeholder { color: #bbb; font-style: italic; }
#caption-input:focus { outline: none; border-bottom-color: var(--accent); }

.caption-count {
  font-size: 0.7rem;
  font-family: 'Courier New', Courier, monospace;
  color: var(--muted);
  white-space: nowrap;
}

.btn-clear-img {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 2.2rem;
  height: 2.2rem;
  font-size: 1.2rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.btn-clear-img:hover { background: rgba(0,0,0,0.85); }

/* ── Photo source buttons ────────────────────────────────────── */

.photo-btns {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 0.75rem;
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  padding: 0.45rem 1rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn-secondary:hover {
  background: #f4f0e8;
  border-color: var(--accent);
  color: var(--accent);
}

/* photo-mode class no longer needed — both modes use the same scrollable layout */
