/* Calm library — quiet, reading-first bookmark layout */

:root {
    --bg: #f7f5f0;
    --surface: #fffefb;
    --surface-2: #efece4;
    --text: #1f1d1a;
    --text-2: #5c5850;
    --text-3: #8a857a;
    --line: #e4e0d6;
    --accent: #b4532a;
    --accent-soft: rgba(180, 83, 42, 0.1);
    --danger: #b3261e;
    --shadow: 0 1px 2px rgba(31, 29, 26, 0.04), 0 4px 16px rgba(31, 29, 26, 0.05);
    --shadow-hover: 0 2px 4px rgba(31, 29, 26, 0.06), 0 12px 32px rgba(31, 29, 26, 0.09);

    --font-serif: "Fraunces", Georgia, "Times New Roman", serif;
    --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

    --header-h: 60px;
    --sidebar-w: 232px;
    --radius: 12px;
    --ease: cubic-bezier(0.2, 0.8, 0.2, 1);

    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #161513;
        --surface: #1f1d1a;
        --surface-2: #2a2824;
        --text: #ece8df;
        --text-2: #b3ada1;
        --text-3: #847e72;
        --line: #34312b;
        --accent: #e08a5f;
        --accent-soft: rgba(224, 138, 95, 0.14);
        --danger: #f2948c;
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
        --shadow-hover: 0 8px 28px rgba(0, 0, 0, 0.45);
        color-scheme: dark;
    }
}

:root[data-theme="dark"] {
    --bg: #161513;
    --surface: #1f1d1a;
    --surface-2: #2a2824;
    --text: #ece8df;
    --text-2: #b3ada1;
    --text-3: #847e72;
    --line: #34312b;
    --accent: #e08a5f;
    --accent-soft: rgba(224, 138, 95, 0.14);
    --danger: #f2948c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-hover: 0 8px 28px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
}

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

body {
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-sans);
    font-size: 15px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
}

ul {
    list-style: none;
}

a {
    color: inherit;
    text-decoration: none;
}

button {
    font: inherit;
    color: inherit;
    background: none;
    border: none;
    cursor: pointer;
}

svg {
    width: 18px;
    height: 18px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
}

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

/* Header */
.app-header {
    position: sticky;
    top: 0;
    z-index: 20;
    height: var(--header-h);
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 0 24px;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--line);
}

.header-start {
    width: calc(var(--sidebar-w) - 24px);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: -6px;
}

.brand {
    font-family: var(--font-serif);
    font-size: 1.4rem;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.search {
    flex: 1;
    min-width: 0;
    max-width: 520px;
    display: flex;
    align-items: center;
    gap: 10px;
    height: 38px;
    padding: 0 12px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    color: var(--text-3);
    transition: border-color 150ms var(--ease), box-shadow 150ms var(--ease);
}

.search:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.search input {
    flex: 1;
    min-width: 0;
    border: none;
    outline: none;
    background: transparent;
    color: var(--text);
    font: inherit;
}

.search input::placeholder {
    color: var(--text-3);
}

.search input::-webkit-search-cancel-button {
    display: none;
}

kbd {
    font-family: var(--font-sans);
    font-size: 0.75rem;
    padding: 1px 7px;
    border: 1px solid var(--line);
    border-radius: 5px;
    color: var(--text-3);
}

.header-actions {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}

.view-toggle {
    display: flex;
    padding: 3px;
    background: var(--surface-2);
    border-radius: 9px;
}

.icon-btn {
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    border-radius: 7px;
    color: var(--text-2);
    transition: background 150ms var(--ease), color 150ms var(--ease);
}

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

.view-toggle .icon-btn[aria-pressed="true"] {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
}

#theme-btn .icon-sun {
    display: none;
}

:root[data-theme="dark"] #theme-btn .icon-sun {
    display: block;
}

:root[data-theme="dark"] #theme-btn .icon-moon {
    display: none;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) #theme-btn .icon-sun {
        display: block;
    }

    :root:not([data-theme="light"]) #theme-btn .icon-moon {
        display: none;
    }
}

/* Layout */
.layout {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
}

body.sidebar-collapsed .layout {
    grid-template-columns: minmax(0, 1fr);
}

body.sidebar-collapsed .sidebar {
    display: none;
}

/* Sidebar */
.sidebar {
    --sidebar-bg: color-mix(in srgb, var(--surface-2) 45%, var(--bg));
    position: sticky;
    top: var(--header-h);
    height: calc(100vh - var(--header-h));
    overflow-y: auto;
    padding: 0 10px 32px 12px;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--line);
    scrollbar-width: thin;
    scrollbar-color: var(--line) transparent;
}

