@layer reset, base, theme, components, utilities;

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

  html,
  body {
    margin: 0;
    min-height: 100%;
  }

  button,
  input,
  textarea {
    font: inherit;
    color: inherit;
  }

  button {
    cursor: pointer;
  }

  svg {
    display: block;
  }

  dialog {
    border: 0;
    padding: 0;
  }
}

@layer base {
  :root {
    color-scheme: light;
    --bg: #f3f5f8;
    --surface: #ffffff;
    --ink: #111827;
    --muted: #6b7280;
    --line: #e5e7eb;
    --accent: #0f766e;
    --accent-soft: #ccfbf1;
    --danger: #dc2626;
    --shadow: 0 10px 30px rgb(17 24 39 / 8%);
    --radius: 16px;
    --font: "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    accent-color: var(--accent);
  }

  html {
    background: var(--bg);
  }

  body {
    min-height: 100dvh;
    color: var(--ink);
    font-family: var(--font);
    font-size: 1rem;
    line-height: 1.55;
    background: var(--bg);
  }

  h1,
  h2 {
    margin: 0;
    font-weight: 700;
    letter-spacing: -0.03em;
    text-wrap: balance;
  }

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

@layer components {
  .gate {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.25rem;
    padding-top: max(1.25rem, env(safe-area-inset-top));
    padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
  }

  .gate-card {
    width: min(22rem, 100%);
    padding: 1.5rem 1.25rem 1.25rem;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: var(--surface);
    box-shadow: var(--shadow);
    text-align: center;
  }

  .login-form {
    display: grid;
    gap: 0.85rem;
    margin-block-start: 1.15rem;
    text-align: start;
  }

  .logo {
    display: grid;
    place-items: center;
    width: 2.75rem;
    height: 2.75rem;
    margin: 0 auto 0.75rem;
    border-radius: 10px;
    background: var(--accent);
    color: white;
    font-weight: 700;
    font-size: 1.2rem;
  }

  .logo.sm {
    width: 2rem;
    height: 2rem;
    margin: 0;
    font-size: 0.9rem;
    border-radius: 8px;
  }

  .lede {
    margin: 0.3rem 0 0;
    color: var(--muted);
    font-size: 0.9rem;
  }

  .app {
    min-height: 100dvh;
  }

  .rail {
    display: none;
  }

  .stage {
    padding: 0 1rem calc(5.5rem + env(safe-area-inset-bottom));
  }

  .mobile-bar {
    position: sticky;
    top: 0;
    z-index: 3;
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding-block: 0.7rem;
    padding-top: max(0.7rem, env(safe-area-inset-top));
    background: color-mix(in srgb, var(--bg) 90%, transparent);
    backdrop-filter: blur(16px);
  }

  .mobile-bar h1 {
    flex: 1;
    font-size: 1.15rem;
  }

  .mobile-search {
    display: none;
    margin-block-end: 0.7rem;
  }

  .app:has(#mobile-search-btn[aria-pressed="true"]) .mobile-search {
    display: block;
  }

  .field {
    display: grid;
    gap: 0.35rem;
  }

  .search label,
  .mobile-search label,
  .field label,
  .swatches legend {
    color: var(--muted);
    font-size: 0.8rem;
    font-weight: 500;
  }

  input:where([type="text"], [type="password"], [type="search"]),
  textarea {
    width: 100%;
    min-height: 2.75rem;
    padding: 0.65rem 0.8rem;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--surface);
  }

  textarea {
    min-height: 7.5rem;
    resize: vertical;
    line-height: 1.6;
  }

  .chips,
  .tags {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.45rem;
    overflow-x: auto;
    padding-block: 0.1rem 0.75rem;
    scrollbar-width: none;
  }

  .chips::-webkit-scrollbar,
  .tags::-webkit-scrollbar {
    display: none;
  }

  .chip {
    flex: 0 0 auto;
    min-height: 2rem;
    padding-inline: 0.75rem;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--muted);
    font-size: 0.8rem;
  }

  .chip[aria-pressed="true"],
  .chip:hover:not(:disabled) {
    color: var(--accent);
    border-color: color-mix(in srgb, var(--accent) 35%, var(--line));
    background: var(--accent-soft);
  }

  .board {
    display: grid;
    gap: 0.75rem;
  }

  .card {
    padding: 1rem;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    border-inline-start: 4px solid var(--mark, #9ca3af);
    background: var(--surface);
    box-shadow: var(--shadow);
  }

  .card.ink { --mark: #9ca3af; }
  .card.cinnabar { --mark: #ef4444; }
  .card.moss { --mark: #10b981; }
  .card.gold { --mark: #f59e0b; }

  .card header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.6rem;
  }

  .card h2 {
    font-size: 1rem;
  }

  .card .meta {
    margin: 0.15rem 0 0;
    color: var(--muted);
    font-size: 0.75rem;
  }

  .card .body {
    margin: 0.55rem 0 0;
    color: #374151;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    display: -webkit-box;
    -webkit-line-clamp: 4;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .card .tag-row,
  .card-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin-block-start: 0.7rem;
  }

  .stamp {
    display: inline-flex;
    align-items: center;
    min-height: 1.4rem;
    padding-inline: 0.45rem;
    border-radius: 999px;
    background: #f3f4f6;
    color: #4b5563;
    font-size: 0.72rem;
  }

  .icon-btn,
  .ghost,
  .primary,
  .fab,
  .chip {
    border: 0;
    background: transparent;
  }

  .icon-btn {
    width: 2.5rem;
    height: 2.5rem;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--surface);
  }

  .ghost,
  .primary,
  .fab {
    min-height: 2.75rem;
    border-radius: 10px;
    padding-inline: 1rem;
    font-weight: 600;
  }

  .ghost {
    border: 1px solid var(--line);
    color: #374151;
    background: var(--surface);
  }

  .primary,
  .fab {
    background: var(--accent);
    color: white;
  }

  .login-form .primary,
  .sheet footer .primary {
    width: 100%;
  }

  .fab {
    position: fixed;
    z-index: 5;
    inset-inline-end: 1rem;
    inset-block-end: max(1rem, env(safe-area-inset-bottom));
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    box-shadow: 0 10px 24px rgb(15 118 110 / 28%);
  }

  body:has(.sheet[open]) .fab {
    display: none;
  }

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

  .empty {
    margin: 3rem 0;
    text-align: center;
    color: var(--muted);
  }

  .sheet {
    width: min(36rem, 100%);
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border-radius: 0;
    background: var(--surface);
  }

  .sheet::backdrop {
    background: rgb(17 24 39 / 40%);
  }

  .sheet form {
    display: grid;
    grid-template-rows: auto 1fr auto;
    height: 100%;
    min-height: 0;
  }

  .sheet header,
  .sheet footer {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.85rem 1rem;
  }

  .sheet header {
    justify-content: space-between;
    border-block-end: 1px solid var(--line);
    padding-top: max(0.85rem, env(safe-area-inset-top));
  }

  .sheet footer {
    justify-content: flex-end;
    border-block-start: 1px solid var(--line);
    padding-bottom: max(0.85rem, env(safe-area-inset-bottom));
    background: var(--surface);
  }

  .sheet footer:has(#delete-btn:not([hidden])) {
    justify-content: space-between;
  }

  .sheet-body {
    display: grid;
    align-content: start;
    gap: 0.9rem;
    min-height: 0;
    overflow: auto;
    padding: 1rem;
  }

  .swatches {
    border: 0;
    margin: 0;
    padding: 0;
    display: grid;
    gap: 0.45rem;
  }

  .swatch-row {
    display: flex;
    gap: 0.65rem;
  }

  .swatch {
    display: grid;
    justify-items: center;
    gap: 0.25rem;
    width: 3rem;
    color: var(--muted);
    font-size: 0.72rem;
  }

  .swatch input {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    clip-path: inset(50%);
    overflow: hidden;
  }

  .dot {
    width: 1.5rem;
    height: 1.5rem;
    border-radius: 50%;
    box-shadow: inset 0 0 0 1px rgb(17 24 39 / 12%);
  }

  .dot.ink { background: #9ca3af; }
  .dot.cinnabar { background: #ef4444; }
  .dot.moss { background: #10b981; }
  .dot.gold { background: #f59e0b; }

  .swatch:has(:checked) {
    color: var(--ink);
  }

  .swatch:has(:checked) .dot {
    outline: 2px solid var(--accent);
    outline-offset: 3px;
  }

  .pin {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-height: 2.25rem;
    color: #374151;
    font-size: 0.9rem;
  }

  .pin input {
    width: 1.1rem;
    height: 1.1rem;
    margin: 0;
    flex: 0 0 auto;
  }

  .hint {
    margin: 0;
    color: var(--muted);
    font-size: 0.8rem;
  }

  .hint.error {
    color: var(--danger);
  }

  .toast {
    position: fixed;
    z-index: 8;
    inset-inline: 1rem;
    inset-block-end: calc(5rem + env(safe-area-inset-bottom));
    width: max-content;
    max-width: calc(100% - 2rem);
    margin-inline: auto;
    padding: 0.65rem 0.95rem;
    border-radius: 999px;
    background: #111827;
    color: white;
    font-size: 0.875rem;
  }

  .sync {
    margin: 0.25rem 0 0;
    color: var(--muted);
    font-size: 0.75rem;
  }

  .stats strong {
    display: block;
    font-size: 1.8rem;
    line-height: 1;
    letter-spacing: -0.04em;
  }
}

@layer utilities {
  .visually-hidden {
    position: absolute;
    clip-path: inset(50%);
    overflow: hidden;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    border: 0;
  }

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

@media (min-width: 880px) {
  .app {
    display: grid;
    grid-template-columns: 16rem minmax(0, 1fr);
    min-height: 100dvh;
  }

  .rail {
    display: flex;
    position: sticky;
    top: 0;
    flex-direction: column;
    gap: 1.1rem;
    height: 100dvh;
    padding: 1.25rem 1rem;
    border-inline-end: 1px solid var(--line);
    background: #f8fafc;
  }

  .brand {
    display: flex;
    align-items: center;
    gap: 0.7rem;
  }

  .brand h1 {
    font-size: 1.15rem;
  }

  .tags {
    flex-wrap: wrap;
    overflow: visible;
    padding-block: 0;
  }

  .rail-actions {
    margin-block-start: auto;
    display: flex;
    gap: 0.45rem;
  }

  .mobile-bar,
  .mobile-search {
    display: none;
  }

  .stage {
    padding: 1.25rem 1.4rem 5.5rem;
  }

  .board {
    grid-template-columns: repeat(auto-fill, minmax(17rem, 1fr));
    align-items: start;
  }

  .fab {
    inset-inline-end: 1.4rem;
    inset-block-end: 1.4rem;
  }

  .sheet {
    width: min(32rem, calc(100vw - 3rem));
    height: min(40rem, calc(100dvh - 3rem));
    max-height: calc(100dvh - 3rem);
    margin: auto;
    border-radius: 16px;
    overflow: hidden;
  }

  .sheet header {
    padding-top: 0.85rem;
  }

  .sheet footer {
    padding-bottom: 0.85rem;
  }

  .sheet footer .primary {
    width: auto;
    min-width: 6rem;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0b1220;
    --surface: #151c2c;
    --ink: #e5e7eb;
    --muted: #9ca3af;
    --line: #243044;
    --accent: #2dd4bf;
    --accent-soft: #134e4a;
    --danger: #f87171;
  }

  .rail {
    background: #0f172a;
  }

  .card .body,
  .pin {
    color: #cbd5e1;
  }

  .stamp {
    background: #1f2937;
    color: #d1d5db;
  }

  input:where([type="text"], [type="password"], [type="search"]),
  textarea,
  .ghost,
  .icon-btn,
  .chip {
    background: var(--surface);
  }
}
