@layer tokens, base, layout, components, utilities;

@layer tokens {
  :root {
    color-scheme: light;

    --ui-bg: #f7f8f8;
    --ui-surface: #ffffff;
    --ui-surface-muted: #f0f3f4;
    --ui-border: #dde2e5;
    --ui-text: #24272b;
    --ui-muted: #68717b;
    --ui-accent: #2f6f8f;
    --ui-accent-strong: #1f5874;
    --ui-accent-soft: #e8f2f7;
    --ui-success: #2f7d4f;
    --ui-success-soft: #e8f5ed;
    --ui-warning: #9a6500;
    --ui-warning-soft: #fff4dd;
    --ui-danger: #b44235;
    --ui-danger-soft: #fcecea;

    --ui-font-sans: system-ui, 'Hiragino Sans', 'Noto Sans JP', 'Segoe UI', Meiryo, sans-serif;
    --ui-font-mono:
      ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', monospace;
    --ui-font: var(--ui-font-sans);
    --ui-leading: 1.68;
    --ui-radius-sm: 7px;
    --ui-radius-md: 10px;
    --ui-radius-lg: 14px;
    --ui-radius: var(--ui-radius-md);
    --ui-shadow: 0 16px 34px rgb(36 39 43 / 0.08), 0 2px 8px rgb(36 39 43 / 0.04);
    --ui-shadow-sm: 0 7px 18px rgb(36 39 43 / 0.055), 0 1px 3px rgb(36 39 43 / 0.035);

    --ui-text-sm: 0.86rem;
    --ui-text-md: 0.95rem;
    --ui-text-lg: 1.15rem;

    --ui-space-sm: 0.7rem;
    --ui-space-md: 1.1rem;
    --ui-space-lg: 1.75rem;

    --ui-control-sm: 2rem;
    --ui-control-md: 2.75rem;
    --ui-control-lg: 3.25rem;

    --ui-container: 1080px;
  }
}

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

  html {
    background: var(--ui-bg);
    color: var(--ui-text);
    font-family: var(--ui-font);
    font-size: 16px;
    line-height: var(--ui-leading);
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
  }

  body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--ui-bg);
  }

  :where(h1, h2, h3, p, ul, ol, dl) {
    margin-block: 0;
  }

  :where(h1, h2, h3) {
    line-height: 1.18;
    letter-spacing: 0;
  }

  h1 {
    font-size: clamp(2rem, 5vw, 3.9rem);
    font-weight: 700;
  }

  h2 {
    font-size: clamp(1.35rem, 2.4vw, 2rem);
    font-weight: 700;
  }

  h3 {
    font-size: var(--ui-text-lg);
    font-weight: 700;
  }

  a {
    color: var(--ui-accent-strong);
    text-underline-offset: 0.18em;
  }

  :where(ul, ol) {
    padding-inline-start: 1.25rem;
  }

  :where(li + li) {
    margin-block-start: 0.35rem;
  }

  :where(button, input, select, textarea) {
    font: inherit;
  }

  :where(button, [type='button'], [type='submit'], [type='reset']) {
    -webkit-tap-highlight-color: transparent;
  }

  p {
    max-width: 68ch;
  }

  :focus-visible {
    outline: 3px solid color-mix(in srgb, var(--ui-accent) 32%, transparent);
    outline-offset: 3px;
  }
}

@layer layout {
  .container {
    width: min(100% - clamp(1.25rem, 5vw, 3rem), var(--ui-container));
    margin-inline: auto;
  }

  .stack {
    display: flex;
    flex-direction: column;
    gap: var(--stack-gap, var(--ui-space-lg));
  }

  .cluster {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--cluster-gap, var(--ui-space-sm));
  }

  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
    gap: var(--grid-gap, var(--ui-space-lg));
  }
}

