@layer reset, tokens, base, layout, components, motion, responsive, utility;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  body { font-family: var(--font-sans); line-height: 1.5; -webkit-font-smoothing: antialiased; }
  button { font: inherit; cursor: pointer; border: none; background: none; color: inherit; }
  input, textarea, select { font: inherit; color: inherit; }
  ul, ol { list-style: none; }
  a { color: inherit; text-decoration: none; }
  :focus { outline: none; }
  :focus-visible { outline: none; box-shadow: var(--focus-ring); border-radius: var(--radius-control); }
  ::selection { background: color-mix(in oklch, var(--color-brand) 35%, transparent); }
}

@layer tokens {
  :root {
    color-scheme: light dark;
    --color-bg:            light-dark(var(--gray-0), var(--gray-12));
    --color-surface:       light-dark(#ffffff, var(--gray-11));
    --color-surface-2:     light-dark(var(--gray-1), var(--gray-10));
    --color-surface-3:     light-dark(var(--gray-2), var(--gray-9));
    --color-overlay:       color-mix(in oklch, var(--gray-12) 60%, transparent);
    --color-border:        color-mix(in oklch, currentColor 12%, transparent);
    --color-border-strong: color-mix(in oklch, currentColor 22%, transparent);
    --color-text:          light-dark(var(--gray-12), var(--gray-1));
    --color-text-muted:    light-dark(var(--gray-7), var(--gray-5));
    --color-text-subtle:   light-dark(var(--gray-6), var(--gray-6));
    --color-brand:         light-dark(var(--indigo-7), var(--indigo-4));
    --color-brand-hover:   light-dark(var(--indigo-8), var(--indigo-3));
    --color-brand-fg:      white;
    --color-danger:        light-dark(var(--red-7), var(--red-4));
    --color-danger-fg:     white;
    --color-success:       light-dark(var(--green-7), var(--green-4));
    --color-success-fg:    white;
    --color-warning:       light-dark(var(--yellow-7), var(--yellow-4));
    --color-warning-fg:    var(--gray-12);
    --focus-ring-color:    var(--color-brand);
    --focus-ring:          0 0 0 2px var(--color-bg), 0 0 0 4px var(--focus-ring-color);
    --radius-control:      var(--radius-2);
    --radius-card:         var(--radius-3);
    --radius-pill:         var(--radius-round);
    --space-1: var(--size-1); --space-2: var(--size-2); --space-3: var(--size-3); --space-4: var(--size-4);
    --space-5: var(--size-5); --space-6: var(--size-6); --space-7: var(--size-7); --space-8: var(--size-8);
    --font-sans: system-ui, -apple-system, "Apple SD Gothic Neo", var(--font-system-ui);
    --font-mono: var(--font-monospace-code);
    --font-size-small: var(--font-size-0); --font-size-body: var(--font-size-1); --font-size-lead: var(--font-size-2);
    --font-size-h3: var(--font-size-3); --font-size-h2: var(--font-size-4); --font-size-h1: var(--font-size-5);
    --shadow-card: var(--shadow-1); --shadow-card-hover: var(--shadow-2); --shadow-overlay: var(--shadow-4);
    --ease-out: var(--ease-out-3); --ease-in-out: var(--ease-in-out-3);
    --duration-fast: 120ms; --duration-base: 200ms; --duration-slow: 320ms;
    --sidebar-width: 240px; --container-max: 1200px;
    --bg: var(--color-bg); --surface: var(--color-surface); --surface-2: var(--color-surface-2); --surface-3: var(--color-surface-3);
    --text: var(--color-text); --muted: var(--color-text-muted); --border: var(--color-border);
    --accent: var(--color-brand); --accent-fg: var(--color-brand-fg);
    --danger: var(--color-danger); --success: var(--color-success); --warning: var(--color-warning);
    --radius: var(--radius-card); --radius-sm: var(--radius-control);
  }
  @media (prefers-reduced-motion: reduce) {
    :root { --duration-fast: 0ms; --duration-base: 0ms; --duration-slow: 0ms; }
  }
  @media (prefers-contrast: more) {
    :root {
      --color-border:        color-mix(in oklch, currentColor 40%, transparent);
      --color-border-strong: color-mix(in oklch, currentColor 60%, transparent);
    }
  }
}

@layer base {
  body {
    background: var(--color-bg); color: var(--color-text);
    min-height: 100dvh;
    display: grid; grid-template-columns: var(--sidebar-width) 1fr;
    font-size: var(--font-size-body);
  }
  h1 { font-size: var(--font-size-h1); letter-spacing: -0.02em; }
  h2 { font-size: var(--font-size-h2); letter-spacing: -0.02em; }
  h3 { font-size: var(--font-size-h3); letter-spacing: -0.01em; }
  small { font-size: var(--font-size-small); color: var(--color-text-muted); }
  code, kbd, samp {
    font-family: var(--font-mono); font-size: 0.9em;
    background: var(--color-surface-2);
    padding: 0.05rem 0.35rem;
    border-radius: var(--radius-control);
  }
  hr { border: none; border-top: 1px solid var(--color-border); margin: var(--space-4) 0; }
}

@layer layout {
  .sidebar {
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    padding: 1.5rem 1rem;
    display: flex; flex-direction: column; gap: 1rem;
    position: sticky; top: 0; height: 100dvh;
  }
  .brand { display: flex; align-items: center; gap: 0.6rem; padding: 0.5rem 0.7rem; margin-bottom: 0.5rem; }
  .logo {
    width: 36px; height: 36px;
    border-radius: var(--radius-control);
    background: linear-gradient(135deg, var(--color-brand), var(--violet-6));
    display: grid; place-items: center; font-size: 1.2rem; color: white;
  }
  .brand strong { display: block; font-size: 0.95rem; }
  .brand small { color: var(--color-text-muted); font-size: 0.78rem; }
  .sidebar nav { display: flex; flex-direction: column; gap: 0.15rem; flex: 1; }
  .sidebar nav a {
    padding: 0.55rem 0.85rem;
    border-radius: var(--radius-control);
    font-size: 0.92rem;
    color: var(--color-text-muted);
    transition: background var(--duration-fast), color var(--duration-fast);
    display: flex; align-items: center; gap: 0.5rem;
  }
  .sidebar nav a:hover { color: var(--color-text); background: var(--color-surface-2); }
  .sidebar nav a.active {
    background: color-mix(in oklch, var(--color-brand) 16%, transparent);
    color: var(--color-brand); font-weight: 600;
  }
  .sidebar footer {
    color: var(--color-text-muted); font-size: 0.72rem;
    padding: 0.5rem 0.7rem;
    border-top: 1px solid var(--color-border);
  }
  .main {
    padding: 2rem 2.5rem 4rem;
    container-type: inline-size; container-name: main;
    overflow-x: hidden;
  }
  .page-header { margin-bottom: 1.5rem; }
  .page-header h2 { font-size: 1.6rem; display: inline-flex; align-items: center; gap: 0.5rem; }
  .page-header .subtitle { color: var(--color-text-muted); font-size: 0.9rem; margin-top: 0.2rem; }
}

@layer components {
  .card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
    padding: var(--space-4) var(--space-5);
    margin-bottom: var(--space-3);
    box-shadow: var(--shadow-card);
  }
  .card h3 {
    font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em;
    color: var(--color-text-muted); font-weight: 600; margin-bottom: 0.85rem;
  }
  .stats {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem; margin-bottom: 1.5rem;
  }
  .stat-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
    padding: 1.25rem 1.5rem;
  }
  .stat-card .stat-label {
    display: block; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em;
    color: var(--color-text-muted); margin-bottom: 0.4rem;
  }
  .stat-card strong {
    font-size: 1.8rem; font-weight: 700;
    font-variant-numeric: tabular-nums; letter-spacing: -0.02em;
  }
  .composer {
    display: grid; gap: 0.5rem; grid-template-columns: 1fr; margin-bottom: 1.25rem;
  }
  .composer.two-col { grid-template-columns: minmax(140px, 1fr) 2fr auto; }
  .composer.tag-form { grid-template-columns: 1fr 100px auto; align-items: center; }
  .composer input[type="text"], .composer input:not([type]) {
    padding: 0.6rem 0.85rem;
    border-radius: var(--radius-control);
    border: 1px solid var(--color-border);
    background: var(--color-surface-2);
    transition: border-color var(--duration-fast), box-shadow var(--duration-fast), background var(--duration-fast);
  }
  .composer input:focus-visible {
    outline: none; border-color: var(--color-brand);
    background: var(--color-surface);
    box-shadow: 0 0 0 3px color-mix(in oklch, var(--color-brand) 22%, transparent);
  }
  .composer input:user-invalid { border-color: var(--color-danger); }
  .composer input[type="color"] {
    width: 100%; height: 38px;
    border-radius: var(--radius-control);
    border: 1px solid var(--color-border);
    background: var(--color-surface-2); cursor: pointer;
  }
  .composer button {
    padding: 0.6rem 1.2rem;
    border-radius: var(--radius-control);
    background: var(--color-brand); color: var(--color-brand-fg);
    font-weight: 500;
    transition: background var(--duration-fast);
  }
  .composer button:hover { background: var(--color-brand-hover); }
  .list { display: flex; flex-direction: column; gap: 0.4rem; }
  .list .row {
    display: flex; gap: 1rem; align-items: center;
    padding: 0.85rem 1rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
    view-transition-name: var(--vt);
  }
  .row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.2rem; }
  .row-main .title { font-weight: 600; }
  .row-main .content { color: var(--color-text-muted); font-size: 0.88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .actions { display: flex; gap: 0.3rem; flex-shrink: 0; }
  .actions button {
    padding: 0.35rem 0.75rem;
    border-radius: var(--radius-control);
    border: 1px solid var(--color-border);
    color: var(--color-text-muted); font-size: 0.85rem;
    transition: color var(--duration-fast), border-color var(--duration-fast), background var(--duration-fast);
  }
  .actions button:hover { color: var(--color-brand); border-color: var(--color-brand); }
  .actions button.danger:hover { color: var(--color-danger); border-color: var(--color-danger); }
  form.edit {
    display: grid; grid-template-columns: minmax(120px, 1fr) 2fr auto auto;
    gap: 0.4rem; flex: 1; min-width: 0;
  }
  form.edit input {
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--color-brand);
    border-radius: var(--radius-control);
    background: transparent;
  }
  form.edit button {
    padding: 0.35rem 0.75rem;
    border-radius: var(--radius-control);
    border: 1px solid var(--color-brand);
    background: var(--color-brand); color: var(--color-brand-fg);
  }
  form.edit button[type="button"] {
    background: transparent; color: var(--color-text-muted);
    border-color: var(--color-border);
  }
  .activity-list { display: flex; flex-direction: column; gap: 0.3rem; }
  .activity-list li {
    display: grid; grid-template-columns: auto auto 1fr auto;
    gap: 0.7rem; align-items: center;
    padding: 0.6rem 0.85rem;
    background: var(--color-surface-2);
    border-radius: var(--radius-control); font-size: 0.88rem;
  }
  .activity-list.bigger li {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    padding: 0.85rem 1rem;
  }
  .badge {
    display: inline-flex; align-items: center; gap: var(--space-1);
    padding: 0.15rem 0.5rem;
    border-radius: var(--radius-pill);
    font-size: 0.72rem; font-weight: 600;
    text-transform: uppercase; letter-spacing: 0.04em;
  }
  .badge.create { background: color-mix(in oklch, var(--color-success) 18%, transparent); color: var(--color-success); }
  .badge.update { background: color-mix(in oklch, var(--color-warning) 22%, transparent); color: light-dark(var(--yellow-9), var(--yellow-3)); }
  .badge.delete { background: color-mix(in oklch, var(--color-danger) 18%, transparent); color: var(--color-danger); }
  .badge.seed   { background: var(--color-surface-3); color: var(--color-text-muted); }
  .activity-list .entity { color: var(--color-text-muted); font-size: 0.78rem; }
  .activity-list .summary { color: var(--color-text); }
  .activity-list .ts { color: var(--color-text-muted); font-size: 0.78rem; font-variant-numeric: tabular-nums; }
  .empty {
    text-align: center; color: var(--color-text-muted);
    padding: 3rem 1rem;
    border: 1px dashed var(--color-border);
    border-radius: var(--radius-card);
    margin-top: 1rem;
  }
  .info { display: grid; grid-template-columns: 130px 1fr; gap: 0.5rem 1rem; }
  .info dt {
    color: var(--color-text-muted); font-size: 0.85rem;
    padding: 0.5rem 0; border-bottom: 1px solid var(--color-border);
  }
  .info dd {
    padding: 0.5rem 0; border-bottom: 1px solid var(--color-border);
    font-size: 0.92rem;
  }
  .info code {
    font-family: var(--font-mono);
    background: var(--color-surface-2);
    padding: 0.05rem 0.35rem;
    border-radius: var(--radius-control); font-size: 0.85em;
  }
  .tag-grid { display: flex; gap: 0.5rem; flex-wrap: wrap; }
  .tag-chip {
    display: inline-flex; align-items: center; gap: 0.4rem;
    padding: 0.4rem 0.75rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-pill);
    font-size: 0.88rem;
    view-transition-name: var(--vt);
  }
  .tag-chip .swatch {
    width: 12px; height: 12px;
    border-radius: 50%;
    border: 1px solid color-mix(in oklch, currentColor 25%, transparent);
  }
  .tag-chip button {
    margin-left: 0.3rem; padding: 0;
    color: var(--color-text-muted); font-size: 1.1rem; line-height: 1;
  }
  .tag-chip button:hover { color: var(--color-danger); }
  output#note-count, output#tag-count {
    margin-left: 0.4rem; font-size: 0.85rem;
    color: var(--color-text-muted); font-variant-numeric: tabular-nums;
    padding: 0.15rem 0.55rem;
    background: var(--color-surface-2);
    border-radius: var(--radius-pill);
    border: 1px solid var(--color-border);
  }
  .toast {
    position: fixed; bottom: 1rem; right: 1rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card, 12px);
    padding: 0.7rem 1rem;
    box-shadow: var(--shadow-card);
    color: var(--color-text); font-size: 0.88rem;
    max-width: 360px; z-index: 9999;
    transform: translateX(120%);
    transition: transform 0.25s ease-out;
    pointer-events: auto;
  }
  .toast--show { transform: translateX(0); }
  .toast--error { border-left: 3px solid var(--color-danger); }
  .toast--info  { border-left: 3px solid var(--color-brand); }
  .toast--success { border-left: 3px solid var(--color-success); }
  .sentinel {
    list-style: none; text-align: center;
    padding: 0.85rem;
    color: var(--color-text-muted, #888); font-size: 0.85rem;
    border: 1px dashed var(--color-border, color-mix(in oklch, currentColor 12%, transparent));
    border-radius: var(--radius-card, 12px);
    margin: 0.4rem 0;
    display: flex; align-items: center; justify-content: center;
    gap: 0.5rem;
  }
  .sentinel-spinner {
    width: 12px; height: 12px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }
}

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

@layer responsive {
  @container main (max-width: 720px) {
    .composer.two-col { grid-template-columns: 1fr; }
    .row-main .content { white-space: normal; }
    .info { grid-template-columns: 1fr; gap: 0.2rem 1rem; }
    .info dt { padding-bottom: 0; border: none; }
    .info dd { padding-top: 0; }
  }
  @media (max-width: 600px) {
    body { grid-template-columns: 1fr; }
    .sidebar {
      position: static; height: auto;
      flex-direction: row; flex-wrap: wrap;
      padding: 1rem;
    }
    .sidebar footer { display: none; }
    .sidebar nav { flex-direction: row; flex-wrap: wrap; }
    .main { padding: 1.5rem 1rem; }
  }
}

@layer utility {
  .stack   { display: flex; flex-direction: column; gap: var(--space-3); }
  .cluster { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
  .grow    { flex: 1; }
  .visually-hidden {
    position: absolute !important;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden; clip-path: inset(50%);
    white-space: nowrap; border: 0;
  }
}
