/* All selectors are prefixed with .pt- so the component can sit inside the product's pages.
   Theme: the CSS variables on .pt-root (colours, radii, shadows); dark mode follows the phone's setting. */

.pt-root {
  --pt-accent: #2f5bea;
  --pt-accent-hover: #2549c4;
  --pt-accent-text: #fff;
  --pt-accent-soft: rgba(47, 91, 234, 0.12);
  --pt-text: #161a24;
  --pt-muted: #626a7b;
  --pt-bg: #ffffff;
  --pt-surface: #ffffff;
  --pt-bg-soft: #f2f4f8;
  --pt-border: #e1e4ea;
  --pt-error: #b42318;
  --pt-error-soft: #fdecea;
  --pt-warn-bg: #fff6e0;
  --pt-ok: #1a7f37;
  --pt-radius: 14px;
  --pt-radius-small: 10px;
  --pt-shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 6px 20px rgba(16, 24, 40, 0.08);
  display: flex;
  flex-direction: column;
  gap: 16px;
  font: 16px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--pt-text);
  -webkit-tap-highlight-color: transparent;
  flex: 1 0 auto; /* fills its host when the host is a flex column (the demo page), so the bottom bar sits at the bottom */
}
@media (prefers-color-scheme: dark) {
  .pt-root {
    --pt-accent: #6d8cff;
    --pt-accent-hover: #87a0ff;
    --pt-accent-text: #0b1020;
    --pt-accent-soft: rgba(109, 140, 255, 0.18);
    --pt-text: #eef0f5;
    --pt-muted: #a3aabb;
    --pt-bg: #0f1218;
    --pt-surface: #181c25;
    --pt-bg-soft: #1f2430;
    --pt-border: #2c3240;
    --pt-error: #ff8a80;
    --pt-error-soft: rgba(255, 138, 128, 0.12);
    --pt-warn-bg: #3a3218;
    --pt-ok: #6fd08c;
    --pt-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

/* Anything marked `hidden` stays hidden, whatever display a .pt- class sets (buttons are inline-flex). */
.pt-root [hidden] { display: none !important; }

.pt-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.pt-banner {
  padding: 8px 12px;
  border-radius: var(--pt-radius-small);
  background: var(--pt-warn-bg);
  font-size: 14px;
}

/* ---- buttons ---- */
.pt-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--pt-border);
  background: var(--pt-surface);
  color: inherit;
  font: inherit;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: var(--pt-radius-small);
  cursor: pointer;
  min-height: 44px;
  transition: background-color 0.15s, transform 0.1s;
}
.pt-btn:hover { background: var(--pt-bg-soft); }
.pt-btn:active { transform: scale(0.98); }
.pt-btn:disabled { opacity: 0.55; cursor: progress; }
.pt-btn:focus-visible { outline: 3px solid var(--pt-accent-soft); outline-offset: 2px; }
.pt-btn--primary { background: var(--pt-accent); border-color: var(--pt-accent); color: var(--pt-accent-text); }
.pt-btn--primary:hover { background: var(--pt-accent-hover); }
.pt-btn--ghost { border-color: transparent; background: transparent; color: var(--pt-accent); }
.pt-btn--ghost:hover { background: var(--pt-accent-soft); }
.pt-btn--small { padding: 6px 12px; min-height: 36px; font-size: 14px; }
.pt-btn--big { min-height: 52px; font-size: 17px; border-radius: var(--pt-radius); }
.pt-btn--icon { flex-direction: column; gap: 0; font-size: 12px; padding: 6px 14px; min-width: 76px; }
.pt-icon { width: 22px; height: 22px; flex: none; }
.pt-btn--icon .pt-icon { width: 24px; height: 24px; }