@layer components {
  .card {
    padding: var(--card-padding, clamp(1.25rem, 3vw, 1.85rem));
    background: var(--ui-surface);
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius);
    box-shadow: var(--ui-shadow-sm);
  }

  .panel {
    padding: var(--panel-padding, var(--ui-space-lg));
    background: rgb(255 255 255 / 0.72);
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius-sm);
  }

  .hero {
    padding-block: clamp(3.25rem, 8vw, 6.5rem) clamp(2.5rem, 6vw, 5rem);
  }

  .hero > * {
    max-width: 760px;
  }

  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--ui-control-md);
    padding: var(--ui-space-sm) var(--ui-space-md);
    color: #fffdfa;
    font-size: var(--ui-text-md);
    font-weight: 700;
    line-height: 1;
    text-decoration: none;
    background: var(--ui-accent);
    border: 1px solid var(--ui-accent);
    border-radius: var(--ui-radius-sm);
    box-shadow: 0 8px 16px color-mix(in srgb, var(--ui-accent) 14%, transparent);
    cursor: pointer;
    transition:
      background-color 160ms ease,
      border-color 160ms ease,
      box-shadow 160ms ease,
      transform 160ms ease;
  }

  .btn:hover {
    background: var(--ui-accent-strong);
    border-color: var(--ui-accent-strong);
    box-shadow: 0 10px 18px rgb(31 88 116 / 0.18);
    transform: translateY(-1px);
  }

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

  .btn-sm {
    min-height: var(--ui-control-sm);
    padding: 0.45rem var(--ui-space-sm);
    font-size: var(--ui-text-sm);
  }

  .btn-lg {
    min-height: var(--ui-control-lg);
    padding: var(--ui-space-md) var(--ui-space-lg);
    font-size: var(--ui-text-lg);
  }

  .btn-secondary {
    color: var(--ui-text);
    background: var(--ui-surface);
    border-color: var(--ui-border);
    box-shadow: none;
  }

  .btn-secondary:hover {
    color: var(--ui-accent-strong);
    background: var(--ui-accent-soft);
    border-color: color-mix(in srgb, var(--ui-accent) 28%, transparent);
    box-shadow: none;
  }

  .btn:disabled,
  .btn[aria-disabled='true'] {
    color: var(--ui-muted);
    background: var(--ui-surface-muted);
    border-color: var(--ui-border);
    box-shadow: none;
    cursor: not-allowed;
    transform: none;
  }

  .btn-ghost {
    color: var(--ui-accent-strong);
    background: transparent;
    border-color: transparent;
    box-shadow: none;
  }

  .btn-ghost:hover {
    background: var(--ui-accent-soft);
    border-color: transparent;
    box-shadow: none;
  }

  .btn-danger {
    color: #fffdfa;
    background: var(--ui-danger);
    border-color: var(--ui-danger);
  }

  .btn-danger:hover {
    background: color-mix(in srgb, var(--ui-danger) 82%, black);
    border-color: color-mix(in srgb, var(--ui-danger) 82%, black);
  }

  .btn-secondary.btn-danger,
  .btn-ghost.btn-danger {
    color: var(--ui-danger);
    background: transparent;
    border-color: var(--ui-border);
  }

  .btn-secondary.btn-danger:hover,
  .btn-ghost.btn-danger:hover {
    color: var(--ui-danger);
    background: var(--ui-danger-soft);
    border-color: color-mix(in srgb, var(--ui-danger) 28%, transparent);
    box-shadow: none;
  }

  .icon-btn {
    display: inline-grid;
    place-items: center;
    width: var(--ui-control-sm);
    height: var(--ui-control-sm);
    padding: 0;
    color: var(--ui-text);
    background: var(--ui-surface);
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius-sm);
    cursor: pointer;
    transition:
      background-color 160ms ease,
      border-color 160ms ease,
      color 160ms ease;
  }

  .icon-btn:hover {
    color: var(--ui-accent-strong);
    background: var(--ui-accent-soft);
    border-color: color-mix(in srgb, var(--ui-accent) 28%, transparent);
  }

  .drag-handle {
    display: grid;
    place-items: center;
    width: var(--ui-control-sm);
    height: var(--ui-control-sm);
    color: var(--ui-muted);
    border-radius: var(--ui-radius-sm);
    cursor: grab;
    user-select: none;
  }

  .drag-handle:hover {
    color: var(--ui-accent-strong);
    background: var(--ui-accent-soft);
  }

  .drag-handle:active {
    cursor: grabbing;
  }

  .drag-item.is-dragging {
    opacity: 0.48;
  }

  .drag-item.is-drag-over {
    border-color: var(--ui-accent);
    box-shadow: 0 0 0 3px var(--ui-accent-soft);
  }

  .badge {
    display: inline-flex;
    align-items: center;
    min-height: var(--ui-control-sm);
    padding: 0.22rem 0.62rem;
    color: var(--ui-accent-strong);
    font-size: var(--ui-text-sm);
    font-weight: 700;
    line-height: 1;
    background: var(--ui-accent-soft);
    border: 1px solid color-mix(in srgb, var(--ui-accent) 12%, transparent);
    border-radius: var(--ui-radius-sm);
  }

  .field {
    display: grid;
    gap: var(--ui-space-sm);
    color: var(--ui-muted);
    font-size: var(--ui-text-md);
    font-weight: 700;
  }

  .input {
    width: 100%;
    min-height: var(--ui-control-lg);
    padding: var(--ui-space-sm) var(--ui-space-md);
    color: var(--ui-text);
    background: var(--ui-surface);
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius-sm);
    box-shadow: inset 0 1px 2px rgb(47 48 51 / 0.04);
    transition:
      background-color 160ms ease,
      border-color 160ms ease,
      box-shadow 160ms ease;
  }

  textarea.input {
    resize: vertical;
  }

  .input::placeholder {
    color: rgb(104 113 123 / 0.72);
  }

  .input:focus {
    background: var(--ui-surface);
    border-color: color-mix(in srgb, var(--ui-accent) 58%, transparent);
    box-shadow:
      0 0 0 4px color-mix(in srgb, var(--ui-accent) 12%, transparent),
      inset 0 1px 2px rgb(47 48 51 / 0.03);
    outline: 0;
  }

  .input:disabled {
    color: var(--ui-muted);
    background: var(--ui-surface-muted);
    cursor: not-allowed;
  }

  .input[aria-invalid='true'],
  .input.is-invalid {
    border-color: color-mix(in srgb, var(--ui-danger) 58%, transparent);
    box-shadow: 0 0 0 4px color-mix(in srgb, var(--ui-danger) 10%, transparent);
  }

  .field-hint {
    color: var(--ui-muted);
    font-size: var(--ui-text-sm);
    font-weight: 500;
  }

  .alert {
    padding: var(--ui-space-md) var(--ui-space-lg);
    color: var(--ui-text);
    background: var(--ui-warning-soft);
    border: 1px solid color-mix(in srgb, var(--ui-warning) 45%, transparent);
    border-radius: var(--ui-radius-sm);
  }

  .alert-success {
    background: var(--ui-success-soft);
    border-color: color-mix(in srgb, var(--ui-success) 28%, transparent);
  }

  .alert-danger {
    background: var(--ui-danger-soft);
    border-color: color-mix(in srgb, var(--ui-danger) 28%, transparent);
  }

  .empty {
    padding: clamp(2rem, 6vw, calc(var(--ui-space-lg) * 1.45));
    color: var(--ui-muted);
    text-align: center;
    background: var(--ui-surface-muted);
    border: 1px dashed var(--ui-border);
    border-radius: var(--ui-radius);
  }

  .divider {
    border: 0;
    border-block-start: 1px solid var(--ui-border);
    margin: 0;
  }

  .section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--ui-space-sm);
  }

  .section-head h2 {
    font-size: var(--ui-text-lg);
  }

  dialog.dialog {
    width: min(100% - 2rem, var(--dialog-width, 30rem));
    max-width: none;
    padding: 0;
    color: var(--ui-text);
    background: var(--ui-surface);
    border: 0;
    border-radius: var(--ui-radius);
    box-shadow: var(--ui-shadow);
    margin: auto;
  }

  dialog.dialog::backdrop {
    background: rgb(36 39 43 / 0.34);
    backdrop-filter: blur(4px);
  }

  .dialog-body {
    display: grid;
    gap: var(--ui-space-md);
    padding: var(--ui-space-lg);
  }

  .dialog-body h2,
  .dialog-body h3 {
    font-size: var(--ui-text-lg);
  }

  .dialog-body p {
    color: var(--ui-muted);
    font-size: var(--ui-text-md);
  }

  .dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--ui-space-sm);
  }

  .button-group {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.2rem;
    background: color-mix(in srgb, var(--ui-surface-muted) 76%, white);
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius);
  }

  .button-group .btn {
    min-height: calc(var(--ui-control-sm) + 0.3rem);
    padding: 0.45rem 0.72rem;
    font-size: var(--ui-text-sm);
    line-height: 1;
    border-color: transparent;
    box-shadow: none;
    transform: none;
  }

  .button-group .btn-secondary {
    color: var(--ui-muted);
    background: transparent;
  }

  .button-group .btn-secondary:hover {
    color: var(--ui-accent-strong);
    background: var(--ui-surface);
    border-color: var(--ui-border);
  }

  .button-group .btn-primary {
    color: #fff;
    background: var(--ui-accent);
    border-color: var(--ui-accent);
  }

  .button-group .btn-primary:hover {
    background: var(--ui-accent-strong);
    border-color: var(--ui-accent-strong);
  }

  .check-item {
    display: flex;
    align-items: center;
    gap: var(--ui-space-sm);
    cursor: pointer;
  }

  .check-box {
    position: relative;
    display: inline-flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    width: var(--check-box-size, 1.3rem);
    height: var(--check-box-size, 1.3rem);
    border: 1.5px solid var(--ui-border);
    border-radius: var(--ui-radius-sm);
    transition:
      background-color 160ms ease,
      border-color 160ms ease;
  }

  .check-box::after {
    content: '';
    width: 0.5rem;
    height: 0.32rem;
    border: solid #fff;
    border-width: 0 0 2px 2px;
    opacity: 0;
    transform: translateY(-1px) rotate(-45deg) scale(0.8);
    transition:
      opacity 160ms ease,
      transform 160ms ease;
  }

  .check-item.checked .check-box,
  .check-item[aria-checked='true'] .check-box {
    background: var(--ui-accent);
    border-color: var(--ui-accent);
  }

  .check-item.checked .check-box::after,
  .check-item[aria-checked='true'] .check-box::after {
    opacity: 1;
    transform: translateY(-1px) rotate(-45deg) scale(1);
  }

  .check-label {
    flex: 1;
    min-width: 0;
    transition: color 160ms ease;
  }

  .check-item.checked .check-label,
  .check-item[aria-checked='true'] .check-label {
    color: var(--ui-muted);
  }

  .toast {
    position: fixed;
    left: 50%;
    bottom: var(--ui-space-lg);
    z-index: 20;
    padding: 0.62rem 1rem;
    color: #fff;
    background: var(--ui-text);
    border-radius: 999px;
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 0.6rem);
    transition:
      opacity 200ms ease,
      transform 200ms ease;
  }

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

  .nav {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--ui-space-md);
    padding-block: clamp(1rem, 3vw, var(--ui-space-lg));
  }

  .nav a {
    color: var(--ui-muted);
    font-weight: 700;
    text-decoration: none;
  }

  .nav a:hover {
    color: var(--ui-accent-strong);
  }

  .bottom-tabs {
    display: flex;
    gap: 0.25rem;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .bottom-tabs::-webkit-scrollbar {
    display: none;
  }

  .bottom-tab {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    min-width: 4.25rem;
    padding: 0.35rem 0.5rem;
    color: var(--ui-muted);
    font-weight: 700;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--ui-radius-sm);
    cursor: pointer;
    transition:
      background-color 160ms ease,
      border-color 160ms ease,
      color 160ms ease;
  }

  .bottom-tab:hover {
    color: var(--ui-accent-strong);
    background: var(--ui-accent-soft);
  }

  .bottom-tab.active,
  .bottom-tab[aria-current='page'] {
    color: var(--ui-accent-strong);
    background: var(--ui-accent-soft);
    border-color: color-mix(in srgb, var(--ui-accent) 18%, transparent);
  }

  .list {
    display: grid;
    gap: var(--ui-space-sm);
    padding: 0;
    list-style: none;
  }

  .list-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--ui-space-md);
    padding: var(--ui-space-md);
    background: var(--ui-surface);
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius-sm);
  }

  .stat {
    display: grid;
    gap: 0.2rem;
  }

  .stat-value {
    font-size: clamp(1.65rem, 4vw, 2.7rem);
    font-weight: 800;
    line-height: 1;
  }

  .stat-label {
    color: var(--ui-muted);
    font-size: var(--ui-text-sm);
    font-weight: 700;
  }

  .table-wrap {
    overflow-x: auto;
    background: var(--ui-surface);
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius);
  }

  .table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--ui-text-md);
  }

  .table th,
  .table td {
    padding: 0.85rem 1rem;
    text-align: left;
    border-block-end: 1px solid var(--ui-border);
  }

  .table th {
    color: var(--ui-muted);
    font-size: var(--ui-text-sm);
    font-weight: 700;
  }

  .table tbody tr:hover {
    background: color-mix(in srgb, var(--ui-accent) 3.5%, transparent);
  }

  .table tr:last-child > * {
    border-block-end: 0;
  }
}

