:root {
    --bg: #f5f6f3;
    --sidebar-bg: #fbfbf8;
    --surface: #ffffff;
    --surface-muted: #f0f2ed;
    --text: #20252b;
    --text-soft: #3b424a;
    --text-muted: #687078;
    --border: #dde1d8;
    --border-strong: #cfd5ca;
    --accent: #2f5148;
    --accent-hover: #243f38;
    --danger: #a33a32;
    --danger-bg: #fff7f6;
    --danger-hover: #872820;
}

/* Public pages share the application palette and form controls. */
.public-page { display: flex; flex-direction: column; min-height: 100vh; }
.public-header { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; padding: 20px clamp(16px, 5vw, 64px); border-bottom: 1px solid var(--border); background: var(--sidebar-bg); }
.public-header .brand { margin: 0; }
.public-language-switcher { display: flex; gap: 4px; }
.public-main { flex: 1; width: 100%; padding: 40px 16px; }
.public-main .auth-page { width: 100%; max-width: 480px; margin: 0 auto; }
.auth-page .workspace-header { justify-content: center; text-align: center; }
.auth-page .auth-account-link { margin: 24px 0 0; text-align: center; }
.auth-page button:disabled, .auth-page input:disabled { opacity: 0.6; cursor: not-allowed; }

.auth-page .auth-success {
    padding: clamp(24px, 5vw, 40px);
    text-align: center;
}

.auth-success-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    margin: 0 auto 20px;
    border: 1px solid var(--border-strong);
    border-radius: 50%;
    background: var(--surface-muted);
    color: var(--accent);
    font-size: 1.6rem;
    line-height: 1;
}

.auth-success h1 { font-size: clamp(1.5rem, 5vw, 1.75rem); }
.auth-page .auth-success p { margin: 12px 0 24px; color: var(--text-muted); }
.auth-success .button { min-height: 44px; }
.auth-success .button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: Arial, Helvetica, sans-serif;
    font-size: 15px;
    line-height: 1.6;
}

body.modal-open {
    overflow: hidden;
}

.app-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 248px minmax(0, 1fr);
}

.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    padding: 24px 16px;
    border-right: 1px solid var(--border);
    background: var(--sidebar-bg);
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 32px;
    color: var(--text);
    text-decoration: none;
}

.brand-mark {
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    background: var(--surface);
    color: var(--accent);
    font-weight: 700;
}

.brand-name {
    display: block;
    font-size: 1rem;
    font-weight: 700;
    line-height: 1.2;
}

.brand-subtitle {
    display: block;
    color: var(--text-muted);
    font-size: 0.78rem;
    line-height: 1.3;
}

.side-nav {
    display: grid;
    gap: 4px;
}

.side-nav a,
.mobile-nav a {
    padding: 8px 10px;
    border-radius: 6px;
    color: var(--text-soft);
    text-decoration: none;
}

.side-nav a:hover,
.mobile-nav a:hover {
    background: var(--surface-muted);
    color: var(--text);
}

.sidebar .side-nav a {
    padding: 9px 12px;
    font-size: 0.9rem;
    font-weight: 600;
    line-height: 1.5;
}

.sidebar-types {
    margin-top: 24px;
}