/* ---- language bar: "Greek -> [English v]" ---- */
.pt-langbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.pt-lang-from { font-weight: 600; color: var(--pt-muted); }
.pt-lang-arrow { color: var(--pt-muted); }
.pt-lang-pill { position: relative; display: inline-flex; align-items: center; }
.pt-lang-select {
  appearance: none;
  -webkit-appearance: none;
  font: inherit;
  font-weight: 600;
  color: var(--pt-accent);
  background: var(--pt-accent-soft);
  border: 0;
  border-radius: 999px;
  padding: 10px 38px 10px 16px;
  min-height: 44px;
  cursor: pointer;
}
.pt-lang-select:focus-visible { outline: 3px solid var(--pt-accent-soft); outline-offset: 2px; }
.pt-lang-caret {
  position: absolute;
  right: 16px;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--pt-accent);
  border-bottom: 2px solid var(--pt-accent);
  transform: translateY(-2px) rotate(45deg);
  pointer-events: none;
}

/* Generic form controls (also used by the TEMPORARY benchmark panel). */
.pt-lang { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--pt-muted); }
.pt-select {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--pt-border);
  border-radius: var(--pt-radius-small);
  background: var(--pt-surface);
  color: var(--pt-text);
  min-height: 44px;
}

/* ---- empty state (before the first photo) ---- */
.pt-emptystate {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 6px;
  padding: 40px 20px;
  border: 2px dashed var(--pt-border);
  border-radius: var(--pt-radius);
  background: var(--pt-bg-soft);
}
.pt-emptystate[hidden] { display: none; }
.pt-emptystate-icon { width: 48px; height: 48px; color: var(--pt-accent); margin-bottom: 4px; }
.pt-emptystate p { margin: 0; font-weight: 600; max-width: 28ch; }

/* ---- photo stage ---- */
.pt-editor { display: flex; flex-direction: column; gap: 10px; }
.pt-editor[hidden] { display: none; }
.pt-hint { margin: 0; color: var(--pt-muted); font-size: 14px; }
.pt-stage {
  position: relative;
  align-self: center;
  max-width: 100%;
  overflow: hidden;
  border-radius: var(--pt-radius);
  background: #111;
  box-shadow: var(--pt-shadow);
  user-select: none;
  -webkit-user-select: none;
}
.pt-stage img {
  display: block;
  max-width: 100%;
  max-height: 70vh;
  pointer-events: none;
}
/* The finished image from the server, exactly over the photo (same proportions); fades in once loaded. */
.pt-stage img.pt-rendered {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  opacity: 0;
  transition: opacity 0.35s ease;
}
.pt-stage img.pt-rendered.pt-rendered--in { opacity: 1; }
.pt-stage img.pt-rendered[hidden] { display: none; }

/* While working: the photo dims slightly and a light band sweeps over it ("scanning"). */
.pt-scan { position: absolute; inset: 0; z-index: 4; pointer-events: none; opacity: 0; transition: opacity 0.25s; }
.pt-stage--busy .pt-scan {
  opacity: 1;
  background:
    linear-gradient(180deg, transparent 0%, rgba(255, 255, 255, 0.0) 35%, rgba(255, 255, 255, 0.32) 50%, rgba(255, 255, 255, 0) 65%, transparent 100%) 0 0 / 100% 220% no-repeat,
    rgba(10, 14, 24, 0.18);
  animation: pt-scan 1.6s ease-in-out infinite;
}
@keyframes pt-scan {
  from { background-position: 0 120%, 0 0; }
  to { background-position: 0 -120%, 0 0; }
}
@media (prefers-reduced-motion: reduce) {
  .pt-stage--busy .pt-scan { animation: none; }
  .pt-stage img.pt-rendered { transition: none; }
}

