/* ===========================
   SUBTEXT — stylesheet
   Mobile-first, serif-forward
   =========================== */

/* ── Garamond fallback (garamontio) ── */
@font-face {
  font-family: 'GaramondIO';
  src: url('fonts/garamontio.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'GaramondIO';
  src: url('fonts/garamontio_it.woff2') format('woff2');
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

/* ── Saira variable font (UI sans) ── */
@font-face {
  font-family: 'Saira';
  src: url('fonts/saira.woff2') format('woff2');
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Saira';
  src: url('fonts/saira-italic.woff2') format('woff2');
  font-weight: 100 900;
  font-style: italic;
  font-display: swap;
}

*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg:        #f5f0e8;
  --surface:   #fffdf8;
  --border:    #d4c9b0;
  --ink:       #1a1410;
  --ink-muted: #7a6e5e;
  --accent:    #3b2e1e;
  --danger:    #b33a2e;
  --panel-h:   248px;
  --top-bar-h: 52px;
  --radius:    6px;
  --font-ui:   'Saira', system-ui, -apple-system, 'Helvetica Neue', Arial, sans-serif;
  --font-serif: Georgia, 'Times New Roman', serif;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  /* Use surface (not bg) so safe-area inset zones match the top bar
     and bottom panel. Upload screen and canvas wrapper set their own
     backgrounds explicitly, so this only affects the bleed-through areas. */
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  /* Saira wdth axis — slightly condensed for a tighter UI feel */
  font-variation-settings: 'wdth' 88;
}

/* ===== SCREENS ===== */

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* ===== UPLOAD SCREEN ===== */

#upload-screen {
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.upload-inner {
  text-align: center;
  padding: 2rem 1.5rem;
  max-width: 360px;
}

.wordmark {
  font-size: 2.8rem;
  font-weight: 400;
  letter-spacing: 0.04em;
  margin: 0 0 2.5rem;
  color: var(--accent);
  font-style: italic;
  font-family: var(--font-serif);
}

.tagline {
  font-size: 0.95rem;
  color: var(--ink-muted);
  margin: 0 0 2rem;
  font-style: italic;
}

.upload-btn {
  display: inline-block;
  padding: 0.75rem 2rem;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  font-family: var(--font-ui);
  font-size: 1rem;
  cursor: pointer;
  letter-spacing: 0.03em;
  transition: background 0.15s;
  border: none;
}

.upload-btn:hover {
  background: #5a4430;
}

.upload-btn input[type="file"] {
  display: none;
}

.upload-hint {
  margin-top: 1.2rem;
  font-size: 0.8rem;
  color: var(--ink-muted);
  font-style: italic;
}

/* ===== EDITOR SCREEN ===== */

#editor-screen {
  background: var(--surface);
}

/* TOP BAR */

#top-bar {
  /* Extend into status-bar safe area at the top */
  height: calc(var(--top-bar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) 1rem 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  flex-shrink: 0;
  z-index: 100;
}

.wordmark-small {
  font-size: 1.25rem;
  font-style: italic;
  color: var(--accent);
  letter-spacing: 0.04em;
  font-family: var(--font-serif);
  text-align: center;
}

/* Right-align the save button in its grid column */
#export-btn {
  justify-self: end;
}

.icon-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--ink);
  font-size: 1.1rem;
  width: 36px;
  height: 36px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-ui);
  transition: background 0.12s;
}

.icon-btn:hover {
  background: var(--bg);
}

.icon-btn:active {
  background: var(--border);
}

.export-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 0.45rem 1.1rem;
  font-family: var(--font-ui);
  font-size: 0.9rem;
  cursor: pointer;
  letter-spacing: 0.03em;
  transition: background 0.15s;
}

.export-btn:hover {
  background: #5a4430;
}

.export-btn:active {
  background: #3b2e1e;
  transform: scale(0.97);
}

/* CANVAS WRAPPER */

#canvas-wrapper {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: #1a1a1a;
}

#canvas-container {
  position: relative;
  display: inline-block;
  line-height: 0;
  user-select: none;
}

#base-image {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - var(--top-bar-h) - var(--panel-h) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 20px);
  pointer-events: none;
}

/* ===== SNAP GUIDES ===== */

.guide {
  position: absolute;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 5;
}

.guide.visible {
  opacity: 1;
}

