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

[hidden] {
    display: none !important;
}

:root {
    --bg: #f5f5f5;
    --surface: #ffffff;
    --border: #ddd;
    --text: #222;
    --text-muted: #777;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --selected: #e8f0fe;
    --hover: #f0f0f0;
    --danger: #c00;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1a1a1a;
        --surface: #242424;
        --border: #3a3a3a;
        --text: #e0e0e0;
        --text-muted: #999;
        --accent: #5b9aff;
        --accent-hover: #4a85e6;
        --selected: #2a3a52;
        --hover: #2e2e2e;
        --danger: #f55;
    }
}

:root {
    --space: 0.5rem;
    --font-xs: 0.6875rem;
    --font-sm: 0.75rem;
    --font-md: 0.875rem;
    --font-base: 0.9375rem;
    --font-lg: 1.125rem;
    --font-xl: 1.375rem;

    --pane-feeds: 14rem;
    --pane-posts: 20rem;
    --thumb-size: 3.5rem;
    --detail-max: 45rem;
    --modal-width: 26rem;
    --icon-size: 1.25rem;
    --focus-edge: 2px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100dvh;
    overflow: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
}

/* Header */

header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space) calc(var(--space) * 2);
    background: var(--surface);
    border-block-end: 1px solid var(--border);
    flex-shrink: 0;

    h1 {
        font-size: var(--font-lg);
        font-weight: 700;
    }
}

#header-actions {
    display: flex;
    align-items: center;
    gap: var(--space);
}

.icon {
    display: block;
    width: var(--icon-size);
    height: var(--icon-size);
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

#subscribe-btn {
    padding: calc(var(--space) * 0.75);
    background: var(--accent);
    color: #fff;
    border: 1px solid var(--accent);
    border-radius: 6px;
    cursor: pointer;

    &:hover {
        background: var(--accent-hover);
        border-color: var(--accent-hover);
    }
}

#logout-btn {
    padding: calc(var(--space) * 0.75);
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text-muted);
    cursor: pointer;

    &:hover {
        background: var(--hover);
        color: var(--text);
    }
}

/* Main 3-pane layout */

main {
    display: flex;
    flex: 1;
    min-height: 0;
}

/* Pane titles: plain headings in the wide layout, expand/collapse toggles in
   the narrow one. */

.pane-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space);
    padding: calc(var(--space) * 1.5);
    background: var(--surface);
    border-block-end: 1px solid var(--border);

    h2 {
        flex: 1;
        min-width: 0;
        font-size: var(--font-sm);
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--text-muted);
    }
}

.pane-toggle {
    display: block;
    width: 100%;
    padding: 0;
    border: none;
    background: none;
    font: inherit;
    text-align: start;
    text-transform: inherit;
    letter-spacing: inherit;
    color: inherit;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    &:disabled {
        cursor: default;
    }
}

#detail-pane > .pane-header {
    display: none;
}

/* Panes are focusable so the arrow keys know where to act. Instead of a focus
   ring around the whole pane, mark it with an accent edge along the top and
   highlight its title. The edge is a border rather than a shadow so that it
   stays put when the pane scrolls and child backgrounds can't cover it. */

#feeds-pane, #posts-pane, #detail-pane {
    outline: none;
    border-block-start: var(--focus-edge) solid transparent;

    &:focus-within {
        border-block-start-color: var(--accent);

        > .pane-header h2 {
            color: var(--accent);
        }
    }
}

/* Feeds pane (left) */

#feeds-pane {
    width: var(--pane-feeds);
    flex-shrink: 0;
    background: var(--surface);
    border-inline-end: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .pane-header {
        padding: calc(var(--space) * 1.5) calc(var(--space) * 1.5) var(--space);
        border-block-end: none;
    }
}

#feeds-list {
    list-style: none;
    overflow-y: auto;
    flex: 1;

    li {
        padding: var(--space) calc(var(--space) * 1.5);
        font-size: var(--font-md);
        cursor: pointer;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-block-end: 1px solid var(--border);

        &:hover {
            background: var(--hover);
        }

        &.selected {
            background: var(--selected);
            font-weight: 600;
        }
    }
}

.feed-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}

.feed-unread-count {
    font-size: var(--font-xs);
    font-weight: 700;
    color: var(--accent);
    margin-inline-start: calc(var(--space) * 0.75);
    flex-shrink: 0;
}

/* Posts pane (center) */

#posts-pane {
    width: var(--pane-posts);
    flex-shrink: 0;
    background: var(--surface);
    border-inline-end: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#posts-toolbar {
    padding: calc(var(--space) * 1.5);
}