.sidebar-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 22px 10px 6px;
}

.sidebar-label {
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-3);
}

.sidebar-total {
    font-size: 0.72rem;
    font-variant-numeric: tabular-nums;
    color: var(--text-3);
}

.cat-group > ul {
    display: flex;
    flex-direction: column;
    gap: 1px;
}

/* Letter headings stick while scrolling through their group */
.group-label {
    position: sticky;
    top: 0;
    z-index: 1;
    padding: 12px 10px 4px;
    background: var(--sidebar-bg);
    font-family: var(--font-serif);
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-3);
}

.cat-group.recent {
    padding-bottom: 10px;
    margin-bottom: 4px;
    border-bottom: 1px solid var(--line);
}

.cat-group.recent .group-label {
    position: static;
    font-family: var(--font-sans);
    font-size: 0.72rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

#categories button {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 10px;
    border: 1px solid transparent;
    border-radius: 8px;
    color: var(--text-2);
    font-size: 0.92rem;
    text-align: left;
    transition: background 150ms var(--ease), color 150ms var(--ease);
}

#categories button:hover {
    background: color-mix(in srgb, var(--surface-2) 80%, transparent);
    color: var(--text);
}

#categories button[aria-current="true"] {
    background: var(--surface);
    border-color: var(--line);
    box-shadow: var(--shadow);
    color: var(--text);
    font-weight: 600;
}

.cat-dot {
    width: 8px;
    height: 8px;
    flex-shrink: 0;
    border-radius: 50%;
    background: hsl(var(--hue) 42% 58%);
    transition: transform 150ms var(--ease);
}

#categories button[aria-current="true"] .cat-dot {
    transform: scale(1.25);
    box-shadow: 0 0 0 3px hsl(var(--hue) 42% 58% / 0.2);
}

.cat-name {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.cat-count {
    font-size: 0.75rem;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    color: var(--text-3);
}

#categories .empty-note {
    padding: 8px 10px;
    color: var(--text-3);
    font-size: 0.88rem;
}

#categories .skeleton-line {
    margin: 6px 10px;
}

/* Main */
.main {
    min-width: 0;
    padding: 36px 40px 64px;
}

.page-head {
    max-width: 1400px;
    margin-bottom: 28px;
}

.page-head h2 {
    font-family: var(--font-serif);
    font-size: 2.1rem;
    font-weight: 600;
    line-height: 1.15;
    letter-spacing: -0.015em;
}

#page-meta {
    margin-top: 6px;
    color: var(--text-3);
}

/* Grid view: masonry via CSS columns so tweets keep their natural height */
.grid[data-view="grid"] {
    max-width: 1400px;
    columns: 300px;
    column-gap: 20px;
}

.grid[data-view="grid"] > * {
    break-inside: avoid;
    margin-bottom: 20px;
}