.guide-v {
  top: 0;
  bottom: 0;
  width: 1px;
  transform: translateX(-0.5px);
  background: rgba(255, 215, 60, 0.35);
}

.guide-h {
  left: 0;
  right: 0;
  height: 1px;
  transform: translateY(-0.5px);
  background: rgba(255, 215, 60, 0.35);
}

.guide-v.snapped {
  background: rgba(255, 215, 60, 0.85);
  box-shadow: 0 0 4px rgba(255, 215, 60, 0.5);
}

.guide-h.snapped {
  background: rgba(255, 215, 60, 0.85);
  box-shadow: 0 0 4px rgba(255, 215, 60, 0.5);
}

/* ===== TEXT FIELDS ===== */

.text-field {
  position: absolute;
  cursor: move;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* Size to content width so the browser doesn't constrain it based on
     the remaining space to the right of the `left` position, which would
     cause text to reflow/wrap when the field is dragged toward the right edge. */
  width: max-content;
  /* Centered on tap point via JS transform */
}


.text-field-delete,
.text-field-drag {
  display: none;
  position: absolute;
  width: 22px;
  height: 22px;
  color: #fff;
  border: 2px solid #fff;
  border-radius: 50%;
  font-size: 13px;
  line-height: 1;
  align-items: center;
  justify-content: center;
  z-index: 20;
  box-shadow: 0 1px 4px rgba(0,0,0,0.35);
  font-family: sans-serif;
  font-weight: 700;
}

.text-field.selected .text-field-delete,
.text-field.selected .text-field-drag {
  display: flex;
}

.text-field-delete {
  top: -14px;
  left: -14px;
  background: var(--danger);
  cursor: pointer;
}

.text-field-drag {
  top: -14px;
  right: -14px;
  background: var(--accent);
  cursor: grab;
  font-size: 11px;
  letter-spacing: -0.5px;
  /* Never let the browser treat touches on the drag handle as scroll gestures */
  touch-action: none;
}

.text-field-drag:active {
  cursor: grabbing;
}

.text-field-inner {
  position: relative;
  min-width: 60px;
  white-space: pre-wrap;
  word-break: break-word;
  outline: none;
  cursor: text;
  text-align: center;
  /* Explicit line-height and padding so the selection outline
     properly contains cap-height, descenders, and stroke overflow */
  line-height: 1.25;
  padding: 0.15em 0.2em;
  /* Override parent's user-select: none so text editing works */
  user-select: text;
  -webkit-user-select: text;
}

.text-field.selected {
  outline: 1.5px dashed rgba(255,255,255,0.6);
  outline-offset: 4px;
}

/* Placeholder for empty contenteditable */
.text-field-inner:empty::before {
  content: attr(data-placeholder);
  color: rgba(255, 255, 255, 0.55);
  pointer-events: none;
  font-style: italic;
}

/* text-stroke via CSS paint-order trick, applied via JS inline styles */

/* ===== BOTTOM PANEL ===== */

#bottom-panel {
  /* Extend into home-indicator safe area at the bottom */
  height: calc(var(--panel-h) + env(safe-area-inset-bottom, 0px));
  padding: 0.45rem 0.85rem calc(0.45rem + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

#no-selection-hint {
  text-align: center;
  color: var(--ink-muted);
  font-style: italic;
  font-size: 0.9rem;
  font-family: var(--font-serif);
}

#font-controls {
  display: flex;
  flex-direction: column;
  gap: 0.38rem;
  height: 100%;
  justify-content: space-evenly;
}

#font-controls.hidden {
  display: none;
}

/* ── Shared row structure ── */

.controls-row {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  flex-wrap: nowrap;
}

.control-label {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--ink-muted);
  white-space: nowrap;
  display: block;
  margin-bottom: 3px;
  font-family: var(--font-ui);
  font-weight: 500;
}

.control-group {
  display: flex;
  flex-direction: column;
}

/* ── Presets row (column: label above pills) ── */

.controls-row--presets {
  flex-direction: column;
  align-items: flex-start;
  gap: 0.3rem;
}

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

.preset-btn {
  border: 1.5px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 0.8rem;
  padding: 0.22rem 0.8rem;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  font-style: normal;
  white-space: nowrap;
  letter-spacing: 0.01em;
  line-height: 1.4;
}

.preset-btn:hover:not(.active) {
  border-color: var(--ink-muted);
}