.sidebar > .brand { flex-shrink: 0; }
.sidebar-main { flex: 1; min-height: 0; overflow-y: auto; }
.sidebar-footer { flex-shrink: 0; padding-top: 12px; border-top: 1px solid var(--border); }
.sidebar-footer .language-switcher { margin-top: 0; padding-top: 12px; }
.sidebar-footer:has(.account-menu:not([hidden])) .language-switcher { padding-top: 54px; }
.account-area { position: relative; min-width: 0; }
.account-trigger {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--text-soft);
    text-align: left;
    font-weight: 400;
}
.account-trigger:hover, .account-trigger[aria-expanded="true"] { background: var(--surface-muted); color: var(--accent); }
.account-avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--surface-muted);
    color: var(--accent);
    font-weight: 600;
}
.account-text { flex: 1; min-width: 0; }
.account-caption { display: block; color: var(--text-muted); font-size: 0.72rem; }
.account-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.85rem; }
.account-more { flex-shrink: 0; color: var(--text-muted); }
.account-menu {
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    z-index: 35;
    width: 100%;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
}
.account-menu[hidden] { display: none; }
.account-menu button { width: 100%; min-height: 32px; padding: 4px 8px; border: 0; background: transparent; color: var(--text-soft); text-align: left; font-size: 0.85rem; font-weight: 400; }
.account-menu button:hover { background: var(--surface-muted); color: var(--accent); }
.account-trigger:focus-visible, .account-menu button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.sidebar-types-title {
    margin: 0 12px 10px;
    color: var(--text-muted);
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    line-height: 1.5;
}

.sidebar-new-type {
    min-height: 28px;
    margin: 0 12px 10px;
    padding: 2px 4px;
    border: 0;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 400;
}

.sidebar-new-type:hover {
    background: var(--surface-muted);
    color: var(--accent);
}

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

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

.sidebar-type-row {
    display: grid;
    grid-template-columns: 20px minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    border-radius: 6px;
}

.sidebar-type-row + .sidebar-type-row,
.sidebar-record-list + .sidebar-type-row {
    margin-top: 4px;
}

.sidebar-type-row:has(.sidebar-type-actions) {
    grid-template-columns: 20px minmax(0, 1fr) auto 24px;
}

.sidebar-type-more {
    width: 24px;
    min-height: 26px;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--text-muted);
}

.sidebar-type-more:hover { background: var(--surface-muted); color: var(--accent); }
.sidebar-type-more:focus-visible { outline: 2px solid var(--accent); }
.sidebar-type-menu {
    position: fixed;
    z-index: 30;
    width: 120px;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
}
.sidebar-type-menu[hidden] { display: none; }
.sidebar-type-menu button {
    display: block;
    width: 100%;
    min-height: 28px;
    padding: 4px 8px;
    border: 0;
    background: transparent;
    color: var(--text-soft);
    text-align: left;
    font-size: 0.78rem;
    font-weight: 400;
}
.sidebar-type-menu button:hover { background: var(--surface-muted); }
.sidebar-type-menu .sidebar-type-delete { color: var(--danger); }

.sidebar-type-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;

    width: 24px;
    height: 26px;
    min-height: 0;

    padding: 0;
    border: 0;
    border-radius: 4px;

    appearance: none;
    background: transparent;
    color: var(--text-muted);

    font: inherit;
    font-size: 0.9rem;
    line-height: 1;

    cursor: pointer;
}

.sidebar-type-toggle:hover {
    background: rgba(47, 81, 72, 0.04);
    color: var(--accent);
}

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

