:root {
    color-scheme: light;
    --script-font-size: 16px;
    --bg: #f5f7f3;
    --surface: #ffffff;
    --ink: #20231f;
    --muted: #70776d;
    --line: #dfe5da;
    --accent: #1f7a5a;
    --accent-ink: #ffffff;
    --danger: #b83a3a;
    --read: #eef4ed;
    --shadow: 0 10px 28px rgba(30, 42, 33, 0.08);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
}

body.focus-mode {
    overflow: hidden;
}

button,
input,
textarea {
    font: inherit;
}

.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}

.login-card {
    width: min(100%, 380px);
    padding: 26px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.login-card h1 {
    margin: 0;
    font-size: 24px;
    letter-spacing: 0;
}

.topbar h1 {
    margin: 0;
    font-size: 17px;
    line-height: 1.15;
    letter-spacing: 0;
    white-space: nowrap;
}

.login-card p,
.status-line {
    margin: 8px 0 0;
    color: var(--muted);
    font-size: 14px;
}

.error {
    margin-top: 16px;
    color: var(--danger);
    font-size: 14px;
}

.password-input,
.edit-dialog textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    color: var(--ink);
    outline: none;
}

.password-input {
    height: 48px;
    margin-top: 18px;
    padding: 0 14px;
}

.password-input:focus,
.edit-dialog textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(31, 122, 90, 0.12);
}

.primary-button,
.ghost-button,
.script-card button,
.icon-button {
    min-height: 42px;
    border-radius: 8px;
    border: 1px solid transparent;
    cursor: pointer;
}

.primary-button {
    width: 100%;
    margin-top: 14px;
    background: var(--accent);
    color: var(--accent-ink);
    border-color: var(--accent);
    font-weight: 700;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: calc(6px + env(safe-area-inset-top)) 8px 6px;
    background: rgba(245, 247, 243, 0.94);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(14px);
}

.brand {
    flex: 0 0 auto;
}

.top-actions {
    display: flex;
    gap: 6px;
    flex: 0 0 auto;
}

.top-actions.is-hidden {
    display: none;
}

.category-tabs {
    flex: 1 1 auto;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 5px;
    min-width: 0;
}

.read-tabs {
    flex: 0 0 auto;
    display: grid;
    grid-template-columns: repeat(2, auto);
    gap: 4px;
}

.category-tab,
.read-tab {
    min-height: 32px;
    padding: 0 5px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--ink);
    font-size: 13px;
    font-weight: 700;
    line-height: 1.14;
    white-space: normal;
    overflow-wrap: anywhere;
}

.category-tab {
    display: grid;
    place-items: center;
    min-width: 0;
    height: 34px;
    padding: 3px 6px;
    text-align: center;
}

.role-tabs {
    position: sticky;
    top: calc(44px + env(safe-area-inset-top));
    z-index: 9;
    display: flex;
    gap: 6px;
    padding: 6px 8px;
    overflow-x: auto;
    background: rgba(245, 247, 243, 0.94);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(14px);
}

.role-tabs.is-hidden {
    display: none;
}

.role-tab {
    flex: 0 0 auto;
    min-height: 30px;
    padding: 0 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #eef2ec;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.role-tab.is-active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-ink);
    box-shadow: 0 0 0 2px rgba(31, 122, 90, 0.18), inset 0 0 0 1px rgba(255, 255, 255, 0.45);
}

.role-tab.is-active::before {
    content: "✓ ";
}

.read-tab {
    min-height: 30px;
    padding: 0 6px;
    font-size: 13px;
}

.category-tab.is-active,
.read-tab.is-active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-ink);
    box-shadow: 0 0 0 2px rgba(31, 122, 90, 0.18), inset 0 0 0 1px rgba(255, 255, 255, 0.45);
}

.read-tab.is-active::before {
    content: "✓ ";
}

.read-tab:not(.is-active) {
    background: #eef2ec;
    color: var(--muted);
}