#feed-actions {
    display: flex;
    gap: calc(var(--space) * 0.25);
    list-style: none;

    button {
        padding: 0 calc(var(--space) * 0.5);
        border: none;
        background: none;
        font-size: var(--font-lg);
        line-height: 1;
        color: var(--text-muted);
        cursor: pointer;

        &:disabled {
            opacity: 0.4;
            cursor: progress;
        }
    }

    #feed-refresh:hover {
        color: var(--accent);
    }

    #feed-remove:hover {
        color: var(--danger);
    }
}

#posts-filter {
    font-size: var(--font-sm);
    padding: 0.125rem calc(var(--space) * 0.75);
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--surface);
}

#posts-list {
    list-style: none;
    overflow-y: auto;
    flex: 1;

    li {
        display: flow-root;
        padding: calc(var(--space) * 1.25) calc(var(--space) * 1.5);
        cursor: pointer;
        border-block-end: 1px solid var(--border);

        &:hover {
            background: var(--hover);
        }

        &.selected {
            background: var(--selected);
        }

        &.unread .post-title {
            font-weight: 700;
        }
    }
}

.post-title {
    font-size: var(--font-md);
    line-height: 1.3;
    margin-bottom: calc(var(--space) * 0.5);
    display: flex;
    align-items: flex-start;
    gap: calc(var(--space) * 0.75);
}

.unread-dot {
    width: 7px;
    height: 7px;
    background: var(--accent);
    border-radius: 50%;
    flex-shrink: 0;
    margin-top: 5px;
}

.post-thumb {
    float: inline-end;
    width: var(--thumb-size);
    height: var(--thumb-size);
    object-fit: cover;
    margin-inline-start: var(--space);
    border-radius: 4px;
    background: var(--bg);
}

.post-date {
    font-size: var(--font-xs);
    color: var(--text-muted);
}

.post-snippet {
    font-size: var(--font-sm);
    color: var(--text-muted);
    margin-top: 0.125rem;
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Detail pane (right) */

#detail-pane {
    flex: 1;
    background: var(--surface);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

#detail-empty {
    flex: 1;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: var(--font-base);
    display: none;

    &:not([hidden]) {
        display: flex;
    }
}

#detail-content {
    padding: calc(var(--space) * 3);
    max-width: var(--detail-max);
}

#detail-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: calc(var(--space) * 1.5);
    margin-bottom: var(--space);
    padding-inline: 0;
}

#detail-title {
    font-size: var(--font-xl);
    line-height: 1.3;
}

#detail-toggle-read {
    padding: calc(var(--space) * 0.5) calc(var(--space) * 1.25);
    font-size: var(--font-sm);
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--surface);
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;

    &:hover {
        background: var(--hover);
    }
}

#detail-meta {
    font-size: var(--font-sm);
    color: var(--text-muted);
    margin-bottom: calc(var(--space) * 2.5);
}

#detail-body {
    font-size: var(--font-base);
    line-height: 1.6;

    p {
        margin-block-end: 1em;
    }

    img {
        max-width: 100%;
        height: auto;
        border-radius: 4px;
        margin: var(--space) 0;
    }

    a {
        color: var(--accent);
    }

    pre {
        background: var(--bg);
        padding: calc(var(--space) * 1.5);
        border-radius: 4px;
        overflow-x: auto;
        font-size: var(--font-sm);
    }

    blockquote {
        border-inline-start: 3px solid var(--border);
        padding-inline-start: calc(var(--space) * 1.5);
        color: var(--text-muted);
        margin: var(--space) 0;
    }
}

#detail-link {
    display: inline-block;
    margin-top: calc(var(--space) * 2.5);
    font-size: var(--font-sm);
    color: var(--accent);
}

#detail-next {
    margin-block-start: calc(var(--space) * 4);
    padding-block: calc(var(--space) * 2);
    border-block-start: 1px solid var(--border);
    text-align: center;
    font-size: var(--font-sm);
    color: var(--text-muted);

    #detail-next-title {
        padding: 0;
        border: none;
        background: none;
        font: inherit;
        font-size: var(--font-md);
        font-weight: 600;
        color: var(--text);
        cursor: pointer;

        &:hover {
            color: var(--accent);
            text-decoration: underline;
        }
    }

    progress {
        width: 100%;
        height: 3px;
        margin-block-start: calc(var(--space) * 1.5);
        accent-color: var(--accent);
        appearance: none;
        border: none;
        background: var(--border);

        &::-webkit-progress-bar {
            background: var(--border);
        }

        &::-webkit-progress-value {
            background: var(--accent);
        }

        &::-moz-progress-bar {
            background: var(--accent);
        }
    }
}

/* Narrow layout: stack the panes; only the expanded one shows its contents */