/* Progress steps over the bottom of the photo: Reading -> Translating -> Drawing. */
.pt-progress {
  position: absolute;
  left: 50%;
  bottom: 12px;
  z-index: 5;
  transform: translateX(-50%);
  display: flex;
  gap: 4px;
  margin: 0;
  padding: 6px;
  list-style: none;
  border-radius: 999px;
  background: rgba(15, 18, 26, 0.72);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  color: rgba(255, 255, 255, 0.6);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.pt-progress[hidden] { display: none; }
.pt-step { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; transition: color 0.2s, background-color 0.2s; }
.pt-step-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.pt-step--active { color: #fff; background: rgba(255, 255, 255, 0.14); }
.pt-step--active .pt-step-dot { animation: pt-pulse 1s ease-in-out infinite; }
.pt-step--done { color: #8fe3a5; }
@keyframes pt-pulse { 50% { opacity: 0.35; } }
.pt-slow { margin: 0; text-align: center; font-size: 14px; color: var(--pt-muted); }
.pt-slow[hidden] { display: none; }

.pt-toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: center; }

/* "Translation | Original" switch */
.pt-seg { display: inline-flex; padding: 4px; gap: 4px; border-radius: 999px; background: var(--pt-bg-soft); }
.pt-seg[hidden] { display: none; }
.pt-seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--pt-muted);
  font: inherit;
  font-weight: 600;
  font-size: 15px;
  padding: 8px 18px;
  min-height: 40px;
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}
.pt-seg-btn[aria-pressed='true'] { background: var(--pt-surface); color: var(--pt-text); box-shadow: 0 1px 3px rgba(16, 24, 40, 0.15); }
.pt-seg-btn:focus-visible { outline: 3px solid var(--pt-accent-soft); outline-offset: 2px; }