.sidebar-type-link {
    min-width: 0;
    padding: 9px 0;
    color: var(--text-soft);
    font-size: 0.875rem;
    line-height: 1.5;
    text-decoration: none;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-type-count {
    color: var(--text-muted);
    font-size: 0.75rem;
    font-variant-numeric: tabular-nums;
    line-height: 1.5;
    text-align: right;
    white-space: nowrap;
}

.sidebar-type-row.is-active {
    background: rgba(47, 81, 72, 0.10);
    border-radius: 8px;
}

.sidebar-type-row.is-active .sidebar-type-link {
    font-weight: 600;
}

.sidebar-type-ungrouped .sidebar-type-link {
    color: var(--text-muted);
}

.sidebar-type-row .sidebar-type-link:hover,
.sidebar-type-row .sidebar-type-link:focus-visible {
    color: var(--accent);
}

.sidebar .side-nav a:focus-visible,
.sidebar-type-link:focus-visible {
    border-radius: 4px;
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.sidebar-record-list {
    min-width: 0;
    margin: 0;
    padding: 0 12px 0 40px;
    list-style: none;
}

.sidebar-record-list[hidden] {
    display: none;
}

.sidebar-record-link {
    display: block;
    min-width: 0;
    padding: 6px 8px;
    overflow: hidden;
    border-radius: 4px;
    color: var(--text-muted);
    font-size: 0.8125rem;
    line-height: 1.5;
    text-decoration: none;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-record-link:hover,
.sidebar-record-link:focus-visible {
    background: var(--surface-muted);
    color: var(--accent);
}

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

.sidebar-record-link.is-current-record {
    background: rgba(47, 81, 72, 0.04);
    color: var(--text-soft);
    font-weight: 600;
    box-shadow: inset 2px 0 0 rgba(47, 81, 72, 0.45);
}

.mobile-header {
    display: none;
}

.language-switcher,
.mobile-language-switcher {
    display: flex;
    gap: 4px;
}

.language-switcher {
    margin-top: auto;
    padding-top: 24px;
}

.language-option {
    min-height: 30px;
    padding: 5px 9px;
    border-color: var(--border);
    background: transparent;
    color: var(--text-muted);
    font-size: 0.82rem;
    font-weight: 600;
}

.language-option:hover,
.language-option.is-active {
    border-color: var(--border-strong);
    background: var(--surface-muted);
    color: var(--text);
}

.page-shell {
    width: min(100% - 64px, 1120px);
    margin: 0 auto;
    padding: 48px 0 64px;
}

h1,
h2,
h3 {
    margin: 0;
    color: var(--text);
    line-height: 1.2;
}

h1 {
    font-size: 2rem;
    font-weight: 700;
}

h2 {
    font-size: 1rem;
    font-weight: 700;
}

p {
    margin: 0 0 16px;
}

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

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

form {
    margin: 0;
}

input,
textarea,
select,
.modal-form select[name="type_id"],
.form-panel select[name="type_id"] {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
}

.modal-form select[name="type_id"],
.form-panel select[name="type_id"] {
    display: block;
    min-width: 0;
    height: calc(1.6em + 22px);
    line-height: 1.6;
}

textarea {
    min-height: 120px;
    resize: vertical;
}

input:focus,
textarea:focus,
select:focus,
.modal-form select[name="type_id"]:focus,
.form-panel select[name="type_id"]:focus {
    border-color: var(--accent);
    outline: 3px solid rgba(47, 81, 72, 0.12);
}

button,
.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 38px;
    padding: 8px 14px;
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: var(--accent);
    color: #ffffff;
    font: inherit;
    font-weight: 600;
    line-height: 1.2;
    text-decoration: none;
    cursor: pointer;
}

button:hover,
.button:hover {
    border-color: var(--accent-hover);
    background: var(--accent-hover);
    color: #ffffff;
}

.button-secondary {
    border-color: var(--border-strong);
    background: var(--surface);
    color: var(--text-soft);
}

.button-secondary:hover {
    border-color: var(--border-strong);
    background: var(--surface-muted);
    color: var(--text);
}

.button-danger {
    border-color: #e5b8b3;
    background: var(--danger-bg);
    color: var(--danger);
}

.button-danger:hover {
    border-color: var(--danger-hover);
    background: var(--danger-hover);
    color: #ffffff;
}

.icon-button {
    width: 32px;
    min-height: 32px;
    padding: 0;
    border-color: var(--border);
    background: var(--surface);
    color: var(--text-muted);
    font-size: 1.35rem;
    font-weight: 400;
}

.icon-button:hover {
    border-color: var(--border-strong);
    background: var(--surface-muted);
    color: var(--text);
}

.workspace-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 24px;
    margin-bottom: 24px;
}

.workspace-header p {
    max-width: 560px;
    margin: 8px 0 0;
    color: var(--text-muted);
}

.dashboard-panel,
.form-panel,
.empty-state {
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
}

.dashboard-panel {
    padding: 24px;
}

.search-form {
    display: flex;
    gap: 8px;
    align-items: stretch;
    max-width: 760px;
}

.search-form input {
    min-width: 0;
}

.action-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 16px;
}