/* List view */
.grid[data-view="list"] {
    max-width: 760px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Cards */
.card {
    display: block;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px;
    box-shadow: var(--shadow);
    animation: rise 320ms var(--ease) both;
    transition: box-shadow 200ms var(--ease), transform 200ms var(--ease), border-color 200ms var(--ease);
}

a.card:hover {
    box-shadow: var(--shadow-hover);
    transform: translateY(-2px);
}

@keyframes rise {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
}

.card-source {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.8rem;
    color: var(--text-3);
}

.card-source img {
    width: 16px;
    height: 16px;
    border-radius: 4px;
    flex-shrink: 0;
}

.card-domain {
    color: var(--text-2);
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.card-source time {
    margin-left: auto;
    flex-shrink: 0;
}

.card-title {
    margin-top: 10px;
    font-family: var(--font-serif);
    font-size: 1.12rem;
    font-weight: 600;
    line-height: 1.3;
    overflow-wrap: anywhere;
}

a.card:hover .card-title {
    color: var(--accent);
}

.card-note {
    margin-top: 8px;
    color: var(--text-2);
    font-size: 0.92rem;
    overflow-wrap: anywhere;
}

.tweet-card .card-note {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--line);
}

.tweet-slot {
    margin-top: 10px;
}

.tweet-slot .twitter-tweet {
    margin: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
}

.tweet-fallback {
    display: inline-block;
    color: var(--accent);
    font-weight: 500;
}

/* List view tightens link cards into rows */
.grid[data-view="list"] .link-card {
    display: grid;
    grid-template-columns: 1fr auto;
    column-gap: 16px;
    padding: 14px 18px;
}

.grid[data-view="list"] .link-card .card-source {
    grid-column: 1 / -1;
}

.grid[data-view="list"] .link-card .card-title {
    margin-top: 4px;
    font-size: 1.02rem;
}

.grid[data-view="list"] .link-card .card-note {
    grid-column: 1 / -1;
    margin-top: 2px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.grid[data-view="list"] .tweet-slot {
    max-width: 550px;
}

/* Skeletons */
.skeleton,
.skeleton-line {
    background: linear-gradient(90deg, var(--surface-2) 0%, var(--line) 50%, var(--surface-2) 100%);
    background-size: 200% 100%;
    animation: shimmer 1.4s linear infinite;
    border-radius: 6px;
}

.skeleton-line {
    height: 30px;
    margin: 3px 0;
}

.tweet-skeleton {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.tweet-skeleton .skeleton:nth-child(1) {
    height: 14px;
    width: 45%;
}

.tweet-skeleton .skeleton:nth-child(2),
.tweet-skeleton .skeleton:nth-child(3) {
    height: 12px;
}

.tweet-skeleton .skeleton:nth-child(3) {
    width: 75%;
}

.tweet-skeleton .skeleton:nth-child(4) {
    height: 150px;
    margin-top: 4px;
}

.card-skeleton {
    height: 140px;
    border-radius: var(--radius);
}

@keyframes shimmer {
    from {
        background-position: 200% 0;
    }
    to {
        background-position: -200% 0;
    }
}

/* Empty state */
.empty-state {
    max-width: 420px;
    padding: 64px 0;
    color: var(--text-2);
}

.empty-state h3 {
    font-family: var(--font-serif);
    font-size: 1.3rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 6px;
}

#sentinel {
    height: 1px;
}

/* Toasts (errors only) */
#toast-region {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 50;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    padding: 10px 14px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-left: 3px solid var(--danger);
    border-radius: 8px;
    box-shadow: var(--shadow-hover);
    font-size: 0.9rem;
    animation: rise 200ms var(--ease) both;
}

/* Tablet */
@media (max-width: 1000px) {
    :root {
        --sidebar-w: 200px;
    }

    .main {
        padding: 28px 24px 48px;
    }
}

/* Phone: sidebar becomes a row of chips under the header */
@media (max-width: 720px) {
    .app-header {
        gap: 12px;
        padding: 0 16px;
    }

    .brand {
        font-size: 1.2rem;
    }

    .search kbd,
    .view-toggle {
        display: none;
    }

    .layout {
        display: block;
    }

    .sidebar-btn {
        display: none;
    }

    .header-start {
        width: auto;
        margin-left: 0;
    }

    /* Collapsing doesn't apply to the chip row */
    body.sidebar-collapsed .sidebar {
        display: block;
    }

    .sidebar {
        position: sticky;
        z-index: 10;
        height: auto;
        padding: 10px 16px;
        overflow-x: auto;
        overflow-y: hidden;
        border-right: none;
        border-bottom: 1px solid var(--line);
        background: var(--bg);
        scrollbar-width: none;
    }

    .sidebar::-webkit-scrollbar {
        display: none;
    }

    .sidebar-head,
    .group-label,
    .cat-group.recent {
        display: none;
    }

    #categories,
    .cat-group > ul {
        display: flex;
        flex-direction: row;
        gap: 8px;
    }

    .cat-group {
        display: contents;
    }

    #categories li {
        flex-shrink: 0;
    }

    #categories button {
        padding: 6px 14px;
        border-color: var(--line);
        border-radius: 999px;
        white-space: nowrap;
    }

    #categories button[aria-current="true"] {
        border-color: var(--accent);
        box-shadow: none;
    }

    .cat-count {
        display: none;
    }

    #categories .skeleton-line {
        width: 90px;
        height: 32px;
        margin: 0;
        border-radius: 999px;
    }

    .main {
        padding: 20px 16px 40px;
    }

    .page-head h2 {
        font-size: 1.6rem;
    }

    .grid[data-view="grid"] {
        columns: 1;
    }
}

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

@media print {
    .app-header,
    .sidebar,
    #toast-region {
        display: none;
    }

    .layout {
        display: block;
    }

    .card {
        box-shadow: none;
        break-inside: avoid;
    }
}
