/*
 * Web Countdown — configuration mode styling.
 * Self-contained (no shared portal CSS import), but echoes the portal's
 * paper/ink visual language so index.html feels integrated (PRD Q10).
 */
:root {
  --paper: #f5f1e8;
  --paper-raised: #fffdf7;
  --ink: #1c1a15;
  --ink-soft: #5a5646;
  --ink-faint: #8a8570;
  --rule: #d8d0bc;
  --rule-strong: #b9ae8c;
  --accent: #b5482f;
  --accent-soft: #f0ded7;
  --focus: #1c1a15;
  --radius-sm: 3px;
  --radius-md: 6px;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --font-mono: "SFMono-Regular", "JetBrains Mono", ui-monospace, "Cascadia Mono", monospace;
}

/* ---------- Dark theme ---------- */
html[data-theme="dark"] {
  --paper: #1b1914;
  --paper-raised: #262319;
  --ink: #ece6d6;
  --ink-soft: #b7ae95;
  --ink-faint: #8f8870;
  --rule: #3a362a;
  --rule-strong: #55503e;
  --accent: #e2825f;
  --accent-soft: #3a2620;
  --focus: #ece6d6;
}

* { box-sizing: border-box; }
html { color-scheme: light; }
html[data-theme="dark"] { color-scheme: dark; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-serif);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}
/* Thai script (Kai.md Complex-Script-Kernwissen) */
html[lang="th"] body {
  font-family: var(--font-thai), var(--font-serif);
  line-height: 1.8;
}
html[lang="th"] .field label, html[lang="th"] legend, html[lang="th"] .lang-badge,
html[lang="th"] .btn, html[lang="th"] .check-item { overflow: visible; }

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

.page { max-width: 1100px; margin: 0 auto; padding: 2rem 1.5rem 5rem; }

/* ---------- Language badges ---------- */
.lang-badges { display: flex; gap: 0.4rem; }
.lang-badge {
  font-family: var(--font-mono); font-size: 0.72rem; letter-spacing: 0.04em;
  border: 1px solid var(--rule-strong); border-radius: 999px;
  padding: 0.3rem 0.75rem; background: var(--paper-raised); color: var(--ink-soft);
  cursor: pointer; transition: transform 0.1s ease, background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}
.lang-badge:hover { border-color: var(--ink); color: var(--ink); }
.lang-badge:active { transform: scale(0.96); }
.lang-badge:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.lang-badge.is-active { background: var(--ink); color: var(--paper); border-color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
  .lang-badge { transition: none; }
}

/* ---------- Masthead ---------- */
.masthead { padding-bottom: 0; margin-top: 1.25rem; }
.masthead-rule { border: none; border-top: 2px solid var(--ink); margin: 0 0 2rem; }
.masthead-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.masthead-brand { display: flex; align-items: center; gap: 0.65rem; min-width: 0; }

.home-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2.1rem; height: 2.1rem; flex-shrink: 0; border-radius: 50%;
  color: var(--ink-soft); text-decoration: none;
  transition: background-color 0.12s ease, color 0.12s ease;
}
.home-icon:hover { background: var(--accent-soft); color: var(--ink); }
.home-icon:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { .home-icon { transition: none; } }

.brand-tile {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2.1rem; height: 2.1rem; flex-shrink: 0; border-radius: var(--radius-md);
  background: var(--ink); color: var(--paper); font-size: 1.05rem;
}

.brand-tile img {
  display: block;
  width: 1.25rem;
  height: 1.25rem;
  /* External <img> SVGs do not inherit currentColor. Brand-tile is ink-on-paper
     inverted (dark tile / paper glyph). Force monochrome paint to match. */
  filter: brightness(0) invert(1);
}
html[data-theme="dark"] .brand-tile img {
  /* Dark theme: --ink tile is light; keep icon dark. */
  filter: brightness(0);
}


.brand-title {
  font-size: clamp(1.3rem, 3.2vw, 1.6rem); margin: 0; min-width: 0; font-weight: 700;
  letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.masthead-actions { display: flex; align-items: center; gap: 0.75rem; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }

/* Theme toggle */
.theme-toggle {
  position: relative; width: 2.1rem; height: 2.1rem; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--rule-strong); border-radius: 50%;
  background: var(--paper-raised); color: var(--ink-soft); cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, transform 0.1s ease;
}
.theme-toggle:hover { border-color: var(--ink); color: var(--ink); }
.theme-toggle:active { transform: scale(0.94); }
.theme-toggle:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.theme-icon { position: absolute; width: 1.1rem; height: 1.1rem; transition: opacity 0.15s ease, transform 0.15s ease; }
.theme-icon--sun { opacity: 1; transform: scale(1) rotate(0deg); }
.theme-icon--moon { opacity: 0; transform: scale(0.5) rotate(-40deg); }
html[data-theme="dark"] .theme-icon--sun { opacity: 0; transform: scale(0.5) rotate(40deg); }
html[data-theme="dark"] .theme-icon--moon { opacity: 1; transform: scale(1) rotate(0deg); }
@media (prefers-reduced-motion: reduce) {
  .theme-toggle, .theme-icon { transition: none; }
}