.content-section {
    margin-top: 32px;
}

.section-heading {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 12px;
}

.section-heading p {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.library-header {
    margin-bottom: 16px;
}

.library-search {
    margin-bottom: 16px;
}

.form-panel {
    max-width: 760px;
    padding: 24px;
}

.form-panel p {
    margin: 0 0 12px;
}

.form-actions,
.reader-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 12px;
}

.record-card {
    min-width: 0;
    min-height: 184px;
    display: flex;
    flex-direction: column;
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.78);
}

.card-title,
.item-title {
    color: var(--text);
    font-weight: 700;
    line-height: 1.3;
    text-decoration: none;
}

.card-title {
    display: -webkit-box;
    margin-bottom: 8px;
    overflow: hidden;
    font-size: 1rem;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}

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

.card-summary,
.item-summary {
    display: -webkit-box;
    overflow: hidden;
    color: var(--text-muted);
    -webkit-box-orient: vertical;
}

.type-badge {
    display: inline-block;
    max-width: 100%;
    padding: 2px 8px;
    border: 1px solid #d5e3db;
    border-radius: 6px;
    background: #eef5f0;
    color: var(--accent);
    font-size: 0.78rem;
    font-weight: 500;
    line-height: 1.5;
    overflow-wrap: anywhere;
    vertical-align: middle;
}

.type-badge[data-en="Ungrouped"] {
    background: #f1f3f0;
    color: #59685f;
    border-color: #dce2dc;
}

.card-type {
    margin: 0 0 10px;
}

.recent-type {
    margin-top: 6px;
}

.reader-type {
    margin-bottom: 16px;
}

.card-summary {
    margin-bottom: 12px;
    -webkit-line-clamp: 3;
}

.card-source {
    margin: auto 0 10px;
    overflow-wrap: anywhere;
    color: var(--text-muted);
    font-size: 0.86rem;
}

.card-source a {
    color: var(--text-muted);
    text-decoration: none;
}

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

.card-meta,
.item-meta {
    color: var(--text-muted);
    font-size: 0.84rem;
}

.card-meta {
    margin: auto 0 0;
}

.recent-list {
    display: grid;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
}

.recent-item {
    min-width: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 16px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}

.recent-item:last-child {
    border-bottom: 0;
}

.item-summary {
    margin: 4px 0 0;
    -webkit-line-clamp: 2;
}

.item-meta {
    white-space: nowrap;
}

.empty-state {
    padding: 24px;
}

.empty-state h2 {
    margin-bottom: 8px;
}

.empty-state p {
    color: var(--text-muted);
}

.reader-page,
.editor-page {
    max-width: 760px;
}

.back-link {
    margin-bottom: 24px;
}

.back-link a {
    color: var(--text-muted);
    text-decoration: none;
}

.back-link a:hover {
    color: var(--accent);
}

.reader-header {
    margin-bottom: 32px;
}

.reader-header h1 {
    max-width: 720px;
    margin-bottom: 16px;
    font-size: 2.1rem;
}

.record-meta-panel {
    display: grid;
    gap: 8px;
    padding: 14px 0;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
}

.record-meta-panel p {
    display: grid;
    grid-template-columns: 80px minmax(0, 1fr);
    gap: 12px;
    margin: 0;
    color: var(--text-muted);
    overflow-wrap: anywhere;
}

.record-meta-panel span {
    color: var(--text-soft);
    font-weight: 700;
}

.reader-content {
    max-width: 720px;
    margin-bottom: 32px;
    color: var(--text-soft);
    font-size: 1rem;
    line-height: 1.75;
    white-space: pre-wrap;
}

.reader-content p {
    margin-bottom: 0;
}

