/* ── Reset & Variables ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --brand:       #3B499E;
    --brand-light: #eef0fa;
    --text:        #111827;
    --muted:       #6b7280;
    --faint:       #9ca3af;
    --bg:          #f5f6fb;
    --surface:     #ffffff;
    --border:      #e5e7eb;
    --danger:      #ef4444;
    --success:     #22c55e;
}

body {
    font-family: "Inter", sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

/* ── Header ────────────────────────────────────────────────────────────────── */
header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 40px;
    height: 56px;
    display: flex;
    align-items: center;
    gap: 14px;
    position: sticky;
    top: 0;
    z-index: 100;
}

header img:not(.user-avatar) { height: 28px; width: auto; display: block; }

.sep { width: 1px; height: 20px; background: var(--border); }

.env-badge {
    font-size: 12px;
    font-weight: 600;
    color: var(--brand);
    background: var(--brand-light);
    padding: 3px 9px;
    border-radius: 99px;
    letter-spacing: .01em;
}

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

.user-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--border);
}

.user-name {
    font-size: 13px;
    font-weight: 500;
    color: var(--muted);
}

/* ── User dropdown ─────────────────────────────────────────────────────────── */
.user-dropdown { position: relative; }

.user-trigger {
    display: flex;
    align-items: center;
    gap: 7px;
    cursor: pointer;
    padding: 4px 8px 4px 4px;
    border-radius: 8px;
    transition: background .15s;
    user-select: none;
}

.user-trigger:hover { background: var(--brand-light); }

.dropdown-arrow {
    color: var(--faint);
    transition: transform .2s;
    flex-shrink: 0;
}

.user-dropdown.open .dropdown-arrow {
    transform: rotate(180deg);
    color: var(--brand);
}

.user-avatar-placeholder {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--brand-light);
    color: var(--brand);
    font-size: 12px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--border);
    flex-shrink: 0;
}

.user-menu {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0,0,0,.1);
    min-width: 140px;
    padding: 4px;
    z-index: 200;
}

.user-dropdown.open .user-menu { display: block; }

.user-menu-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 7px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    text-decoration: none;
    transition: background .12s, color .12s;
    white-space: nowrap;
}

.user-menu-item:hover { background: var(--brand-light); color: var(--brand); }

.user-menu-danger { color: var(--danger); }
.user-menu-danger:hover { background: #fef2f2; color: var(--danger); }

/* ── Page container ────────────────────────────────────────────────────────── */
.page {
    max-width: 1080px;
    margin: 0 auto;
    padding: 48px 24px 80px;
}

/* ── Alerts ────────────────────────────────────────────────────────────────── */
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13.5px;
    margin-bottom: 24px;
}

.alert-error {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #dc2626;
}

.alert-success {
    background: #f0fdf4;
    border: 1px solid #bbf7d0;
    color: #16a34a;
}

/* ── Top bar ───────────────────────────────────────────────────────────────── */
.top {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 16px;
    margin-bottom: 36px;
}

.top-left h1 {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -.02em;
    line-height: 1;
}

.top-left p {
    margin-top: 6px;
    font-size: 13px;
    color: var(--muted);
}

.top-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* ── Breadcrumb ────────────────────────────────────────────────────────────── */
.breadcrumb {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--muted);
    margin-bottom: 8px;
}

.breadcrumb a {
    color: var(--muted);
    text-decoration: none;
    transition: color .15s;
}

.breadcrumb a:hover { color: var(--brand); }

.breadcrumb svg { color: var(--faint); flex-shrink: 0; }

/* ── Search ────────────────────────────────────────────────────────────────── */
.search-wrap { position: relative; }

.search-wrap svg {
    position: absolute;
    left: 11px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--faint);
    pointer-events: none;
}

.search-wrap input {
    font-family: inherit;
    font-size: 13.5px;
    padding: 9px 14px 9px 36px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    width: 220px;
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}

.search-wrap input:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px rgba(59,73,158,.1);
}

.search-wrap input::placeholder { color: var(--faint); }

/* ── Buttons ───────────────────────────────────────────────────────────────── */
.btn-primary {
    display: flex;
    align-items: center;
    gap: 6px;
    font-family: inherit;
    font-size: 13px;
    font-weight: 600;
    color: #fff;
    background: var(--brand);
    border: none;
    border-radius: 8px;
    padding: 9px 16px;
    cursor: pointer;
    text-decoration: none;
    transition: background .15s, box-shadow .15s;
    white-space: nowrap;
}

.btn-primary:hover {
    background: #2e3a84;
    box-shadow: 0 2px 8px rgba(59,73,158,.25);
}

/* ── Group header ──────────────────────────────────────────────────────────── */
.group { margin-bottom: 36px; }

.group-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 14px;
}

.group-letter {
    font-size: 11.5px;
    font-weight: 700;
    color: var(--brand);
    letter-spacing: .08em;
    text-transform: uppercase;
    flex-shrink: 0;
}

