:root {
      --bg: #070a10;
      --panel: #111722;
      --surface: #0a1019;
      --surface-raised: #151d2a;
      --output-bg: #1b2534;
      --text: #f4f7fb;
      --muted: #a7b2c5;
      --line: #2b3648;
      --accent: #5b82ff;
      --line-gap: 8px;
      --flag-w: 88px;
      --flag-h: 59px;
      --pennant-w: 132px;
      --sub-w: 92px;
      --word-gap: 20px;
}

* {
      box-sizing: border-box;
      scrollbar-color: #46536a transparent;
}
    html, body { height: 100%; overflow: hidden; }
    html { color-scheme: dark; }
    body {
      margin: 0;
      font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
      background: radial-gradient(circle at top, #10182a 0, var(--bg) 48%);
      color: var(--text);
    }

    .app {
      height: 100vh;
      height: 100dvh;
      display: grid;
      grid-template-columns: clamp(300px, 28vw, 360px) minmax(0, 1fr);
      gap: 20px;
      padding: 20px;
      overflow: hidden;
    }

    .panel {
      background: var(--panel);
      border: 1px solid var(--line);
      border-radius: 18px;
      box-shadow: 0 12px 36px rgba(0, 0, 0, 0.28);
    }

    .controls {
      padding: 18px;
      display: grid;
      grid-template-rows: auto minmax(0, 1fr);
      gap: 16px;
      min-height: 0;
      overflow: hidden;
    }

    .controls-head {
      display: none;
      align-items: start;
      justify-content: space-between;
      gap: 12px;
    }

    .controls-body {
      display: grid;
      gap: 14px;
      align-content: start;
      min-height: 0;
      overflow: auto;
      padding-right: 4px;
    }

    .settings-toggle { display: none; }

    h1, h2 { margin: 0; }
    h1 { font-size: 1.25rem; line-height: 1.2; }
    h2 { font-size: 1.05rem; }

    .field { display: grid; gap: 8px; }
    .field label { font-size: 0.9rem; font-weight: 600; }

    .range-label {
      display: flex;
      align-items: baseline;
      justify-content: space-between;
      gap: 12px;
    }

    .range-label output {
      color: var(--muted);
      font-variant-numeric: tabular-nums;
    }

    .checkbox-label {
      display: flex;
      align-items: center;
      gap: 9px;
      cursor: pointer;
    }

    .checkbox-label input {
      width: 1.05rem;
      height: 1.05rem;
      margin: 0;
      accent-color: var(--accent);
    }

    select {
      width: 100%;
      padding: 10px 12px;
      border: 1px solid var(--line);
      border-radius: 12px;
      background: var(--surface);
      color: inherit;
      font: inherit;
    }

    textarea {
      width: 100%;
      min-height: 120px;
      height: clamp(120px, 20vh, 180px);
      resize: vertical;
      padding: 12px 14px;
      border-radius: 14px;
      border: 1px solid var(--line);
      font: inherit;
      color: inherit;
      background: var(--surface);
      caret-color: #fff;
    }

    .dropzone {
      border: 1.5px dashed #40506b;
      border-radius: 14px;
      padding: 14px;
      color: var(--muted);
      text-align: center;
      background: var(--surface);
      transition: 150ms ease;
    }

    .dropzone.dragover {
      background: #142344;
      border-color: var(--accent);
      color: #a9beff;
    }

    .row {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 12px;
    }

    input[type="range"] { width: 100%; accent-color: var(--accent); }

    .btns {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 10px;
    }

    .btns button {
      width: 100%;
      min-height: 44px;
    }

    button {
      border: 0;
      background: var(--accent);
      color: #fff;
      border-radius: 12px;
      padding: 11px 14px;
      font: inherit;
      font-weight: 600;
      cursor: pointer;
    }

    button.secondary {
      background: #1c2940;
      color: #dbe5ff;
    }

    button:hover { filter: brightness(1.1); }

    button:focus-visible,
    textarea:focus-visible,
    select:focus-visible,
    input:focus-visible {
      outline: 3px solid rgba(91, 130, 255, 0.38);
      outline-offset: 2px;
    }

    .preview-wrap {
      padding: 18px;
      display: grid;
      grid-template-rows: minmax(0, 1fr) auto;
      gap: 16px;
      min-width: 0;
      min-height: 0;
      overflow: hidden;
    }

    .output {
      border: 1px solid var(--line);
      border-radius: 16px;
      background: var(--output-bg);
      min-width: 0;
      min-height: 0;
      padding: 18px;
      overflow: auto;
      scrollbar-gutter: stable;
      display: flex;
      flex-direction: column;
      align-items: stretch;
      gap: var(--line-gap);
    }

    .line {
      display: flex;
      flex-wrap: nowrap;
      flex: 0 0 auto;
      width: max-content;
      min-width: 100%;
      /* Spacing is optically kerned per flag pair and applied as inline
         margins by computeLayout(), so the row adds no gap of its own and
         there is no --gap custom property to fall out of step with it. */
      column-gap: 0;
      align-items: center;
      min-height: var(--flag-h);
    }

    body.flags-rotated .line {
      align-items: flex-start;
    }

    .output .flag-block { gap: 0; }
    .output .flag-label { display: none; }

    .word-gap {
      width: var(--word-gap);
      height: 1px;
      flex: 0 0 auto;
    }

    .flag-block {
      display: grid;
      flex: 0 0 auto;
      gap: 6px;
      justify-items: center;
    }

    .flag-stage {
      --graphic-w: var(--flag-w);
      --graphic-h: var(--flag-h);
      position: relative;
      width: var(--graphic-w);
      height: var(--graphic-h);
    }

    .flag-stage.pennant { --graphic-w: var(--pennant-w); }
    .flag-stage.substitute { --graphic-w: var(--sub-w); }

    .flag-stage > * {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    body.flags-rotated .flag-stage {
      width: var(--graphic-h);
      height: var(--graphic-w);
    }

    body.flags-rotated .flag-stage > * {
      transform: translate(-50%, -50%) rotate(90deg);
    }

    .placeholder-character {
      display: block;
      line-height: 1;
    }

    body.flags-rotated .placeholder-character {
      transform: rotate(-90deg);
    }

    .flag-label {
      font-size: 0.78rem;
      color: var(--muted);
      letter-spacing: 0.04em;
      text-transform: uppercase;
    }

    .flag-svg {
      width: var(--flag-w);
      height: var(--flag-h);
      display: block;
      overflow: visible;
    }

    .flag-svg.pennant {
      width: var(--pennant-w);
    }

    .flag-svg.substitute {
      width: var(--sub-w);
    }

    .placeholder {
      width: var(--flag-w);
      height: var(--flag-h);
      display: grid;
      place-items: center;
      border-radius: 10px;
      border: 1px dashed #46536a;
      color: #f4f7fb;
      background: #111111;
      font-size: 0.9rem;
      text-transform: uppercase;
    }

    .library {
      display: grid;
      grid-template-columns: repeat(10, minmax(0, 1fr));
      gap: 6px;
    }

    .keyboard-panel { min-width: 0; }

    .library-head {
      display: flex;
      align-items: end;
      justify-content: space-between;
      gap: 16px;
      margin-bottom: 10px;
    }

    .keyboard-actions {
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-end;
      gap: 8px;
    }

    .keyboard-actions button { padding: 8px 11px; }

    .mini-card {
      border: 1px solid var(--line);
      border-radius: 9px;
      padding: 4px 2px;
      background: var(--surface-raised);
      color: var(--text);
      display: grid;
      gap: 3px;
      justify-items: center;
      width: 100%;
      min-width: 0;
      font-weight: 400;
      transition: border-color 120ms ease, box-shadow 120ms ease, transform 120ms ease;
    }

    .mini-card:hover {
      border-color: #6f91ff;
      box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);
      transform: translateY(-1px);
    }

    .mini-card:active { transform: translateY(0); }

    .mini-card .flag-stage {
      --graphic-w: 48px;
      --graphic-h: 32px;
    }

    .mini-card .flag-stage.pennant { --graphic-w: 64px; }

    .mini-card .flag-stage.substitute { --graphic-w: 48px; }

    .mini-card .flag-svg,
    .mini-card .placeholder { width: var(--graphic-w); height: var(--graphic-h); }

    .mini-card .flag-label { font-size: 0.66rem; }

    @media (min-width: 761px) and (max-width: 1100px) {
      .library { grid-template-columns: repeat(8, minmax(0, 1fr)); }

      .mini-card .flag-stage {
        --graphic-w: 40px;
        --graphic-h: 27px;
      }

      .mini-card .flag-stage.pennant { --graphic-w: 46px; }
      .mini-card .flag-stage.substitute { --graphic-w: 40px; }
    }

    @media (max-width: 760px) {
      .app {
        grid-template-columns: 1fr;
        grid-template-rows: auto minmax(0, 1fr);
        padding: 8px;
        gap: 8px;
      }

      .controls {
        position: relative;
        z-index: 10;
        padding: 9px 12px;
        overflow: visible;
      }

      .controls-head { display: flex; align-items: center; }
      .settings-toggle { display: block; padding: 7px 10px; }

      .controls-body {
        display: none;
        position: absolute;
        top: calc(100% + 6px);
        left: 0;
        right: 0;
        max-height: calc(100dvh - 72px);
        padding: 14px;
        border: 1px solid var(--line);
        border-radius: 14px;
        background: var(--panel);
        box-shadow: 0 14px 34px rgba(20, 35, 70, 0.2);
        overflow: auto;
      }

      .controls.settings-open .controls-body { display: grid; }

      .preview-wrap {
        padding: 8px;
        gap: 6px;
        border-radius: 14px;
      }


      .output {
        padding: 8px;
        border-radius: 10px;
      }

      .library-head {
        align-items: center;
        gap: 8px;
        margin-bottom: 5px;
      }

      .library-head h2 { font-size: 0.88rem; }

      .keyboard-actions {
        flex-wrap: nowrap;
        gap: 4px;
      }

      .keyboard-actions button {
        padding: 5px 7px;
        border-radius: 7px;
        font-size: 0.7rem;
      }

      .library { gap: 3px; }

      .mini-card {
        padding: 2px 1px;
        border-radius: 6px;
        gap: 1px;
      }

      .mini-card .flag-stage {
        --graphic-w: 27px;
        --graphic-h: 18px;
      }

      .mini-card .flag-stage.pennant { --graphic-w: 34px; }
      .mini-card .flag-stage.substitute { --graphic-w: 27px; }
      .mini-card .flag-label { font-size: 0.55rem; line-height: 1; }

    }

    /* Use the same tight graphic bounds as the export, including after rotation. */
    .output .flag-stage > * {
      width: var(--graphic-w);
      height: var(--graphic-h);
    }

/* The insertion marker is an overlay and never affects flag spacing or export. */
.line { position: relative; }
.output .flag-block, .output .word-gap { cursor: text; }
.insertion-caret {
  position: absolute; top: 0; width: 3px; height: 100%;
  background: #9dbbff; box-shadow: 0 0 0 1px #10182a;
  pointer-events: none; z-index: 2; transform: translateX(-2px);
}
@media (max-width: 760px) {
  .controls { grid-template-columns: minmax(0, 1fr) auto; grid-template-rows: auto; gap: 8px; padding: 8px; }
  .controls-head { grid-column: 2; grid-row: 1; align-self: center; }
  .input-field { grid-column: 1; grid-row: 1; min-width: 0; }
  .input-field textarea { display: block; min-height: 52px; height: 52px; max-height: 100px; padding: 8px; font-size: 16px; }
  .controls-body { max-height: min(65dvh, calc(100dvh - 100px)); background: rgba(17, 23, 34, .78); }
  .controls-body select, .controls-body .dropzone { background: rgba(10, 16, 25, .65); }
}

.input-field { position: relative; }
.input-field textarea { padding-right: 48px; }
.clear-input {
  position: absolute; top: 4px; right: 4px;
  width: 44px; height: 44px; padding: 8px;
  display: grid; place-items: center; background: transparent;
  color: var(--muted); border-radius: 50%;
}
.clear-input span {
  display: grid; place-items: center; width: 26px; height: 26px;
  border: 1px solid currentColor; border-radius: 50%;
  font-size: 22px; font-weight: 400; line-height: 1;
}
.dropzone { font-weight: 400; }
@media (pointer: coarse) { .drop-hint { display: none; } }