.reader-actions {
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.reader-actions form {
    display: inline-flex;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: rgba(32, 37, 43, 0.34);
}

.modal-backdrop.is-open {
    display: flex;
}

.modal-dialog {
    width: min(100%, 560px);
    max-height: min(720px, calc(100vh - 48px));
    overflow: auto;
    padding: 20px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    background: var(--surface);
}

.modal-dialog-small {
    width: min(100%, 440px);
}

.modal-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 16px;
}

.modal-header h2 {
    margin-bottom: 6px;
}

.modal-header p {
    margin: 0;
    color: var(--text-muted);
}

.modal-form p {
    margin: 0 0 12px;
}

.modal-form label,
.form-panel label {
    display: block;
    margin-bottom: 6px;
    color: var(--text-soft);
    font-size: 0.9rem;
    font-weight: 700;
}

.modal-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 16px;
}

@media (min-width: 1280px) {
    .card-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

@media (max-width: 860px) {
    .app-shell {
        display: block;
    }

    .sidebar {
        display: none;
    }

    .mobile-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-wrap: wrap;
        gap: 16px;
        padding: 12px 16px;
        border-bottom: 1px solid var(--border);
        background: var(--sidebar-bg);
    }

    .mobile-brand {
        color: var(--text);
        font-weight: 700;
        text-decoration: none;
    }

    .mobile-header .account-area { flex: 0 1 220px; max-width: 100%; margin-left: auto; }

    .mobile-nav {
        display: flex;
        gap: 4px;
    }

    .page-shell {
        width: min(100% - 32px, 1120px);
        padding: 32px 0 48px;
    }

    .workspace-header,
    .section-heading {
        flex-direction: column;
        gap: 8px;
    }

    .recent-item {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .item-meta {
        white-space: normal;
    }
}

@media (max-width: 560px) {
    body {
        font-size: 14px;
    }

    h1,
    .reader-header h1 {
        font-size: 1.65rem;
    }

    .dashboard-panel,
    .form-panel,
    .empty-state {
        padding: 16px;
    }

    .search-form,
    .form-actions,
    .reader-actions,
    .modal-actions {
        align-items: stretch;
        flex-direction: column;
    }

    button,
    .button {
        width: 100%;
    }

    .icon-button {
        width: 32px;
        align-self: flex-start;
    }

    .language-option {
        width: auto;
    }

    .record-meta-panel p {
        grid-template-columns: 1fr;
        gap: 2px;
    }
}

/* Sidebar display filters are independent of the saved collapse state. */
.sidebar-type-filters { margin: 0 12px 10px; }
.sidebar-type-filters input[type="search"] { padding: 6px 8px; font-size: 0.8rem; }
.sidebar-hide-empty { display: flex; align-items: center; gap: 6px; margin-top: 8px; color: var(--text-muted); font-size: 0.75rem; cursor: pointer; }
.sidebar-hide-empty input { width: 14px; height: 14px; margin: 0; padding: 0; accent-color: var(--accent); }
.sidebar-type-row.is-type-filtered,
.sidebar-record-list.is-type-filtered { display: none; }

.reader-page .reader-back { width: auto; min-height: 32px; gap: 6px; padding: 5px 10px; font-size: 0.82rem; font-weight: 400; }
.reader-page .reader-header { margin-bottom: 20px; }
.reader-page .reader-header h1 { line-height: 1.25; overflow-wrap: anywhere; margin-bottom: 12px; }
.reader-page .reader-type { margin-bottom: 20px; }
.reader-page .record-meta-panel { padding: 14px 16px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-muted); font-size: 0.85rem; }
.reader-page .record-meta-panel a,
.reader-page .record-meta-panel time { min-width: 0; overflow-wrap: anywhere; }
.reader-page .reader-content { max-width: none; margin-bottom: 20px; padding: clamp(16px, 3vw, 24px); border: 1px solid var(--border); border-radius: 8px; background: var(--surface); white-space: normal; overflow-wrap: anywhere; }
.reader-page .reader-content p { white-space: pre-wrap; }
.reader-page .reader-actions { padding-top: 20px; }
