/* ---------------------------------------------------------------
 * base.css — shared shell for every tool page (and the home page).
 * Owns: theme tokens, page background, hero/topbar (lang switch +
 * home link), card surface, buttons, status cards, modal chrome,
 * generic utilities.
 * Tool-specific styles live in their own file (e.g. wordgraph.css).
 * --------------------------------------------------------------- */

/* Flag emoji fallback. Windows' system emoji font (Segoe UI Emoji) has NO
 * flag glyphs — it renders a flag's two Regional Indicators as the two
 * letters ("PT"). This font (flags only, ~78 KB, derived from Twemoji,
 * CC-BY 4.0) supplies them. It's only PREPENDED to the emoji font stack
 * when the browser can't render flags natively (see csv-utils.js adding
 * `html.no-flag-emoji`), so platforms that already show flags (macOS, iOS,
 * Android…) keep their native glyphs. The url is relative to this CSS file
 * so it resolves correctly under any ROOT_PATH. */
@font-face {
  font-family: "Twemoji Country Flags";
  src: url("../fonts/TwemojiCountryFlags.woff2") format("woff2");
  unicode-range: U+1F1E6-1F1FF;   /* Regional Indicator Symbols only */
  font-display: swap;
}

:root {
  --bg-1: #0f172a;
  --bg-2: #111827;
  --bg-3: #1f2937;
  --surface: rgba(255, 255, 255, 0.08);
  --surface-strong: #ffffff;
  --surface-soft: #f8fafc;
  --surface-muted: #f1f5f9;
  --border: #e5e7eb;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-soft: #475569;
  --text-faint: #64748b;
  --primary: #111827;
  --primary-2: #334155;
  --primary-contrast: #ffffff;
  --success-bg: #eff6ff;
  --success-border: #93c5fd;
  --error-bg: #fef2f2;
  --error-border: #fca5a5;
  --error-text: #991b1b;
  --shadow-lg: 0 30px 80px rgba(2, 6, 23, 0.35);
  --shadow-md: 0 16px 40px rgba(15, 23, 42, 0.14);
  --shadow-sm: 0 8px 20px rgba(15, 23, 42, 0.08);
  --radius-xl: 24px;
  --radius-lg: 18px;
  --radius-md: 14px;
  --radius-sm: 10px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(148, 163, 184, 0.16), transparent 28%),
    radial-gradient(circle at top right, rgba(255, 255, 255, 0.08), transparent 18%),
    linear-gradient(135deg, var(--bg-1) 0%, var(--bg-2) 48%, var(--bg-3) 100%);
  min-height: 100vh;
}

/* Windows has no native flag glyphs (it shows "PT" for 🇵🇹). When detection
 * flags that (html.no-flag-emoji, set in csv-utils.js), route flag
 * codepoints through the bundled flag font everywhere on the page. The
 * @font-face's unicode-range limits the font to flag codepoints, so all
 * other text keeps the body font. (The word cloud overrides the emoji font
 * itself — see wordcloud.css.) */
html.no-flag-emoji body {
  font-family: "Twemoji Country Flags", Inter, system-ui, -apple-system,
               BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

a { text-decoration: none; color: inherit; }

/* ---------- shell ---------- */

.page-shell {
  min-height: 100vh;
  padding: 32px 16px 48px;
}

.container {
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
}

.hero {
  margin-bottom: 20px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 12px;
  margin-bottom: 20px;
}

/* lang switch always pinned right, regardless of whether a home link is present */
.lang-switch {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 30px rgba(0,0,0,0.18);
}

.lang-link { text-decoration: none; }

.lang-flag {
  min-width: 74px;
  height: 42px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: 999px;
  font-size: 18px;
  line-height: 1;
  color: #fff;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.14);
  transition: transform .18s ease, background .18s ease, border-color .18s ease, box-shadow .18s ease;
}

.lang-flag:hover,
.lang-flag:focus-visible {
  transform: translateY(-1px);
  background: rgba(255,255,255,0.14);
  border-color: rgba(255,255,255,0.28);
  box-shadow: 0 12px 24px rgba(0,0,0,0.20);
  outline: none;
}

.lang-flag.active {
  background: rgba(255,255,255,0.20);
  border-color: rgba(255,255,255,0.38);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.14), 0 12px 24px rgba(0,0,0,0.20);
}

.lang-code {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.flag-img {
  display: block;
  width: 22px;
  height: 16px;
  border-radius: 2px;
}

/* home link (top-left). Height matches .lang-switch (58 px = 42 px flag
 * + 8 px padding × 2 + 2 px border) so both chips on the topbar share
 * the same baseline, but the width is content-driven — natural icon +
 * label + side padding — rather than forced to mirror the lang-switch
 * pair, which felt unnecessarily chunky for a single button. */
.home-link {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  height: 58px;
  padding: 0 18px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.14);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.04em;
  text-decoration: none;
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 30px rgba(0,0,0,0.18);
  transition: transform .18s ease, background .18s ease, border-color .18s ease, box-shadow .18s ease;
}

