@layer reset, base, components;

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

  html {
    color-scheme: light;
  }

  body,
  h1,
  h2,
  p,
  ul {
    margin: 0;
  }

  ul {
    padding: 0;
  }

  button,
  input {
    font: inherit;
  }

  button {
    border: 0;
  }

  svg {
    display: block;
  }

  [hidden] {
    display: none !important;
  }
}

@layer base {
  :root {
    --page: #f6f7fb;
    --page-accent: #eef3f8;
    --surface: #ffffff;
    --surface-muted: #f8fafc;
    --surface-strong: #eef2f7;
    --border: #d8dee8;
    --border-strong: #b7c2d0;
    --text: #18202d;
    --text-soft: #465365;
    --text-muted: #6f7d91;
    --accent: #1f6feb;
    --accent-soft: #e7f0ff;
    --accent-strong: #1456c8;
    --success: #147b4b;
    --danger: #b42318;
    --warning: #9a5b00;
    --shadow: 0 18px 44px rgba(21, 35, 58, 0.11);
    --shadow-soft: 0 8px 22px rgba(21, 35, 58, 0.08);
    --focus: 0 0 0 4px rgba(31, 111, 235, 0.18);
    --radius-lg: 18px;
    --radius-md: 12px;
    --radius-sm: 8px;
    --content-width: 1180px;
  }

  body {
    min-width: 320px;
    min-height: 100vh;
    background:
      linear-gradient(180deg, var(--page-accent), var(--page) 210px);
    color: var(--text);
    font-family:
      ui-sans-serif,
      system-ui,
      -apple-system,
      BlinkMacSystemFont,
      "Segoe UI",
      sans-serif;
    line-height: 1.45;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
  }

  button,
  input {
    color: inherit;
  }

  button {
    cursor: pointer;
  }

  button:disabled {
    cursor: not-allowed;
  }

  :focus-visible {
    outline: none;
    box-shadow: var(--focus);
  }

  ::selection {
    background: rgba(31, 111, 235, 0.18);
  }
}

