@layer reset, tokens, base, components;

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

@layer tokens {
  :root {
    --bg:        hsl(214, 100%, 5%);
    --surface:   hsl(215, 11%, 3%);
    --surface2:  hsl(214, 4%, 5%);
    --border:    hsl(214, 35%, 22%);
    --border2:   hsl(214, 10%, 25%);
    --text:      hsl(215, 25%, 85%);
    --text2:     hsl(215, 25%, 85%);
    --text3:     hsl(216, 15%, 60%);
    --accent:    hsl(22, 100%, 62%);
    --accent2:   hsl(22, 100%, 78%);
    --danger:    hsl(0, 100%, 71%);
    --pass-a:    hsl(41, 100%, 65%);
    --pass-aa:   hsl(153, 100%, 55%);
    --pass-aaa:  hsl(201, 100%, 65%);
    --mono:      'Courier New', Courier, monospace;
    --sans:      system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    --radius:    0.625rem;
    --radius-sm: 0.375rem;
    --fs-xs:     0.9rem;
    --fs-sm:     1rem;
    --fs-base:   1rem;
    --fs-md:     1.125rem;
    --fs-xl:     3rem;
    --ls-tight:  0.02em;
    --ls-normal: 0.04em;
    --ls-wide:   0.06em;
    --canvas-bg: hsl(0, 0%, 100%);
  }
}

@layer base {
  body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    font-size: var(--fs-base);
    min-height: 100dvh;
    padding: 1rem;
    line-height: 1.6;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    user-select: none;
  }

  h1 {
    font-weight: 400;
    color: var(--accent);
  }

  .app {
    width: 100%;
    max-width: 65rem;
  }

  main {
    display: grid;
    gap: 1rem;
  }

  button {
    font-family: var(--sans);
  }
}