.home-link:hover,
.home-link:focus-visible {
  transform: translateY(-1px);
  background: rgba(255,255,255,0.16);
  border-color: rgba(255,255,255,0.28);
  box-shadow: 0 12px 24px rgba(0,0,0,0.20);
  outline: none;
}

.home-icon {
  font-size: 18px;
  line-height: 1;
  transform: translateY(-1px);
}

/* Platform brand (home page only). Plain wordmark — no chrome. */
.platform-brand {
  display: inline-flex;
  align-items: center;
  color: #fff;
  font-weight: 800;
  font-size: 1.7rem;
  line-height: 1;
  letter-spacing: -0.025em;
  text-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}

.hero-copy {
  text-align: center;
  color: #fff;
  padding: 16px 0 6px;
}

.hero-subtitle {
  margin: 10px 0 0;
  color: rgba(255,255,255,0.78);
  font-size: 1.05rem;
  font-weight: 500;
}

h1 {
  margin: 0;
  color: #fff;
  text-align: center;
  font-size: clamp(2rem, 5vw, 3.4rem);
  line-height: 1.05;
  letter-spacing: -0.03em;
  text-shadow: 0 10px 24px rgba(0,0,0,0.22);
}

/* ---------- card surface ---------- */

.card {
  background: rgba(255, 255, 255, 0.97);
  width: 100%;
  padding: 28px;
  border-radius: var(--radius-xl);
  border: 1px solid rgba(255,255,255,0.28);
  box-shadow: var(--shadow-lg);
  backdrop-filter: blur(12px);
}

/* ---------- buttons ---------- */

.btn,
.btn-secondary {
  border: 0;
  padding: 0.95rem 1.35rem;
  border-radius: 14px;
  cursor: pointer;
  font-weight: 800;
  font-size: 0.96rem;
  line-height: 1;
  transition: transform 0.18s ease, box-shadow 0.18s ease, background 0.18s ease, opacity 0.18s ease;
}

.btn {
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
  color: var(--primary-contrast);
  box-shadow: 0 12px 24px rgba(15, 23, 42, 0.24);
}

.btn:hover { transform: translateY(-2px); box-shadow: 0 18px 30px rgba(15, 23, 42, 0.28); }
.btn:active { transform: translateY(0); }

.btn:disabled,
.btn[disabled] {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
  box-shadow: none;
}

.btn-secondary {
  background: #e2e8f0;
  color: #0f172a;
  border: 1px solid #cbd5e1;
}

.btn-secondary:hover {
  background: #dbe4ee;
  transform: translateY(-2px);
}

.actions-row {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  margin-top: 24px;
}

/* ---------- form layout primitives (shared by all tools) ----------
 * Sections, headings, two-column rows, panel surfaces. Keep these
 * here so a new tool only needs to write its tool-specific bits.
 * ----------------------------------------------------------------- */

.form-section { position: relative; }

.form-section + .form-section { margin-top: 28px; }

.form-section-separated {
  padding-top: 24px;
  border-top: 1px solid #e5e7eb;
}

.section-heading {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
  margin-left: 5px;
}

.section-title {
  /* flex so an inline .help-icon (used on /extract) vertically centres
   * against the text — without this the icon sits on the text baseline
   * (default `vertical-align: baseline` for inline-flex), visibly low.
   * For text-only section titles (every other section in every tool)
   * flex behaves identically to block — the text becomes a single
   * anonymous flex item. flex-wrap: wrap keeps very long titles + a
   * help-icon usable on narrow viewports. */
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin: 0;
  font-size: 1.2rem;
  line-height: 1.2;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: #0f172a;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
}

.row-2cols {
  display: flex;
  gap: 16px;
  /* `flex-start` (not stretch): a card growing — e.g. when the wordcloud's
   * custom-colour pickers appear — must NOT drag the other cards taller. */
  align-items: flex-start;
  flex-wrap: wrap;
}

.row-2cols > div {
  flex: 1 1 280px;
  min-width: 220px;
}

.input-panel,
.setting-card {
  background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
  border: 1px solid #e2e8f0;
  border-radius: 18px;
  padding: 18px;
  box-shadow: var(--shadow-sm);
}

/* ---------- inputs (shared baseline) ---------- */

label {
  font-weight: 700;
  display: block;
  margin-bottom: 0.55rem;
  color: var(--text);
  font-size: 0.95rem;
}

input[type="number"],
input[type="text"],
textarea,
.select-input {
  width: 100%;
  padding: 0.9rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 0.96rem;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease, transform 0.2s ease;
  font-family: inherit;
  background: #fff;
  color: var(--text);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

input[type="number"]:hover,
input[type="text"]:hover,
textarea:hover,
.select-input:hover {
  border-color: var(--border-strong);
}

input[type="number"]:focus,
input[type="text"]:focus,
textarea:focus,
.select-input:focus,
button:focus-visible,
a:focus-visible,
input[type="checkbox"]:focus-visible,
input[type="radio"]:focus-visible,
.close:focus-visible {
  outline: none;
  border-color: #94a3b8;
  box-shadow: 0 0 0 4px rgba(148, 163, 184, 0.22);
}

textarea {
  min-height: 200px;
  resize: vertical;
  line-height: 1.55;
}

input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: #0f172a;
  cursor: pointer;
}