@media (width < 48rem) {
    main {
        flex-direction: column;
    }

    #feeds-pane, #posts-pane {
        width: auto;
        border-inline-end: none;
        border-block-end: 1px solid var(--border);
    }

    #feeds-pane, #posts-pane, #detail-pane {
        flex: none;
        min-height: 0;

        &.expanded {
            flex: 1;
        }
    }

    :is(#feeds-pane, #posts-pane, #detail-pane):not(.expanded) > :not(.pane-header) {
        display: none;
    }

    #feeds-pane .pane-header {
        padding-block-end: calc(var(--space) * 1.5);
    }

    /* A collapsed pane's own bottom border separates it from the next one. */
    .pane-header {
        border-block-end: none;
    }

    :is(#feeds-pane, #posts-pane, #detail-pane).expanded > .pane-header {
        border-block-end: 1px solid var(--border);
    }

    #detail-pane > .pane-header {
        display: flex;
        position: sticky;
        inset-block-start: 0;
        z-index: 1;
    }

    .pane-toggle {
        cursor: pointer;

        &::before {
            content: "\25b8";
            display: inline-block;
            width: 1.25em;
        }

        &[aria-expanded="true"]::before {
            content: "\25be";
        }
    }

    #detail-content {
        padding: calc(var(--space) * 2);
    }
}

/* Modal */

dialog#modal {
    background: var(--surface);
    border: none;
    border-radius: 10px;
    padding: calc(var(--space) * 3);
    width: var(--modal-width);
    max-width: 90vw;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);

    &::backdrop {
        background: rgba(0, 0, 0, 0.4);
    }
}

#modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: calc(var(--space) * 2);

    h2 {
        font-size: 1rem;
    }
}

#modal-close {
    background: none;
    border: none;
    font-size: var(--font-xl);
    color: var(--text-muted);
    cursor: pointer;
    padding: 0 2px;
    line-height: 1;

    &:hover {
        color: var(--text);
    }
}

#feed-url-input {
    width: 100%;
    padding: var(--space) calc(var(--space) * 1.25);
    font-size: var(--font-md);
    border: 1px solid var(--border);
    border-radius: 6px;
    outline: none;

    &:focus {
        border-color: var(--accent);
        box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent);
    }
}

#modal-error {
    margin-top: var(--space);
    font-size: var(--font-sm);
    color: var(--danger);
}

#modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--space);
    margin-top: calc(var(--space) * 2);
}

#modal-cancel {
    padding: calc(var(--space) * 0.75) calc(var(--space) * 1.75);
    font-size: var(--font-sm);
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    cursor: pointer;

    &:hover {
        background: var(--hover);
    }
}

#modal-submit {
    padding: calc(var(--space) * 0.75) calc(var(--space) * 1.75);
    font-size: var(--font-sm);
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;

    &:hover {
        background: var(--accent-hover);
    }

    &:disabled {
        opacity: 0.6;
        cursor: not-allowed;
    }
}

.loading {
    text-align: center;
    padding: calc(var(--space) * 2.5);
    color: var(--text-muted);
    font-size: var(--font-sm);
}

/* Login / register page */

body.login {
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
}

#login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
    padding: calc(var(--space) * 4);
    width: 22rem;
    max-width: 90vw;

    h1 {
        font-size: var(--font-xl);
        text-align: center;
        margin-bottom: calc(var(--space) * 2.5);
    }
}

#auth-tabs {
    display: flex;
    gap: calc(var(--space) * 0.5);
    border-bottom: 1px solid var(--border);
    margin-bottom: calc(var(--space) * 2);
    padding-bottom: var(--space);

    .tab {
        background: none;
        border: none;
        font-size: var(--font-md);
        color: var(--text-muted);
        cursor: pointer;
        padding: calc(var(--space) * 0.5) var(--space);
        border-radius: 4px;

        &.selected {
            color: var(--accent);
            font-weight: 600;
        }

        &:hover:not(.selected) {
            color: var(--text);
        }
    }
}

#login-form, #register-form {
    display: flex;
    flex-direction: column;
    gap: calc(var(--space) * 0.75);

    label {
        font-size: var(--font-sm);
        font-weight: 600;
        color: var(--text-muted);
        margin-top: calc(var(--space) * 0.5);
    }

    input {
        width: 100%;
        padding: var(--space) calc(var(--space) * 1.25);
        font-size: var(--font-md);
        border: 1px solid var(--border);
        border-radius: 6px;
        background: var(--surface);
        color: var(--text);
        outline: none;

        &:focus {
            border-color: var(--accent);
            box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent);
        }
    }

    p {
        font-size: var(--font-sm);
        color: var(--danger);
        min-height: 1em;
    }

    button[type="submit"] {
        margin-top: var(--space);
        padding: calc(var(--space) * 0.875) calc(var(--space) * 1.75);
        background: var(--accent);
        color: #fff;
        border: none;
        border-radius: 6px;
        font-size: var(--font-md);
        font-weight: 600;
        cursor: pointer;

        &:hover {
            background: var(--accent-hover);
        }
    }
}
