/* style.css — flagme
   One stylesheet, no framework. Light and dark are driven entirely by prefers-color-scheme.
   Colour pairs are chosen to clear WCAG 2.1 AA: 4.5:1 for text, 3:1 for control boundaries. */

@layer reset, base, components;

@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
    margin: 0;
  }

  button,
  input {
    font: inherit;
    color: inherit;
  }

  ul {
    padding: 0;
    list-style: none;
  }
}

@layer base {
  :root {
    color-scheme: light dark;

    --ink: light-dark(#1b1033, #f4efff);
    --ink-soft: light-dark(#544c6b, #b6adcd);
    --paper: light-dark(#fffdfa, #17122a);
    --card: light-dark(#ffffff, #211a3b);
    --field: light-dark(#ffffff, #1a1533);

    /* Decorative dividers. */
    --edge: light-dark(#e8e2f5, #342a58);
    /* Boundaries of interactive controls — must hold 3:1 against their background. */
    --edge-strong: light-dark(#8b82a6, #7d70ad);

    --brand: light-dark(#e11d48, #fb7185);
    --brand-2: light-dark(#7c3aed, #a78bfa);
    --brand-3: light-dark(#0891b2, #22d3ee);
    /* Fixed stops for white text on a coloured fill, in either scheme. */
    --fill-1: #e11d48;
    --fill-2: #7c3aed;

    --radius: 1.25rem;
    --shadow: 0 1px 2px rgb(27 16 51 / 6%), 0 12px 32px -12px rgb(27 16 51 / 18%);
  }

  body {
    min-height: 100dvh;
    padding: clamp(1rem, 4vw, 3rem) clamp(0.75rem, 4vw, 2rem) 4rem;
    background:
      radial-gradient(60vw 60vw at 12% -10%, color-mix(in oklab, var(--fill-1) 20%, transparent), transparent 70%),
      radial-gradient(50vw 50vw at 95% 0%, color-mix(in oklab, var(--brand-3) 18%, transparent), transparent 70%),
      radial-gradient(70vw 50vw at 50% 110%, color-mix(in oklab, var(--fill-2) 16%, transparent), transparent 70%),
      var(--paper);
    background-attachment: fixed;
    color: var(--ink);
    font-family: ui-rounded, "SF Pro Rounded", "Segoe UI Variable", system-ui, -apple-system, sans-serif;
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
  }

  h1,
  h2 {
    line-height: 1.1;
    text-wrap: balance;
  }

  a {
    color: var(--brand);
    text-underline-offset: 0.15em;
  }

  /* A single, unmistakable focus ring for everything that takes focus. Deliberately violet, not
     the rose used for errors — a red ring around a healthy field reads as a problem. */
  :focus-visible {
    outline: 3px solid var(--brand-2);
    outline-offset: 2px;
  }

  kbd {
    padding: 0.1em 0.4em;
    border: 1px solid var(--edge-strong);
    border-radius: 0.4em;
    background: var(--card);
    font-family: ui-monospace, monospace;
    font-size: 0.85em;
  }

  main {
    display: grid;
    gap: clamp(1rem, 3vw, 2rem);
    max-width: 62rem;
    margin-inline: auto;
    grid-template-columns: 1fr;
  }

  @media (width >= 52rem) {
    main {
      align-items: start;
      grid-template-columns: 1.1fr 1fr;
    }

    .output {
      position: sticky;
      top: 2rem;
    }
  }

  footer {
    display: grid;
    gap: 0.4rem;
    justify-items: center;
    max-width: 62rem;
    margin: 2.5rem auto 0;
    color: var(--ink-soft);
    font-size: 0.8rem;
    text-align: center;
  }

  .repo {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    min-height: 2rem;
    font-weight: 600;
  }

  .repo svg {
    width: 1.15em;
    height: 1.15em;
    fill: currentColor;
  }

  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    border: 0;
    clip-path: inset(50%);
    overflow: hidden;
    white-space: nowrap;
  }
}

@layer components {
  /* --- Masthead ---------------------------------------------------------- */

  .masthead {
    max-width: 62rem;
    margin: 0 auto clamp(1.25rem, 4vw, 2.5rem);
    text-align: center;
  }

  .masthead h1 {
    font-size: clamp(2.75rem, 11vw, 4.5rem);
    font-weight: 800;
    letter-spacing: -0.04em;
  }

  .accent {
    background: linear-gradient(100deg, var(--brand), var(--brand-2) 60%, var(--brand-3));
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
  }

  .wave {
    display: inline-block;
    animation: wave 2.6s ease-in-out infinite;
    transform-origin: 70% 80%;
  }

  @keyframes wave {
    0%,
    60%,
    100% {
      transform: rotate(0deg);
    }
    70% {
      transform: rotate(-14deg);
    }
    85% {
      transform: rotate(10deg);
    }
  }

  .tagline {
    color: var(--ink-soft);
    font-size: clamp(0.95rem, 3vw, 1.1rem);
  }

  /* --- Panels ------------------------------------------------------------ */

  .editor,
  .output {
    padding: clamp(1rem, 3vw, 1.5rem);
    border: 1px solid var(--edge);
    border-radius: var(--radius);
    background: color-mix(in oklab, var(--card) 90%, transparent);
    box-shadow: var(--shadow);
    backdrop-filter: blur(12px);
  }

  .editor h2,
  .output h2 {
    margin-bottom: 0.75rem;
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
  }

  #rows {
    display: grid;
    gap: 1rem;
  }

  /* --- Rows -------------------------------------------------------------- */

  .row-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.5rem;
    margin-bottom: 0.35rem;
  }

  .row-head label {
    font-size: 0.9rem;
    font-weight: 700;
  }

  .count {
    color: var(--ink-soft);
    font-size: 0.78rem;
    font-variant-numeric: tabular-nums;
    font-weight: 600;
  }

  .count.done {
    color: var(--brand-2);
  }

  .chips {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin: 0;
  }

  .chips:not(:empty) {
    margin-bottom: 0.45rem;
  }

  .chip {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    min-height: 2rem;
    padding: 0.3rem 0.65rem 0.3rem 0.5rem;
    border: 1px solid var(--edge-strong);
    border-radius: 999px;
    background: var(--field);
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    transition: transform 0.12s, border-color 0.12s, background 0.12s;
    animation: pop 0.18s ease-out;
  }

  .chip:hover,
  .chip:focus-visible {
    border-color: var(--brand);
    background: color-mix(in oklab, var(--brand) 14%, var(--card));
    transform: translateY(-1px);
  }

  .chip .flag {
    font-size: 1.15em;
  }

  .chip .x {
    color: var(--ink-soft);
    font-size: 1.1em;
    line-height: 1;
  }

  @keyframes pop {
    from {
      opacity: 0;
      transform: scale(0.8);
    }
  }

  /* --- Combobox ---------------------------------------------------------- */

  .combo {
    position: relative;
  }

  .entry {
    width: 100%;
    padding: 0.65rem 0.85rem;
    border: 1px solid var(--edge-strong);
    border-radius: 0.85rem;
    background: var(--field);
    transition: border-color 0.15s, box-shadow 0.15s;
  }

  .entry::placeholder {
    color: var(--ink-soft);
    opacity: 1;
  }

  .entry:focus-visible {
    border-color: var(--brand-2);
    outline-offset: 0;
  }

  .entry[aria-invalid="true"] {
    border-color: var(--brand);
    border-width: 2px;
  }

  .entry.shake {
    animation: shake 0.35s;
  }

  @keyframes shake {
    25% {
      transform: translateX(-5px);
    }
    50% {
      transform: translateX(5px);
    }
    75% {
      transform: translateX(-3px);
    }
  }

  /* The listbox sits directly under its own input, matching its width — never adrift. */
  .listbox {
    position: absolute;
    z-index: 20;
    inset-inline: 0;
    top: calc(100% + 0.3rem);
    max-height: min(17rem, 45vh);
    border: 1px solid var(--edge-strong);
    border-radius: 0.85rem;
    background: var(--card);
    box-shadow: 0 12px 28px -10px rgb(27 16 51 / 45%);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .option {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    min-height: 2.75rem;
    padding: 0.5rem 0.8rem;
    border-inline-start: 4px solid transparent;
    cursor: pointer;
    font-size: 0.95rem;
  }

  .option .flag {
    font-size: 1.25em;
  }

  .option:hover {
    background: color-mix(in oklab, var(--brand) 10%, var(--card));
  }

  /* Highlight is never colour alone — the leading bar carries the same meaning. */
  .option[aria-selected="true"] {
    border-inline-start-color: var(--brand);
    background: color-mix(in oklab, var(--brand) 16%, var(--card));
    font-weight: 700;
  }

  .error {
    margin-top: 0.35rem;
    color: var(--brand);
    font-size: 0.8rem;
    font-weight: 600;
  }

  /* --- Meme -------------------------------------------------------------- */

  .meme {
    display: block;
    padding: 1rem;
    border: 2px dashed var(--edge-strong);
    border-radius: 1rem;
    background: var(--field);
    cursor: pointer;
    font-size: clamp(0.95rem, 3.4vw, 1.05rem);
    overflow-wrap: anywhere;
  }

  .meme.empty {
    color: var(--ink-soft);
    cursor: default;
    font-style: italic;
  }

  .meme .line {
    display: block;
  }

  .meme .key {
    font-weight: 600;
  }

  .meme .credit {
    margin-top: 0.9rem;
    color: var(--ink-soft);
    font-size: 0.8rem;
  }

  /* --- Actions ----------------------------------------------------------- */

  .actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.9rem;
  }

  .btn {
    flex: 1 1 auto;
    min-height: 2.75rem;
    padding: 0.65rem 1rem;
    border: 1px solid var(--edge-strong);
    border-radius: 999px;
    background: var(--card);
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 700;
    white-space: nowrap;
    transition: transform 0.12s, box-shadow 0.12s;
  }

  .btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 18px -8px rgb(27 16 51 / 35%);
  }

  .btn:active {
    transform: translateY(0) scale(0.98);
  }

  .btn.primary {
    border-color: transparent;
    background: linear-gradient(100deg, var(--fill-1), var(--fill-2));
    color: #fff;
  }

  .btn.ghost {
    background: transparent;
    color: var(--ink-soft);
  }

  .hint {
    margin: 0.7rem 0 0;
    color: var(--ink-soft);
    font-size: 0.8rem;
  }

  /* --- Toast and confetti ------------------------------------------------ */

  .toast {
    position: fixed;
    bottom: 1.25rem;
    left: 50%;
    z-index: 30;
    max-width: min(90vw, 26rem);
    padding: 0.6rem 1.1rem;
    border-radius: 999px;
    background: light-dark(#1b1033, #f4efff);
    color: light-dark(#fff, #1b1033);
    font-size: 0.9rem;
    font-weight: 600;
    opacity: 0;
    pointer-events: none;
    text-align: center;
    transform: translate(-50%, 1rem);
    transition: opacity 0.2s, transform 0.2s;
  }

  .toast:empty {
    display: none;
  }

  .toast.show {
    opacity: 1;
    transform: translate(-50%, 0);
  }

  .confetti {
    position: fixed;
    top: 100vh;
    z-index: 5;
    animation: fly 1.6s ease-out forwards;
    font-size: 1.6rem;
    pointer-events: none;
  }

  @keyframes fly {
    to {
      opacity: 0;
      transform: translateY(-110vh) rotate(360deg);
    }
  }

  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
    }
  }
}