/* ---------- status cards ---------- */

.hint {
  color: var(--text-faint);
  font-size: 0.875rem;
  margin-top: 0.25rem;
  line-height: 1.5;
}

.status-block { margin-top: 18px; }

.status-card {
  padding: 1rem 1.15rem;
  border-radius: 16px;
  margin-top: 1rem;
}

.ok {
  background: var(--success-bg);
  border: 1px solid var(--success-border);
}

.ok strong { color: #0f172a; font-size: 1.05rem; }

.ok a {
  color: #0f172a;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.ok a:hover { color: #020617; }

.err {
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  color: var(--error-text);
  font-weight: 600;
}

/* On a small screen the error box can be below the fold when the user
 * clicks "Generate" at the top of a long form. JS scrolls it into view;
 * this animation makes the box pulse briefly so the eye catches the
 * change even if the scroll itself is short. Auto-disables for users who
 * have prefers-reduced-motion turned on. */
@keyframes err-flash {
  0%   { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.55); transform: scale(1); }
  30%  { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); transform: scale(1.01); }
  100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0);   transform: scale(1); }
}
.err.is-flash { animation: err-flash 0.9s ease-out; }
@media (prefers-reduced-motion: reduce) {
  .err.is-flash { animation: none; }
}

/* ---------- modals ---------- */

.modal {
  display: none;
  position: fixed;
  z-index: 1000;
  inset: 0;
  overflow: auto;
  background-color: rgba(2, 6, 23, 0.62);
  backdrop-filter: blur(8px);
  padding: 24px 16px;
}

.modal.is-open { display: block; }

.modal-content {
  background-color: #fff;
  margin: 4vh auto;
  padding: 24px;
  border: 1px solid rgba(255,255,255,0.5);
  border-radius: 24px;
  width: 100%;
  max-width: 720px;
  box-shadow: 0 30px 80px rgba(2, 6, 23, 0.36);
  animation: slideDown 0.24s ease;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-14px) scale(0.985); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  margin-bottom: 1rem;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 0.9rem;
}

.modal-header h2 {
  margin: 0;
  color: var(--text);
  font-size: 1.35rem;
  letter-spacing: -0.02em;
}

.modal-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.close {
  appearance: none;
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  color: #475569;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  font-size: 28px;
  font-weight: 400;
  cursor: pointer;
  transition: transform 0.18s ease, background-color 0.18s ease, color 0.18s ease;
  line-height: 1;
}

.close:hover {
  color: #0f172a;
  background: #e2e8f0;
  transform: rotate(90deg);
}

/* ---------- generic utilities ---------- */

.hidden { display: none !important; }

.spacer-1  { height: 1rem; }
.spacer-06 { height: .6rem; }

.mt-03 { margin-top: .3rem !important; }
.mt-04 { margin-top: .4rem !important; }
.mt-06 { margin-top: .6rem !important; }
.mt-1  { margin-top: 1rem !important; }

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

/* ---------- responsive ---------- */

@media (max-width: 768px) {
  .page-shell { padding-top: 20px; }

  .card {
    padding: 18px;
    border-radius: 20px;
  }

  .row { grid-template-columns: 1fr; }

  .actions-row,
  .modal-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .actions-row .btn,
  .actions-row .btn-secondary,
  .modal-actions .btn,
  .modal-actions .btn-secondary {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .lang-flag { min-width: 62px; padding: 0 10px; }
  .lang-code { display: none; }
  .home-label { display: none; }
  /* On mobile the label hides → only the ← icon remains, so the chip
   * collapses to a tap-friendly width. Height stays 58 px from the
   * desktop rule so the two topbar chips remain aligned. */
  .home-link { padding: 0 14px; }

  h1 { font-size: 2rem; }

  .modal-content {
    padding: 18px;
    border-radius: 18px;
  }
}

/* ---------- × clear button inside the file-input upload zone ----------
 * NOTE: don't add `position: relative` to .file-input-label — it would
 * stack on top of the absolutely-positioned <input type="file"> and steal
 * the click. The button positions against .file-input-wrapper (which is
 * already relative) and sits visually in the same place. z-index: 3 keeps
 * it on top of the invisible file input so the × is itself clickable. */
.file-clear {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: 0;
  background: rgba(15, 23, 42, 0.06);
  color: var(--text-faint);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 999px;
  display: none;
  align-items: center;
  justify-content: center;
  transition: background 0.12s ease, color 0.12s ease;
  z-index: 3;
}
.file-input-label.has-file .file-clear { display: inline-flex; }
.file-clear:hover { background: #fee2e2; color: #b91c1c; }