@layer components {
  .hero, .footer {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem 1.5rem;
  }

  .app--editor {
    flex: 1;
    min-height: 0;
    max-width: none;
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .hero {
    padding: 0.75rem 1.25rem;
  }

  .hero__eyebrow {
    font-size: var(--fs-sm);
    color: var(--text3);
  }

  .hero__title {
    font-size: var(--fs-xl);
    line-height: 1.2;
    letter-spacing: var(--ls-tight);
    margin: 0.15rem 0 0.35rem;
  }

  .hero__text {
    color: var(--text1);
    max-width: 70ch;
    font-size: var(--fs-sm);
  }

  

  .editor {
    flex: 1;
    min-height: 75vh;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    transition: border-color 0.2s;
  }

  .editor:hover {
    border-color: var(--accent);
  }

  .editor__workspace {
    flex: 1;
    min-height: 0;
    display: flex;
  }

  .info {
    display: grid;
    gap: 1.125rem;
    background-color: var(--surface);
    padding: 1.25rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }

  .info > p,
  .info > ul {
    width: min(32rem, 100%);
    margin-inline: auto;
  }

  .info > ul {
    padding-left: 1rem;
  }

  .info h2:first-of-type {
    width: min(65rem, 100%);
    margin-inline: auto;
  }

  .info h2 {
    width: min(35rem, 100%);
    margin-inline: auto;
    color: var(--accent);
    font-weight: 400;
  }

  .info__title {
    font-size: var(--fs-xl);
    font-weight: 400;
    color: var(--accent);
  }

  .tools-bar {
    width: 3.25rem;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.375rem;
    padding: 0.625rem 0;
    background: var(--surface2);
    border-right: 1px solid var(--border);
    overflow-y: auto;
  }

  .tool-btn {
    width: 2.5rem;
    height: 2.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text1);
    cursor: pointer;
    transition: color 0.15s, background 0.15s, border-color 0.15s;
  }

  .tool-btn svg {
    width: 1.25rem;
    height: 1.25rem;
  }

  .tool-btn:hover {
    background: var(--surface);
    color: var(--text);
  }

  .tool-btn.active {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--surface);
  }

  .tool-separator {
    width: 1.5rem;
    height: 1px;
    background: var(--border);
    margin: 0.25rem 0;
  }

  .canvas-area {
    flex: 1;
    min-width: 0;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background:
      linear-gradient(45deg, var(--surface2) 25%, transparent 25%),
      linear-gradient(-45deg, var(--surface2) 25%, transparent 25%),
      linear-gradient(45deg, transparent 75%, var(--surface2) 75%),
      linear-gradient(-45deg, transparent 75%, var(--surface2) 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0;
    background-color: var(--bg);
  }

  .canvas-area canvas {
    position: absolute;
    image-rendering: pixelated;
  }

  #pixelCanvas {
    z-index: 1;
    background: var(--canvas-bg);
  }

  #gridCanvas {
    z-index: 2;
    pointer-events: none;
  }

  .palette-panel {
    width: 13rem;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 0.875rem;
    background: var(--surface2);
    border-left: 1px solid var(--border);
    overflow-y: auto;
  }

  .palette-panel__label {
    font-size: var(--fs-xs);
    font-weight: 500;
    letter-spacing: var(--ls-normal);
    color: var(--accent);
    margin-bottom: 0.5rem;
  }

  .color-pick {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .color-value {
    font-family: var(--mono);
    font-size: var(--fs-xs);
    color: var(--text3);
    letter-spacing: var(--ls-normal);
  }

  .color-eye {
    position: relative;
    display: block;
    width: 2.25rem;
    height: 2.25rem;
    flex-shrink: 0;
    cursor: pointer;
  }

  .color-eye__fill {
    display: block;
    width: 100%;
    height: 100%;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    transition: transform 0.1s, border-color 0.15s;
  }

  .color-eye:hover .color-eye__fill,
  .color-eye:focus-within .color-eye__fill {
    transform: scale(1.1);
    border-color: var(--text);
  }

  .color-eye__input {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
    border: none;
    padding: 0;
  }

  .palette-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(1.125rem, 1fr));
    gap: 0.25rem;
  }

  #paletteSelect {
    width: 100%;
    margin-bottom: 0.5rem;
  }

  .palette-panel__actions {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
    margin-top: 0.5rem;
  }

  .palette-panel__actions[hidden] {
    display: none;
  }

  .btn--sm {
    padding: 0.25rem 0.625rem;
    font-size: var(--fs-xs);
  }

  .palette-color {
    position: relative;
    aspect-ratio: 1;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: transform 0.1s, border-color 0.15s;
  }

  .palette-color:hover {
    transform: scale(1.15);
  }

  .palette-color.selected {
    border-color: var(--text);
    transform: scale(1.1);
  }

  .swatch-remove {
    position: absolute;
    top: -0.3125rem;
    right: -0.3125rem;
    width: 1rem;
    height: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--bg);
    color: var(--text3);
    font-size: 0.7rem;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, border-color 0.15s;
  }

  .palette-color:hover .swatch-remove,
  .palette-color:focus-within .swatch-remove,
  .swatch-remove:focus-visible {
    opacity: 1;
  }

  .swatch-remove:hover {
    color: var(--accent);
    border-color: var(--accent);
  }

  .editor-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    background: var(--surface2);
    border-top: 1px solid var(--border);
  }

  .editor-bar__controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
  }

  .editor-bar__label {
    font-size: var(--fs-sm);
    color: var(--text3);
  }

  .editor-bar__spacer {
    flex: 1;
  }

  .editor-bar__hint {
    font-size: var(--fs-xs);
    color: var(--text3);
    opacity: 0.8;
  }

  select {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.375rem 0.5rem;
    font-family: var(--sans);
    font-size: var(--fs-sm);
    cursor: pointer;
  }

  .btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.5rem 1.125rem;
    font-family: var(--sans);
    font-size: var(--fs-base);
    letter-spacing: var(--ls-normal);
    cursor: pointer;
    transition: border-color 0.2s, color 0.2s, transform 0.1s;
  }

  .btn:hover {
    border-color: var(--accent);
    color: var(--accent2);
  }

  .btn:active {
    transform: translateY(1px);
  }

  .btn--icon {
    padding: 0.375rem 0.5rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .btn--icon svg {
    width: 1.125rem;
    height: 1.125rem;
  }

  .btn:focus-visible,
  .tool-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  .footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    width: 100%;
  }

  .footer__brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .footer__logo {
    width: 1.75rem;
    height: 1.75rem;
    border-radius: var(--radius-sm);
    object-fit: cover;
  }

  .footer__name {
    font-weight: 600;
    letter-spacing: var(--ls-wide);
  }

  .footer__link {
    color: var(--text3);
    text-decoration: none;
    font-size: var(--fs-sm);
  }

  .footer__link:hover {
    color: var(--accent);
  }

  

  @media (max-width: 55rem) {
    body {
      height: auto;
      overflow: auto;
    }

    .app--editor {
      flex: none;
    }

    .editor {
      height: auto;
    }

    .editor__workspace {
      flex-direction: column;
    }

    .tools-bar {
      width: 100%;
      flex-direction: row;
      justify-content: center;
      border-right: none;
      border-bottom: 1px solid var(--border);
      overflow-x: auto;
      overflow-y: hidden;
    }

    .tool-separator {
      width: 1px;
      height: 1.5rem;
    }

    .canvas-area {
      flex: 1;
      min-height: 55dvh;
    }

    .palette-panel {
      width: 100%;
      flex-direction: row;
      flex-wrap: wrap;
      border-left: none;
      border-top: 1px solid var(--border);
      min-height: 10rem;
    }

    .palette-panel__group {
      flex: 1;
      min-width: 9rem;
    }

    .editor-bar {
      justify-content: flex-start;
    }

    .editor-bar__hint {
      display: none;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .tool-btn,
    .palette-color,
    .color-eye__fill,
    .btn,
    .editor,
    .swatch-remove {
      transition: none;
    }
  }
}