@layer components {
  .skip-link {
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 100;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-weight: 760;
    text-decoration: none;
    transform: translateY(-160%);
    transition: transform 160ms ease;
  }

  .skip-link:focus {
    transform: translateY(0);
  }

  .app-shell {
    width: min(var(--content-width), calc(100% - 32px));
    margin-inline: auto;
  }

  .source-note,
  .code-pill,
  .size-pill {
    display: inline-flex;
    min-height: 30px;
    align-items: center;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.72);
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 680;
    white-space: nowrap;
  }

  .source-note,
  .code-pill,
  .size-pill {
    padding-inline: 11px;
  }

  .app-shell {
    padding-block: 22px 32px;
  }

  .intro {
    display: flex;
    gap: 24px;
    align-items: end;
    justify-content: space-between;
    margin-bottom: 18px;
  }

  .intro h1 {
    font-size: clamp(1.7rem, 3vw, 2.35rem);
    font-weight: 820;
    line-height: 1.08;
    letter-spacing: 0;
  }

  .intro p {
    max-width: 720px;
    margin-top: 8px;
    color: var(--text-soft);
    font-size: 0.98rem;
  }

  .workspace {
    display: grid;
    grid-template-columns: minmax(340px, 0.88fr) minmax(420px, 1.12fr);
    gap: 18px;
    align-items: stretch;
  }

  .controls-panel,
  .preview-panel {
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow-soft);
  }

  .controls-panel {
    position: relative;
    z-index: 10;
    display: flex;
    flex-direction: column;
    gap: 18px;
    padding: 22px;
  }

  .field-group label {
    display: block;
    margin-bottom: 8px;
    color: var(--text);
    font-size: 0.9rem;
    font-weight: 760;
  }

  .combobox {
    position: relative;
  }

  .search-icon {
    position: absolute;
    top: 50%;
    left: 14px;
    z-index: 1;
    pointer-events: none;
    transform: translateY(-50%);
  }

  .search-icon svg {
    width: 19px;
    fill: var(--text-muted);
  }

  #country-search {
    width: 100%;
    min-height: 48px;
    padding: 0 48px 0 44px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    outline: none;
    background: var(--surface);
    color: var(--text);
    font-size: 0.98rem;
    transition:
      border-color 150ms ease,
      background-color 150ms ease,
      box-shadow 150ms ease;
  }

  #country-search::placeholder {
    color: #8894a5;
  }

  #country-search:hover:not(:disabled) {
    border-color: #8998ad;
  }

  #country-search:focus {
    border-color: var(--accent);
    box-shadow: var(--focus);
  }

  #country-search:disabled {
    background: var(--surface-muted);
    color: var(--text-muted);
  }

  #country-search[aria-invalid="true"] {
    border-color: var(--danger);
  }

  .clear-button {
    position: absolute;
    top: 50%;
    right: 7px;
    z-index: 2;
    display: grid;
    width: 36px;
    height: 36px;
    place-items: center;
    border-radius: var(--radius-sm);
    background: transparent;
    transform: translateY(-50%);
  }

  .clear-button:hover {
    background: var(--surface-strong);
  }

  .clear-button svg {
    width: 18px;
    fill: var(--text-soft);
  }

  .country-options {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    left: 0;
    z-index: 80;
    max-height: min(330px, calc(100vh - 180px));
    overflow-y: auto;
    padding: 6px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    background: var(--surface);
    box-shadow: var(--shadow);
    list-style: none;
    overscroll-behavior: contain;
  }

  .country-option,
  .country-no-results {
    display: grid;
    min-height: 48px;
    align-items: center;
    border-radius: var(--radius-sm);
  }

  .country-option {
    grid-template-columns: 32px 1fr auto;
    gap: 10px;
    padding: 7px 9px;
    cursor: pointer;
  }

  .country-option:hover,
  .country-option[aria-selected="true"] {
    background: var(--accent-soft);
  }

  .country-option-flag {
    font-size: 1.32rem;
    line-height: 1;
  }

  .country-option-name {
    overflow: hidden;
    color: var(--text);
    font-weight: 680;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .country-option-code {
    color: var(--text-muted);
    font-family:
      ui-monospace,
      SFMono-Regular,
      Menlo,
      Monaco,
      Consolas,
      monospace;
    font-size: 0.75rem;
    font-weight: 760;
  }

  .country-no-results {
    padding: 10px 12px;
    color: var(--text-muted);
    font-size: 0.88rem;
  }

  .field-help,
  .field-status,
  .status-message,
  .section-heading p,
  .preview-heading p {
    color: var(--text-muted);
    font-size: 0.82rem;
  }

  .field-help {
    margin-top: 8px;
  }

  .field-status {
    min-height: 20px;
    margin-top: 4px;
  }

  .field-status-row {
    display: flex;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
  }

  .retry-button {
    flex: 0 0 auto;
    margin-top: 4px;
  }

  .field-status[data-state="error"],
  .status-message[data-state="error"] {
    color: var(--danger);
  }

  .field-status[data-state="success"],
  .status-message[data-state="success"] {
    color: var(--success);
  }

  .field-status[data-state="warning"],
  .status-message[data-state="warning"] {
    color: var(--warning);
  }

  .palette-section,
  .selection-details,
  .format-section {
    border-top: 1px solid var(--border);
    padding-top: 18px;
  }

  .section-heading,
  .preview-heading {
    display: flex;
    gap: 16px;
    align-items: start;
    justify-content: space-between;
  }

  .section-heading h2,
  .preview-heading h2 {
    font-size: 1.05rem;
    line-height: 1.2;
    letter-spacing: 0;
  }

  .section-heading p,
  .preview-heading p {
    margin-top: 3px;
  }

  .palette-options {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    margin-top: 14px;
  }

  .empty-state {
    grid-column: 1 / -1;
    min-height: 96px;
    display: grid;
    place-items: center;
    padding: 18px;
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-md);
    color: var(--text-muted);
    font-size: 0.86rem;
    text-align: center;
  }

  .palette-option {
    min-width: 0;
    min-height: 44px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    color: var(--text);
    text-align: left;
    transition:
      border-color 150ms ease,
      background-color 150ms ease,
      transform 150ms ease,
      box-shadow 150ms ease;
  }

  .palette-option:hover {
    border-color: #8fb5ec;
    background: #fbfdff;
    transform: translateY(-1px);
  }

  .palette-option[aria-pressed="true"] {
    border-color: var(--accent);
    background: var(--accent-soft);
    box-shadow: 0 0 0 3px rgba(31, 111, 235, 0.12);
  }

  .palette-thumbnail {
    display: block;
    overflow: hidden;
    width: 100%;
    aspect-ratio: 1;
    border: 1px solid rgba(24, 32, 45, 0.08);
    border-radius: var(--radius-sm);
    background: var(--surface-muted);
  }

  .palette-thumbnail svg {
    width: 100%;
    height: 100%;
  }

  .palette-meta {
    display: grid;
    gap: 2px;
    margin-top: 8px;
  }

  .palette-label {
    overflow: hidden;
    font-size: 0.75rem;
    font-weight: 760;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .palette-hex {
    color: var(--text-muted);
    font-family:
      ui-monospace,
      SFMono-Regular,
      Menlo,
      Monaco,
      Consolas,
      monospace;
    font-size: 0.7rem;
  }

  .selection-details {
    display: grid;
    gap: 12px;
  }

  .format-section {
    display: grid;
  }

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

  .format-option {
    position: relative;
    display: grid;
    min-height: 44px;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    color: var(--text-soft);
    cursor: pointer;
    font-size: 0.86rem;
    font-weight: 780;
    transition:
      background-color 150ms ease,
      border-color 150ms ease,
      color 150ms ease,
      box-shadow 150ms ease;
  }

  .format-option:hover {
    border-color: #8fb5ec;
    background: #fbfdff;
  }

  .format-option[data-selected="true"] {
    border-color: var(--accent);
    background: var(--accent-soft);
    color: var(--accent-strong);
    box-shadow: 0 0 0 3px rgba(31, 111, 235, 0.1);
  }

  .format-option input {
    position: absolute;
    inset: 0;
    margin: 0;
    opacity: 0;
    cursor: pointer;
  }

  .format-option:focus-within {
    box-shadow: var(--focus);
  }

  .detail-row {
    display: flex;
    gap: 18px;
    min-height: 24px;
    align-items: center;
    justify-content: space-between;
  }

  .detail-row > span:first-child {
    color: var(--text-muted);
    font-size: 0.84rem;
  }

  .detail-row strong {
    overflow: hidden;
    max-width: 68%;
    font-size: 0.86rem;
    text-align: right;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .selected-color {
    display: inline-flex;
    gap: 8px;
    align-items: center;
    min-width: 0;
  }

  .selected-color strong {
    max-width: none;
    font-family:
      ui-monospace,
      SFMono-Regular,
      Menlo,
      Monaco,
      Consolas,
      monospace;
  }

  .selected-swatch {
    width: 18px;
    height: 18px;
    flex: 0 0 auto;
    border: 1px solid rgba(24, 32, 45, 0.25);
    border-radius: 5px;
    background: transparent;
  }

  .actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .button {
    display: inline-flex;
    min-height: 46px;
    gap: 8px;
    align-items: center;
    justify-content: center;
    padding: 0 14px;
    border-radius: var(--radius-md);
    font-size: 0.88rem;
    font-weight: 760;
    transition:
      background-color 150ms ease,
      border-color 150ms ease,
      transform 150ms ease,
      opacity 150ms ease;
  }

  .button:not(:disabled):active {
    transform: translateY(1px);
  }

  .button svg {
    width: 18px;
    flex: 0 0 auto;
    fill: currentColor;
  }

  .button-primary {
    background: var(--accent);
    color: #ffffff;
  }

  .button-primary:not(:disabled):hover {
    background: var(--accent-strong);
  }

  .button-secondary {
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
  }

  .button-secondary:not(:disabled):hover {
    background: var(--surface-strong);
  }

  .button:disabled {
    opacity: 0.48;
  }

  .status-message {
    min-height: 20px;
  }

  .preview-panel {
    display: flex;
    flex-direction: column;
    padding: 22px;
  }

  .preview-heading {
    margin-bottom: 16px;
  }

  .preview-canvas {
    position: relative;
    display: grid;
    width: min(100%, 610px);
    aspect-ratio: 1;
    margin: auto;
    overflow: hidden;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background:
      linear-gradient(45deg, rgba(111, 125, 145, 0.09) 25%, transparent 25%),
      linear-gradient(-45deg, rgba(111, 125, 145, 0.09) 25%, transparent 25%),
      linear-gradient(45deg, transparent 75%, rgba(111, 125, 145, 0.09) 75%),
      linear-gradient(-45deg, transparent 75%, rgba(111, 125, 145, 0.09) 75%),
      #f4f7fb;
    background-position:
      0 0,
      0 13px,
      13px -13px,
      -13px 0;
    background-size: 26px 26px;
  }

  .preview-canvas > svg {
    width: 100%;
    height: 100%;
  }

  .preview-empty,
  .loading-state {
    display: grid;
    gap: 8px;
    max-width: 280px;
    justify-items: center;
    padding: 24px;
    color: var(--text-muted);
    text-align: center;
  }

  .preview-empty strong {
    color: var(--text-soft);
  }

  .preview-empty > span:last-child,
  .loading-state {
    font-size: 0.86rem;
  }

  .preview-empty-icon {
    display: grid;
    width: 54px;
    height: 54px;
    margin-bottom: 4px;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
  }

  .preview-empty-icon svg {
    width: 28px;
    fill: var(--text-muted);
  }

  .loading-state {
    position: absolute;
    inset: 0;
    z-index: 5;
    max-width: none;
    place-content: center;
    background: rgba(248, 250, 252, 0.82);
    backdrop-filter: blur(8px);
  }

  .spinner {
    width: 30px;
    height: 30px;
    border: 3px solid rgba(111, 125, 145, 0.2);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 720ms linear infinite;
  }

  @keyframes spin {
    to {
      transform: rotate(360deg);
    }
  }

  @media (min-width: 941px) and (max-height: 900px) {
    .app-shell {
      padding-block: 14px 18px;
    }

    .intro {
      margin-bottom: 12px;
    }

    .controls-panel,
    .preview-panel {
      padding: 16px;
    }

    .controls-panel {
      gap: 11px;
    }

    .palette-section,
    .selection-details,
    .format-section {
      padding-top: 11px;
    }

    .selection-details {
      gap: 9px;
    }

    .palette-options {
      margin-top: 10px;
    }

    .palette-thumbnail {
      aspect-ratio: 1.22;
    }

    .preview-heading {
      margin-bottom: 12px;
    }

    .preview-canvas {
      width: min(100%, 540px);
    }
  }

  @media (max-width: 940px) {
    .workspace {
      grid-template-columns: 1fr;
    }

    .preview-canvas {
      width: min(100%, 560px);
    }
  }

  @media (max-width: 640px) {
    .app-shell {
      width: min(100% - 24px, var(--content-width));
    }

    .source-note {
      display: none;
    }

    .app-shell {
      padding-block: 18px 28px;
    }

    .intro {
      display: block;
      margin-bottom: 16px;
    }

    .intro h1 {
      font-size: 1.72rem;
    }

    .intro p {
      font-size: 0.93rem;
    }

    .controls-panel,
    .preview-panel {
      padding: 16px;
      border-radius: 15px;
    }

    .palette-options {
      grid-template-columns: 1fr;
    }

    .palette-option {
      display: grid;
      grid-template-columns: 78px 1fr;
      gap: 12px;
      align-items: center;
    }

    .palette-meta {
      margin-top: 0;
    }

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

    .button,
    #country-search {
      min-height: 48px;
    }

    .preview-panel {
      min-height: auto;
    }

    .preview-canvas {
      width: 100%;
      min-height: 0;
      border-radius: 15px;
    }

    .country-options {
      max-height: min(318px, calc(100vh - 150px));
    }
  }

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