.icon-button {
    width: 34px;
    min-height: 32px;
    display: grid;
    place-items: center;
    background: var(--surface);
    color: var(--ink);
    border-color: var(--line);
    text-decoration: none;
    font-size: 18px;
    line-height: 1;
}

@media (max-width: 430px) {
    .topbar {
        gap: 6px;
        padding-left: 6px;
        padding-right: 6px;
    }

    .topbar h1 {
        font-size: 16px;
    }

    .category-tab {
        height: 36px;
        font-size: 12px;
    }
}

.status-line {
    display: none;
}

.feed {
    width: min(100%, 900px);
    margin: 0 auto;
    padding: 8px 10px 90px;
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
}

.script-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: 0 4px 14px rgba(30, 42, 33, 0.04);
    overflow: hidden;
}

.script-card.is-focused {
    position: fixed;
    inset: 0;
    z-index: 30;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100dvh;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    overflow: auto;
}

.script-card.is-focused .script-head {
    position: sticky;
    top: 0;
    z-index: 1;
    padding: calc(12px + env(safe-area-inset-top)) 14px 10px;
    background: inherit;
    border-bottom: 1px solid var(--line);
}

.script-card.is-focused .script-content {
    flex: 1;
    padding: 18px 16px calc(92px + env(safe-area-inset-bottom));
    font-size: var(--script-font-size);
    line-height: 1.78;
}

.script-card.is-focused .script-actions {
    display: none;
}

.script-card.is-read {
    background: var(--read);
}

.script-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 12px 12px 0;
}

.script-id {
    font-size: 13px;
    color: var(--muted);
    font-weight: 700;
}

.read-badge {
    flex: 0 0 auto;
    padding: 4px 8px;
    border-radius: 999px;
    background: #e8efe8;
    color: #50604f;
    font-size: 12px;
}

.script-content {
    margin: 0;
    padding: 12px;
    white-space: pre-wrap;
    word-break: break-word;
    overflow-wrap: anywhere;
    font-size: var(--script-font-size);
    line-height: 1.68;
    touch-action: pan-y;
}

.dialogue-line {
    margin: 0 0 9px;
}

.dialogue-line:last-child {
    margin-bottom: 0;
}

.dialogue-role {
    color: var(--accent);
    font-weight: 700;
}

.dialogue-line.is-selected-role {
    font-weight: 900;
}

.dialogue-line.is-selected-role .dialogue-role,
.dialogue-line.is-selected-role span {
    font-weight: 900;
}

.script-actions {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    padding: 0 12px 12px;
}

.script-card button {
    background: #f8faf7;
    border-color: var(--line);
    color: var(--ink);
    font-size: 14px;
}

.script-card button.danger {
    color: var(--danger);
}

.loader {
    width: fit-content;
    min-height: 38px;
    margin: -72px auto 34px;
    padding: 9px 14px;
    color: var(--muted);
    font-size: 14px;
}

.edit-dialog {
    width: min(calc(100% - 24px), 560px);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0;
    box-shadow: var(--shadow);
}

.edit-dialog::backdrop {
    background: rgba(20, 24, 20, 0.42);
}

.edit-dialog form {
    padding: 16px;
}

.edit-dialog h2 {
    margin: 0 0 12px;
    font-size: 18px;
}

.edit-dialog textarea {
    resize: vertical;
    min-height: 260px;
    padding: 12px;
    line-height: 1.6;
}

.dialog-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 12px;
}

.dialog-actions .primary-button {
    margin-top: 0;
}

.ghost-button {
    background: #fff;
    border-color: var(--line);
}

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(24px + env(safe-area-inset-bottom));
    z-index: 20;
    transform: translate(-50%, 16px);
    max-width: calc(100% - 28px);
    padding: 10px 14px;
    border-radius: 8px;
    background: rgba(32, 35, 31, 0.92);
    color: #fff;
    font-size: 14px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

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

@media (min-width: 720px) {
    .feed {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        align-items: start;
    }
}