.group-count {
    font-size: 11px;
    font-weight: 500;
    color: var(--faint);
    flex-shrink: 0;
}

.group-line {
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* ── Category cards grid ───────────────────────────────────────────────────── */
.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
    gap: 12px;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    position: relative;
    transition: box-shadow .16s, border-color .16s, transform .16s;
}

.card:hover {
    border-color: #c5cbe8;
    box-shadow: 0 4px 20px rgba(59,73,158,.12);
    transform: translateY(-2px);
}

.card > a {
    display: flex;
    flex-direction: column;
    padding: 18px 18px 14px;
    text-decoration: none;
    color: inherit;
    gap: 10px;
}

.card-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
}

.initial-wrap { flex-shrink: 0; }

.initial {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    background: var(--brand-light);
    color: var(--brand);
    font-size: 14px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    text-transform: uppercase;
    user-select: none;
}

.card-arrow {
    color: var(--faint);
    opacity: 0;
    transition: opacity .15s, transform .15s;
    margin-top: 2px;
    flex-shrink: 0;
}

.card:hover .card-arrow { opacity: 1; transform: translate(2px, -2px); }

.card-name {
    font-size: 13.5px;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.card-count {
    font-size: 11.5px;
    color: var(--faint);
}

/* ── Card action buttons (edit/delete) ─────────────────────────────────────── */
.card-actions {
    position: absolute;
    top: 10px;
    right: 10px;
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity .15s;
    pointer-events: none;
}

.card:hover .card-actions {
    opacity: 1;
    pointer-events: auto;
}

.card-action-btn {
    width: 26px;
    height: 26px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color .12s, border-color .12s, background .12s;
}

.card-action-btn:hover {
    color: var(--brand);
    border-color: #c5cbe8;
    background: var(--brand-light);
}

.btn-del-cat:hover {
    color: var(--danger) !important;
    border-color: #fecaca !important;
    background: #fef2f2 !important;
}

/* ── Empty state ───────────────────────────────────────────────────────────── */
#empty {
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 64px 0;
    color: var(--muted);
    text-align: center;
}

#empty svg   { color: #d1d5db; }
#empty p     { font-size: 14px; font-weight: 500; }
#empty small { font-size: 13px; color: var(--faint); }

/* ── Wiki item accordion ───────────────────────────────────────────────────── */
.wiki-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.wiki-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    transition: border-color .15s, box-shadow .15s;
}

.wiki-item:hover {
    border-color: #c5cbe8;
}

.wiki-item.open {
    border-color: #c5cbe8;
    box-shadow: 0 2px 12px rgba(59,73,158,.08);
}

.wiki-item-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    cursor: pointer;
    user-select: none;
    gap: 12px;
}

.wiki-item-title {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    min-width: 0;
}

.wiki-chevron {
    color: var(--faint);
    flex-shrink: 0;
    transition: transform .2s;
}

.wiki-item.open .wiki-chevron {
    transform: rotate(90deg);
    color: var(--brand);
}

.wiki-item-title h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.wiki-item.open .wiki-item-title h3 {
    color: var(--brand);
}

/* ── Item action buttons ───────────────────────────────────────────────────── */
.wiki-item-actions {
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity .15s;
    flex-shrink: 0;
}

.wiki-item:hover .wiki-item-actions {
    opacity: 1;
}

.item-action-btn {
    width: 28px;
    height: 28px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color .12s, border-color .12s, background .12s;
}

.item-action-btn:hover {
    color: var(--brand);
    border-color: #c5cbe8;
    background: var(--brand-light);
}

.btn-del-wiki:hover {
    color: var(--danger) !important;
    border-color: #fecaca !important;
    background: #fef2f2 !important;
}

/* ── Item body (collapsible) ───────────────────────────────────────────────── */
.wiki-item-body {
    display: none;
    padding: 0 20px 20px;
    border-top: 1px solid var(--border);
}

.wiki-item.open .wiki-item-body {
    display: block;
}

.wiki-item-desc {
    font-size: 14px;
    line-height: 1.7;
    color: #374151;
    padding-top: 16px;
    white-space: pre-wrap;
    word-break: break-word;
}

.wiki-item-meta {
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
    font-size: 12px;
    color: var(--faint);
}

.wiki-item-meta strong { color: var(--muted); }

/* ── Wiki HTML content ─────────────────────────────────────────────────────── */
.wiki-item-html { white-space: normal; }