/* ---- detected text blocks (older two-call flow) ---- */
.pt-blocks { position: absolute; inset: 0; }
/* Not choosing: just thin outlines, taps do nothing. */
.pt-blocks--passive .pt-block { cursor: default; background: transparent; border-width: 1px; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.3); }
.pt-block {
  position: absolute;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  min-width: 0;
  border: 2px solid rgba(255, 255, 255, 0.95);
  border-radius: 4px;
  background: rgba(47, 91, 234, 0.12);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
/* Small blocks (one short word) still get a finger-sized tap target. */
.pt-block::before { content: ''; position: absolute; inset: -8px; }
.pt-block--selected { border-color: var(--pt-accent); background: rgba(47, 91, 234, 0.35); box-shadow: 0 0 0 2px #fff; }
.pt-block:focus-visible { outline: 3px solid #ffd43b; outline-offset: 1px; }
.pt-block-count { font-size: 14px; color: var(--pt-muted); }

/* ---- translation overlay (older two-call flow; drawn under the block outlines) ---- */
.pt-blocks { z-index: 2; }
.pt-overlay { position: absolute; inset: 0; z-index: 1; pointer-events: none; }
.pt-overlay[hidden] { display: none; }
/* Two stacked layers: all block backgrounds below, all text above (see overlay.js). */
.pt-overlay-layer { position: absolute; inset: 0; }
/* One background per line (or per one-piece block), tight to the letters, in the colour sampled around it; the
   same colour is spread 1px around it (box-shadow set in overlay.js) to cover anti-aliased letter edges.
   Barely rounded, so it looks like part of the photo rather than a label. */
.pt-overlay-bg { position: absolute; border-radius: 1px; }
/* Block text wrapped in the block box (when there are no line translations). Never clipped; words are
   only broken with a proper hyphen (lang is set on the box), never cut in the middle. */
.pt-overlay-block {
  position: absolute;
  box-sizing: border-box;
  padding: 1px 3px;
  white-space: pre-wrap;
  overflow-wrap: normal;
  word-break: normal;
  -webkit-hyphens: auto;
  hyphens: auto;
  line-height: 1.15;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-weight: 600;
}
/* One translated line (or table cell) in the box of the original line, transparent, on the block background. */
.pt-overlay-line {
  position: absolute;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  padding: 0; /* the text must fit the box exactly (overlay.js measures it against the box) */
  white-space: nowrap; /* a line that doesn't fit grows, is squeezed, shrinks or wraps (overlay.js) - never clipped */
  line-height: 1.05;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-weight: 600;
}
/* The text itself, so it can be squeezed horizontally (scaleX) without changing the box. */
.pt-overlay-text { display: inline-block; transform-origin: left center; }
/* A line too long even for its grown box: wraps onto more rows, at spaces only. */
.pt-overlay-line--wrap { white-space: normal; overflow-wrap: normal; word-break: normal; -webkit-hyphens: auto; hyphens: auto; }
/* While a translation is shown, blocks are just thin outlines so the text stays readable. */
.pt-blocks--translated .pt-block { background: transparent; border-width: 1px; border-style: dashed; box-shadow: none; }
.pt-blocks--translated .pt-block--selected { border-style: solid; }

/* ---- DEBUG view (?debug=1) ---- */
.pt-debug { position: absolute; inset: 0; z-index: 3; pointer-events: none; }
.pt-debug[hidden] { display: none; }
.pt-debug-line { position: absolute; box-sizing: border-box; border: 1px solid rgba(230, 30, 30, 0.9); }
.pt-debug-quads { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
/* the line through each text line (debug view): stays 3 px whatever the photo's size */
.pt-debug-paths { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.pt-debug-paths polyline {
  fill: none; stroke-width: 3px; stroke-linecap: round; stroke-linejoin: round; opacity: 0.9;
  vector-effect: non-scaling-stroke; filter: drop-shadow(0 0 1px rgba(255, 255, 255, 0.9));
}
.pt-debug-paths polyline.pt-debug-unchanged { stroke-dasharray: 6 5; opacity: 0.75; }
.pt-debug-paths polyline.pt-debug-low { stroke-dasharray: 0.5 7; stroke-linecap: round; } /* dotted: too unsure to translate */
.pt-debug-quads polygon { fill: none; stroke: rgba(230, 30, 30, 0.9); stroke-width: 1px; vector-effect: non-scaling-stroke; }
.pt-debug-block { position: absolute; box-sizing: border-box; border: 2px dashed rgba(30, 90, 230, 0.9); }
.pt-debug-label {
  position: absolute;
  top: -14px;
  left: -2px;
  padding: 0 3px;
  background: rgba(30, 90, 230, 0.9);
  color: #fff;
  font: 600 10px/13px system-ui, sans-serif;
}
.pt-debug-check { margin: 6px 0 0; font-size: 13px; color: var(--pt-ok); }
.pt-debug-check--bad { color: var(--pt-error); font-weight: 600; }
.pt-debug-panel h3 { font-size: 13px; margin: 0 0 6px; color: var(--pt-muted); }
.pt-debug-list { margin: 0; padding: 8px; background: var(--pt-bg-soft); border-radius: 6px; font-size: 12px; overflow-x: auto; white-space: pre; }

/* ---- notice (friendly errors with "Try again") ---- */
.pt-notice {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 14px;
  border-radius: var(--pt-radius);
  background: var(--pt-error-soft);
  color: var(--pt-error);
}
.pt-notice[hidden] { display: none; }
.pt-notice-icon { width: 22px; height: 22px; flex: none; }
.pt-notice-text { margin: 0; flex: 1 1 200px; font-weight: 600; }
.pt-notice .pt-btn[hidden] { display: none; }

/* ---- results ---- */
.pt-result { display: flex; flex-direction: column; gap: 12px; }
.pt-result[hidden] { display: none; }
.pt-summary { margin: 0; color: var(--pt-muted); }
.pt-summary strong { color: var(--pt-text); }
.pt-note { margin: 0; padding: 10px 14px; border-radius: var(--pt-radius-small); background: var(--pt-bg-soft); font-size: 15px; }
.pt-warning { margin: 0; padding: 8px 12px; border-radius: var(--pt-radius-small); background: var(--pt-warn-bg); font-size: 14px; }
.pt-empty {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin: 0;
  padding: 16px;
  border-radius: var(--pt-radius);
  background: var(--pt-bg-soft);
}
.pt-empty p { margin: 2px 0 0; color: var(--pt-muted); }
.pt-empty-icon { width: 28px; height: 28px; flex: none; color: var(--pt-muted); }
.pt-columns { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.pt-columns--single { grid-template-columns: 1fr; }
@media (max-width: 640px) { .pt-columns { grid-template-columns: 1fr; } }
.pt-panel {
  border: 1px solid var(--pt-border);
  border-radius: var(--pt-radius);
  background: var(--pt-surface);
  padding: 12px 14px;
  min-width: 0;
}
.pt-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.pt-panel-head h3 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--pt-muted); }
.pt-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 16px; user-select: text; -webkit-user-select: text; }
.pt-meta { margin: 0; font-size: 12px; color: var(--pt-muted); font-variant-numeric: tabular-nums; }

/* ---- history: a row you tap to open/close (<details>) ---- */
.pt-history { border: 1px solid var(--pt-border); border-radius: var(--pt-radius); background: var(--pt-surface); }
.pt-history-summary {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 0 16px;
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  -webkit-user-select: none;
  user-select: none;
}
.pt-history-summary::-webkit-details-marker { display: none; }
.pt-history-summary:focus-visible { outline: 3px solid var(--pt-accent-soft); outline-offset: 2px; border-radius: var(--pt-radius); }
.pt-history-count {
  min-width: 22px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--pt-bg-soft);
  color: var(--pt-muted);
  font-size: 13px;
  text-align: center;
}
.pt-history-caret {
  margin-left: auto;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--pt-muted);
  border-bottom: 2px solid var(--pt-muted);
  transform: translateY(-2px) rotate(45deg);
  transition: transform 0.2s;
}
.pt-history[open] .pt-history-caret { transform: translateY(2px) rotate(-135deg); }
.pt-history-list { list-style: none; margin: 0; padding: 0 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.pt-history-item {
  width: 100%;
  min-height: 56px;
  text-align: left;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  padding: 10px 14px;
  border: 1px solid var(--pt-border);
  border-radius: var(--pt-radius);
  background: var(--pt-surface);
  color: var(--pt-text);
  font: inherit;
  cursor: pointer;
}
.pt-history-item:hover { background: var(--pt-bg-soft); }
.pt-history-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.pt-history-meta { font-size: 13px; color: var(--pt-muted); }

/* ---- sticky bottom bar: [Take photo] [Gallery], within thumb reach, above the iPhone home bar ---- */
.pt-actionbar {
  position: sticky;
  bottom: 0;
  margin-top: auto !important; /* at the bottom even when there is little content (the host lets .pt-root grow) */
  z-index: 10;
  display: flex;
  gap: 10px;
  margin: 0 -16px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--pt-bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--pt-border);
}
.pt-actionbar .pt-btn--primary { flex: 1; }

/* ---- TEMPORARY benchmark panel ---- */
.pt-bench { margin-top: 24px; padding-top: 16px; border-top: 2px dashed var(--pt-border); display: flex; flex-direction: column; gap: 10px; font: 16px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; color: var(--pt-text, #1b1f2a); }
.pt-bench h2 { margin: 0; font-size: 18px; }
.pt-bench-label { margin-left: 0; }
.pt-bench-input { flex: 1; min-width: 0; }
.pt-bench-bar { height: 6px; border-radius: 3px; background: var(--pt-bg-soft); overflow: hidden; }
.pt-bench-bar-fill { height: 100%; width: 0; background: var(--pt-accent); transition: width 0.3s; }
.pt-bench-out { display: flex; flex-direction: column; gap: 8px; }
.pt-bench-scroll { overflow-x: auto; }
.pt-bench-table { border-collapse: collapse; width: 100%; font-size: 14px; }
.pt-bench-table th, .pt-bench-table td { border: 1px solid var(--pt-border); padding: 6px 8px; text-align: left; white-space: nowrap; }
.pt-bench-table th { background: var(--pt-bg-soft); font-weight: 600; }
.pt-num { text-align: right !important; font-variant-numeric: tabular-nums; }
.pt-strong { font-weight: 700; }
.pt-bench-details { border: 1px solid var(--pt-border); border-radius: var(--pt-radius-small); padding: 8px 12px; }
.pt-bench-details summary { cursor: pointer; font-weight: 600; }
.pt-bench-text { margin-top: 6px; padding: 8px; background: var(--pt-bg-soft); border-radius: 6px; font-size: 14px; }
.pt-bench-err { white-space: pre-wrap; font-size: 13px; margin: 6px 0; }
.pt-bench a.pt-btn { align-self: flex-start; text-decoration: none; display: inline-flex; align-items: center; }