.preset-btn.active {
  background: var(--ink);
  color: var(--surface);
  border-color: var(--ink);
}

.preset-btn:active {
  opacity: 0.72;
}

/* ── Typeface row (column: label above dropdown+B+I) ── */

.controls-row--typeface {
  flex-direction: column;
  align-items: stretch;
  gap: 0.28rem;
}

.typeface-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.typeface-row select {
  flex: 1;
  min-width: 0;
}

/* Bold / Italic style-toggle buttons */

.style-btn {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
  width: 36px;
  height: 36px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.88rem;
  font-family: var(--font-ui);
  flex-shrink: 0;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  line-height: 1;
  padding: 0;
}

#ctrl-bold   { font-weight: 700;  font-family: var(--font-serif); }
#ctrl-italic { font-style: italic; font-family: var(--font-serif); }

.style-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.style-btn:active { opacity: 0.7; }

/* ── Size + Align row ── */

.controls-row--size-align {
  align-items: flex-end;
  gap: 0.85rem;
}

.control-group--size {
  flex: 2;
  min-width: 0;
}

/* No extra width override needed — input[type=range] is block by default */

.control-group--align {
  flex-shrink: 0;
}

.row-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 3px;
}

.row-header .control-label {
  margin-bottom: 0;
}

#ctrl-size-val,
#ctrl-outline-width-val {
  display: none;
}

/* ── Colors + Stroke row ── */

.controls-row--colors {
  align-items: flex-end;
  gap: 0.7rem;
}

.colors-left {
  display: flex;
  gap: 0.55rem;
  flex-shrink: 0;
  align-items: flex-end;
}

.control-group--stroke,
.control-group--focus {
  flex: 1;
  min-width: 0;
}

/* ── Form elements ── */

select,
input[type="color"] {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 0.85rem;
}

select {
  padding: 0.28rem 0.4rem;
  width: 100%;
  cursor: pointer;
  height: 36px;
}

input[type="color"] {
  width: 40px;
  height: 36px;
  padding: 2px 3px;
  cursor: pointer;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  /* Match the height of color inputs (36px) so all controls on a row align. */
  height: 36px;
  background: transparent;
  outline: none;
  cursor: pointer;
  padding: 0;
  margin: 0;
  display: block;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}

input[type="range"]::-moz-range-track {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 32px;
  border-radius: 4px;
  background: var(--accent);
  cursor: pointer;
  margin-top: -14px; /* center on 4px track: -(32-4)/2 */
}

input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 32px;
  border-radius: 4px;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

/* ── Align buttons ── */

.btn-group {
  display: flex;
  gap: 2px;
}

.align-btn {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
  font-size: 1rem;
  width: 36px;
  height: 36px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s;
}

.align-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.align-btn:active {
  opacity: 0.7;
}

/* ===== iOS SAVE OVERLAY ===== */

#ios-save-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.2rem;
  padding: 1.5rem;
}

#ios-save-overlay img {
  max-width: 100%;
  max-height: 40vh;
  border-radius: var(--radius);
  box-shadow: 0 4px 24px rgba(0,0,0,0.6);
}

#ios-save-overlay p {
  color: #fff;
  font-family: var(--font-ui);
  font-style: italic;
  font-size: 0.95rem;
  text-align: center;
  margin: 0;
}

.ios-overlay-divider {
  color: rgba(255,255,255,0.4) !important;
  font-size: 0.75rem !important;
}

.ios-open-btn {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 0.7rem 2.2rem;
  font-family: var(--font-ui);
  font-size: 1.05rem;
  text-decoration: none;
  cursor: pointer;
  letter-spacing: 0.02em;
}

.ios-open-btn:active {
  opacity: 0.8;
}

#ios-save-overlay button {
  background: transparent;
  color: rgba(255,255,255,0.5);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: var(--radius);
  padding: 0.5rem 1.5rem;
  font-family: var(--font-ui);
  font-size: 0.9rem;
  cursor: pointer;
}

/* ===== Utility ===== */
.hidden { display: none !important; }

/* ===== DESKTOP tweaks ===== */

@media (min-width: 600px) {
  :root {
    --panel-h: 252px;
  }

  #base-image {
    max-height: calc(100vh - var(--top-bar-h) - var(--panel-h) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 30px);
  }
}