.wiki-item-html p    { margin-bottom: .6em; }
.wiki-item-html h1,.wiki-item-html h2,.wiki-item-html h3,
.wiki-item-html h4   { font-weight: 700; margin: 1em 0 .4em; }
.wiki-item-html ul,.wiki-item-html ol { margin: .4em 0 .6em 1.4em; }
.wiki-item-html li   { margin-bottom: .2em; }
.wiki-item-html blockquote {
    border-left: 3px solid var(--brand);
    padding-left: 12px;
    margin: .6em 0;
    color: var(--muted);
}
.wiki-item-html code {
    background: var(--brand-light);
    color: var(--brand);
    font-size: 12.5px;
    padding: 1px 5px;
    border-radius: 4px;
    font-family: monospace;
}
.wiki-item-html pre {
    background: #1e2130;
    color: #e2e8f0;
    border-radius: 8px;
    padding: 14px 16px;
    font-size: 13px;
    overflow-x: auto;
    margin: .6em 0;
}
.wiki-item-html pre code { background: transparent; color: inherit; padding: 0; }
.wiki-item-html table {
    border-collapse: collapse;
    width: 100%;
    margin: .6em 0;
    font-size: 13.5px;
}
.wiki-item-html th,.wiki-item-html td {
    border: 1px solid var(--border);
    padding: 7px 12px;
    text-align: left;
}
.wiki-item-html th { background: var(--brand-light); font-weight: 600; }
.wiki-item-html hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }

.wiki-link {
    color: var(--brand);
    text-decoration: underline;
    text-decoration-color: transparent;
    transition: text-decoration-color .15s;
    word-break: break-all;
}
.wiki-link:hover { text-decoration-color: var(--brand); }

/* ── Custom Modal ──────────────────────────────────────────────────────────── */
.cw-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(17,24,39,.35);
    z-index: 900;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.cw-overlay.active { display: flex; }

.cw-modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 16px 48px rgba(0,0,0,.14);
    width: 100%;
    max-width: 480px;
    display: flex;
    flex-direction: column;
    max-height: 90vh;
    overflow: hidden;
}

.cw-modal.cw-modal-lg { max-width: 780px; }

.cw-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px 0;
    flex-shrink: 0;
}

.cw-modal-header h2 {
    font-size: 16px;
    font-weight: 700;
    color: var(--text);
    letter-spacing: -.01em;
}

.cw-modal-close {
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    transition: background .12s, color .12s;
    flex-shrink: 0;
}

.cw-modal-close:hover { background: var(--brand-light); color: var(--brand); }

.cw-modal-body {
    padding: 20px 24px;
    overflow-y: auto;
    flex: 1;
}

.cw-modal-footer {
    padding: 0 24px 20px;
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    flex-shrink: 0;
}

/* form fields inside modal */
.cw-field { margin-bottom: 14px; }
.cw-field:last-child { margin-bottom: 0; }

.cw-label {
    display: block;
    font-size: 12.5px;
    font-weight: 600;
    color: var(--muted);
    margin-bottom: 5px;
}

.cw-input, .cw-textarea {
    width: 100%;
    font-family: inherit;
    font-size: 13.5px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 9px 12px;
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}

.cw-input:focus, .cw-textarea:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px rgba(59,73,158,.1);
}

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

.cw-error-msg {
    display: none;
    font-size: 12px;
    color: var(--danger);
    margin-top: 6px;
}

.cw-error-msg.visible { display: block; }

/* confirm delete inside modal */
.cw-confirm-text {
    font-size: 13.5px;
    color: var(--text);
    line-height: 1.6;
}

.cw-confirm-text strong { color: var(--text); }
.cw-confirm-text small  { color: var(--muted); display: block; margin-top: 4px; }

/* buttons */
.btn-ghost {
    display: flex;
    align-items: center;
    gap: 6px;
    font-family: inherit;
    font-size: 13px;
    font-weight: 500;
    color: var(--muted);
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 16px;
    cursor: pointer;
    transition: background .12s, color .12s, border-color .12s;
    white-space: nowrap;
}

.btn-ghost:hover { background: var(--brand-light); color: var(--brand); border-color: #c5cbe8; }

.btn-danger {
    display: flex;
    align-items: center;
    gap: 6px;
    font-family: inherit;
    font-size: 13px;
    font-weight: 600;
    color: #fff;
    background: var(--danger);
    border: none;
    border-radius: 8px;
    padding: 8px 16px;
    cursor: pointer;
    transition: background .12s;
    white-space: nowrap;
}

.btn-danger:hover { background: #dc2626; }

/* CKEditor wrapper */
.ck-editor-wrap { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.ck-editor-wrap .ck.ck-editor__top .ck-sticky-panel .ck-toolbar {
    border: none !important;
    border-bottom: 1px solid var(--border) !important;
    border-radius: 0 !important;
}
.ck-editor-wrap .ck.ck-editor__main .ck-editor__editable {
    border: none !important;
    min-height: 300px;
    font-size: 14px;
    line-height: 1.7;
}

/* ── Responsive ────────────────────────────────────────────────────────────── */
@media (max-width: 640px) {
    header  { padding: 0 20px; }
    .page   { padding: 28px 16px 60px; }
    .top    { flex-direction: column; align-items: flex-start; }
    .top-right { width: 100%; flex-wrap: wrap; }
    .search-wrap input { width: 100%; }
    .search-wrap { flex: 1; }

    .user-name { display: none; }
    header img:not(.user-avatar) { height: 22px; }
}