.masthead-sub { color: var(--ink-soft); margin: 0.6rem 0 0; max-width: 60ch; }

/* ---------- 2-column layout (≥1024px) ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1.75rem;
}

/* Mobile: preview-col first (top), form below */
@media (min-width: 1024px) {
  .layout {
    /* Left: config form (~460px), Right: preview+share (fills rest) */
    grid-template-columns: minmax(0, 460px) minmax(0, 1fr);
    gap: 2.5rem;
    /* Kein align-items:start — die Grid-Items müssen die volle Zeilenhöhe
       einnehmen, sonst hat die sticky Spalte keinen Scroll-Spielraum. */
  }
  #cd-form { grid-column: 1; grid-row: 1; }
  .preview-col { grid-column: 2; grid-row: 1; }
}

/* ---------- Sticky preview column ---------- */
.preview-col { min-width: 0; }

@media (min-width: 1024px) {
  .preview-sticky {
    position: sticky;
    top: 1.25rem;
    /* Max height so sticky col never overflows viewport; allow inner scroll
       only if content is taller (rare on normal viewports). */
    max-height: calc(100vh - 2.5rem);
    overflow-y: auto;
    overflow-x: hidden;
    /* Subtle scrollbar in dark mode */
    scrollbar-width: thin;
    scrollbar-color: var(--rule-strong) transparent;
  }
}

/* ---------- Fieldsets & cards ---------- */
fieldset {
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  padding: 1rem 1.1rem 1.25rem;
  margin: 0 0 1rem;
  background: var(--paper-raised);
  min-width: 0;
}
legend {
  font-family: var(--font-mono); font-size: 0.72rem; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--accent); padding: 0 0.4rem;
}

/* ---------- Form fields ---------- */
.field { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.85rem; }
.field:last-child { margin-bottom: 0; }
.field label { font-size: 0.85rem; color: var(--ink-soft); }
.field input[type="text"],
.field input[type="date"],
.field input[type="time"],
.field input[type="color"],
.field select {
  font-family: var(--font-serif);
  font-size: 0.95rem;
  padding: 0.55rem 0.7rem;
  background: var(--paper-raised);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  color: var(--ink);
}
.field input:focus-visible, .field select:focus-visible {
  outline: 2px solid var(--focus); outline-offset: 1px;
}
.field-row { display: flex; gap: 0.75rem; }
.field-row .field { flex: 1; min-width: 0; }
.field-row .field input { width: 100%; }

.check-row { display: flex; flex-wrap: wrap; gap: 0.5rem 1.1rem; }
.check-item { display: flex; align-items: center; gap: 0.4rem; font-size: 0.9rem; }

.check-row--recurring {
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--rule);
}

/* Count-up / Confetti (in Nach Ablauf) */
.check-row--extras {
  margin-top: 0.75rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--rule);
}