@layer utilities {
  .text-muted {
    color: var(--ui-muted);
  }

  .text-center {
    text-align: center;
  }

  .text-success {
    color: var(--ui-success);
  }

  .text-danger {
    color: var(--ui-danger);
  }

  .full-width {
    width: 100%;
  }

  .shadow-none {
    box-shadow: none;
  }

  .surface-muted {
    background: var(--ui-surface-muted);
  }

  .max-readable {
    max-width: 68ch;
  }

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

@media (max-width: 640px) {
  @layer tokens {
    :root {
      --ui-space-lg: 1.35rem;
    }
  }

  @layer base {
    html {
      font-size: 15.5px;
    }
  }

  @layer layout {
    .grid {
      gap: var(--ui-space-md);
    }
  }

  @layer components {
    .card {
      padding: var(--card-padding, 1.2rem);
      box-shadow: 0 6px 16px rgb(36 39 43 / 0.05);
    }

    .hero {
      padding-block: 2.4rem 2rem;
    }

    .hero .cluster {
      align-items: stretch;
    }

    .btn {
      min-height: var(--ui-control-lg);
      padding-inline: 1rem;
    }

    .list-item {
      align-items: flex-start;
      flex-direction: column;
    }

    .section-head {
      align-items: flex-start;
    }

    .hero .btn {
      width: 100%;
    }

    .nav {
      align-items: flex-start;
      justify-content: flex-start;
    }
  }
}