/* ---------- Design grid ---------- */
.design-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 0.6rem; margin-bottom: 0.85rem; }
.design-card {
  border: 1px solid var(--rule-strong); border-radius: var(--radius-md);
  padding: 0.7rem 0.6rem; background: var(--paper-raised); cursor: pointer; text-align: center;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
.design-card input { position: absolute; opacity: 0; pointer-events: none; }
.design-card .swatch { display: block; height: 2.2rem; border-radius: var(--radius-sm); margin-bottom: 0.5rem; }
.design-card span { font-size: 0.8rem; }
.design-card.is-selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.design-card:focus-within { outline: 2px solid var(--focus); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { .design-card { transition: none; } }

.sw-classic { background: linear-gradient(135deg, #fff 50%, #111 50%); }
.sw-minimal { background: linear-gradient(135deg, #fff 50%, #ccc 50%); }
.sw-flip { background: linear-gradient(135deg, #1a1a1a 50%, #f0c040 50%); }
.sw-neon { background: linear-gradient(135deg, #000 50%, #0ff 50%); }
.sw-rings { background: linear-gradient(135deg, #f7f7f9 50%, #3b82f6 50%); }

/* ---------- Advanced fieldset (collapsible) ---------- */
.advanced-fieldset legend { width: 100%; }

.adv-toggle {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  width: 100%;
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  font-weight: 600;
  color: var(--ink-soft);
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.adv-toggle:hover { color: var(--ink); }
.adv-toggle:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.adv-chevron {
  margin-left: auto;
  flex: none;
  transition: transform 0.2s ease;
  color: var(--accent);
}
.advanced-fieldset--collapsed .adv-chevron { transform: rotate(-90deg); }
@media (prefers-reduced-motion: reduce) { .adv-chevron { transition: none; } }

.advanced-fieldset--collapsed .adv-panel-body { display: none; }

.adv-panel-body { padding-top: 0.5rem; }
.adv-panel-body .check-row { margin-bottom: 0.6rem; }
.adv-encoding-row { margin-top: 0.25rem; }

/* ---------- Preview column: iframe + share fieldset ---------- */
.preview-frame-wrap {
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: var(--radius-md);
  overflow: hidden;
  margin-bottom: 1rem;
}
.preview-frame-wrap iframe { width: 100%; height: 100%; border: 0; display: block; }

.share-fieldset {
  /* Inherits fieldset base styles */
  padding: 1rem 1.1rem 0.75rem;
  margin-bottom: 0;
}

/* ---------- URL block ---------- */
.url-block { margin-bottom: 0.5rem; }
.url-row { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.75rem; }
.url-row label { font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-faint); }
.url-copy-row { display: flex; gap: 0.5rem; }
.url-copy-row input {
  flex: 1; min-width: 0; font-family: var(--font-mono); font-size: 0.78rem; padding: 0.45rem 0.55rem;
  border: 1px solid var(--rule-strong); border-radius: var(--radius-sm);
  background: var(--paper); color: var(--ink);
}

/* Embed row */
.url-embed-row label { font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-faint); }
.url-embed-copy { display: flex; align-items: center; gap: 0.65rem; flex-wrap: wrap; }
.url-embed-hint { font-family: var(--font-mono); font-size: 0.68rem; color: var(--ink-faint); margin: 0; }

/* QR code */
.url-qr-row label { font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-faint); }
#qr-box {
  display: inline-block; background: #fff; border-radius: var(--radius-sm);
  padding: 0.35rem; border: 1px solid var(--rule-strong); line-height: 0;
}
#qr-box:empty { display: none; }
#qr-box svg { width: 140px; height: 140px; display: block; }

/* Encoding toggle (now in advanced panel) */
.encoding-toggle { display: flex; align-items: center; gap: 0.4rem; font-size: 0.8rem; color: var(--ink-soft); cursor: pointer; }
.encoding-toggle input[type="checkbox"] { width: 1rem; height: 1rem; }

/* ---------- Buttons ---------- */
.btn {
  font-family: var(--font-mono); font-size: 0.78rem; letter-spacing: 0.02em;
  border: 1px solid var(--rule-strong); border-radius: var(--radius-sm);
  padding: 0.5rem 0.85rem; background: var(--paper-raised); color: var(--ink);
  cursor: pointer; transition: transform 0.1s ease, background-color 0.12s ease, border-color 0.12s ease;
  white-space: nowrap;
}
.btn:hover { border-color: var(--ink); }
.btn:active { transform: scale(0.97); }
.btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.btn--primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn--primary:hover { background: #000; }
html[data-theme="dark"] .btn--primary:hover { background: #f5f1e8; color: #1b1914; }
.btn--ghost { background: transparent; }
@media (prefers-reduced-motion: reduce) { .btn { transition: none; } }

/* ---------- Share row & panels ---------- */
.share-row {
  display: flex; flex-wrap: wrap; gap: 0.45rem; align-items: center;
  padding: 0 0 0.75rem;
}

.info-hint {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 1.6rem; height: 1.6rem; border-radius: 50%; border: 1px solid var(--rule-strong);
  background: var(--paper-raised); color: var(--ink-soft); font-family: var(--font-mono); font-size: 0.8rem; cursor: help;
  flex-shrink: 0;
}
.info-hint .tooltip {
  position: absolute; bottom: 130%; left: 50%; transform: translateX(-50%);
  width: 15rem; background: var(--ink); color: var(--paper); font-family: var(--font-serif);
  font-size: 0.78rem; padding: 0.5rem 0.65rem; border-radius: var(--radius-sm);
  opacity: 0; pointer-events: none; transition: opacity 0.12s ease;
  z-index: 5;
}
.info-hint:hover .tooltip, .info-hint:focus-visible .tooltip { opacity: 1; }
@media (prefers-reduced-motion: reduce) { .info-hint .tooltip { transition: none; } }

.share-panel {
  display: flex; flex-wrap: wrap; gap: 0.5rem; padding-bottom: 0.75rem;
}
.share-panel[hidden] { display: none; }
.share-link {
  display: inline-flex; align-items: center; gap: 0.4rem;
  font-family: var(--font-mono); font-size: 0.78rem;
  border: 1px solid var(--rule-strong); border-radius: var(--radius-sm);
  padding: 0.45rem 0.75rem; background: var(--paper-raised); color: var(--ink); text-decoration: none;
}
.share-link:hover { border-color: var(--ink); }
.share-link:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.info-line {
  margin: 0 0 0.5rem;
  font-size: 0.78rem;
  color: var(--ink-faint);
  line-height: 1.45;
}
.gif-hint { margin-top: 0.1rem; }

.status-msg {
  font-family: var(--font-mono); font-size: 0.78rem; color: var(--accent);
  padding: 0.25rem 0 0.5rem; min-height: 1.2em;
}

/* ---------- Footer ---------- */
.page-footer {
  margin-top: 3rem; padding-top: 1.25rem; border-top: 1px solid var(--rule);
  text-align: center; font-family: var(--font-mono); font-size: 0.75rem; color: var(--ink-faint);
}
.app-version { margin-top: 0.35rem; opacity: 0.7; }
.app-version:empty { display: none; }

/* ---------- Mobile (320–500px) ---------- */
@media (max-width: 500px) {
  .home-icon, .theme-toggle { width: 2.75rem; height: 2.75rem; }
  .masthead-actions { margin-left: auto; }
  fieldset { padding: 0.85rem 0.9rem 1rem; }
}

/* ---------- Template picker ---------- */
.tpl-picker { display: flex; flex-direction: column; gap: 0.35rem; }
.tpl-cat {
  margin: 0.5rem 0 0.15rem; font-size: 0.8rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-faint);
}
.tpl-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr));
  gap: 0.45rem;
}
.tpl-card {
  display: flex; flex-direction: column; align-items: flex-start; gap: 0.15rem;
  padding: 0.5rem 0.65rem; border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm); background: var(--paper-raised); color: var(--ink);
  font: inherit; text-align: left; cursor: pointer; min-height: 2.75rem;
}
.tpl-card:hover { border-color: var(--accent); }
.tpl-card:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.tpl-card.is-selected { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.tpl-card--none { border-style: dashed; color: var(--ink-soft); font-size: 0.85rem; }
.tpl-card-title { font-weight: 600; font-size: 0.9rem; line-height: 1.25; }
.tpl-card-date { font-size: 0.78rem; color: var(--ink-faint); }
.tpl-card-date:empty { display: none; }
.tpl-card--foreign { border-style: dashed; border-color: var(--accent); }

.tpl-info { margin-top: 0.7rem; padding: 0.55rem 0.75rem; border-left: 3px solid var(--accent); background: var(--accent-soft); border-radius: var(--radius-sm); }
.tpl-info-text { margin: 0; font-size: 0.88rem; }
.tpl-info-row { display: flex; align-items: center; gap: 0.75rem; margin-top: 0.4rem; }
.tpl-badge { font-size: 0.78rem; font-weight: 700; color: var(--accent); }
.tpl-reset { background: none; border: none; padding: 0; font: inherit; font-size: 0.82rem; text-decoration: underline; color: var(--accent); cursor: pointer; }
.tpl-missing { margin: 0.5rem 0 0; font-size: 0.85rem; color: var(--accent); }

/* Overridden template field highlight */
.field--overridden > label, .field--overridden > .check-row { color: var(--accent); }
.field--overridden input, .field--overridden select { border-color: var(--accent); }

/* Collapsible template panel */
.tpl-fieldset legend { width: 100%; }
.tpl-toggle {
  display: flex; align-items: center; gap: 0.45rem; width: 100%;
  background: none; border: 0; padding: 0; font: inherit; font-weight: 600;
  color: var(--ink); cursor: pointer;
}
.tpl-toggle:hover { color: var(--accent, #4a6fa5); }
.tpl-toggle:focus-visible { outline: 2px solid var(--accent, #4a6fa5); outline-offset: 2px; }
.tpl-chevron {
  margin-left: auto; transition: transform 0.2s ease; flex: none;
}
.tpl-fieldset--collapsed .tpl-chevron { transform: rotate(-90deg); }
@media (prefers-reduced-motion: reduce) { .tpl-chevron { transition: none; } }

.tpl-toggle-badge {
  font-size: 0.72rem; font-weight: 600; padding: 0.1rem 0.5rem; border-radius: 999px;
  background: var(--accent, #4a6fa5); color: #fff; max-width: 16ch;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tpl-fieldset--collapsed .tpl-panel-body { display: none; }
/* Dark mode: accent #e2825f needs dark text for AA contrast */
html[data-theme="dark"] .tpl-toggle-badge { color: #1c1008; }
