/* ============== Tokens: dark default, light override ============== */
:root[data-theme="dark"] {
    --bg:        #0f1115;
    --bg-2:      #161a22;
    --bg-3:      #1d222c;
    --border:    #262c38;
    --text:      #e6e8ec;
    --text-dim:  #9aa3b2;
    --muted:     #6b7280;
    --primary:   #6366f1;
    --primary-2: #818cf8;
    --danger:    #ef4444;
    --success:   #10b981;
    --shadow:    0 6px 20px rgba(0,0,0,0.35);
}
:root[data-theme="light"] {
    --bg:        #f6f7fb;
    --bg-2:      #ffffff;
    --bg-3:      #eef0f6;
    --border:    #dbe0ea;
    --text:      #111827;
    --text-dim:  #4b5563;
    --muted:     #6b7280;
    --primary:   #4f46e5;
    --primary-2: #6366f1;
    --danger:    #dc2626;
    --success:   #059669;
    --shadow:    0 4px 14px rgba(15,20,40,0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14.5px;
    line-height: 1.5;
}
a { color: var(--primary-2); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--text-dim); }
.tiny  { font-size: 12px; }
input, textarea, select, button {
    font-family: inherit;
    font-size: inherit;
    color: var(--text);
}
input, textarea, select {
    background: var(--bg-3);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    width: 100%;
    outline: none;
    transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99,102,241,0.18);
}
label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--text-dim); }
label > input, label > textarea, label > select { margin-top: 4px; }

button {
    cursor: pointer;
    border: 1px solid var(--border);
    background: var(--bg-3);
    border-radius: 8px;
    padding: 9px 14px;
    color: var(--text);
    transition: background .15s, border-color .15s, transform .05s;
}
button:hover { background: var(--bg-2); }
button:active { transform: translateY(1px); }
button.primary {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}
button.primary:hover { background: var(--primary-2); border-color: var(--primary-2); }
button.ghost { background: transparent; }
button.danger { color: var(--danger); }

.icon-btn {
    padding: 6px 9px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 8px;
    line-height: 1;
    cursor: pointer;
}
.icon-btn:hover { background: var(--bg-3); border-color: var(--border); }

.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; }
.alert.error { background: rgba(239,68,68,0.1); border: 1px solid var(--danger); color: var(--danger); }
.alert.success { background: rgba(16,185,129,0.1); border: 1px solid var(--success); color: var(--success); }

/* ============== Login/register centered card ============== */
body.centered {
    display: flex; align-items: center; justify-content: center;
    min-height: 100vh; padding: 20px;
}
.card {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 28px;
    width: 100%;
    max-width: 420px;
    box-shadow: var(--shadow);
    position: relative;
}
.brand { font-size: 22px; font-weight: 600; margin: 0 0 4px; }
.theme-toggle { position: absolute; top: 10px; right: 10px; }

/* ============== App layout ============== */
.app {
    display: grid;
    grid-template-columns: 280px 1fr;
    height: 100vh;
}
.sidebar {
    background: var(--bg-2);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}
.sidebar-section { padding: 12px 10px; flex: 1; overflow-y: auto; }
.section-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 4px 8px 8px;
    color: var(--text-dim);
    text-transform: uppercase;
    font-size: 11px;
    letter-spacing: 0.5px;
}
.category-list { list-style: none; padding: 0; margin: 0; }
.category-list li {
    padding: 8px 10px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-dim);
    margin-bottom: 2px;
}
.category-list li:hover { background: var(--bg-3); color: var(--text); }
.category-list li.active { background: var(--primary); color: #fff; }
.category-list li .cat-count {
    font-size: 11px;
    background: rgba(255,255,255,0.15);
    padding: 1px 7px;
    border-radius: 10px;
}
.category-list li:not(.active) .cat-count {
    background: var(--bg-3);
}
.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 10px;
}
.user-chip {
    display: flex; align-items: center; justify-content: space-between;
    padding: 8px; gap: 8px;
}
.user-name { font-weight: 600; font-size: 13px; }
.user-role { font-size: 11px; color: var(--text-dim); text-transform: uppercase; }
.user-actions { display: flex; gap: 4px; }

/* ============== Chat area ============== */
.chat { display: flex; flex-direction: column; min-width: 0; }
.chat-header {
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-2);
}
.chat-header h2 { margin: 0; font-size: 17px; }
.chat-header-meta { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.empty {
    margin: auto;
    color: var(--text-dim);
    font-style: italic;
    text-align: center;
}

.msg {
    display: flex;
    gap: 10px;
    padding: 6px 8px;
    border-radius: 8px;
    position: relative;
}
.msg:hover { background: var(--bg-2); }
.msg:hover .msg-actions { display: flex; }
.msg-avatar {
    width: 32px; height: 32px; border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600;
    flex-shrink: 0;
}
.msg-main { min-width: 0; flex: 1; }
.msg-head {
    display: flex; gap: 8px; align-items: baseline;
}
.msg-author { font-weight: 600; }
.msg-time { color: var(--text-dim); font-size: 11px; }
.msg-body {
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    line-height: 1.45;
}
.msg-body.deleted { color: var(--muted); font-style: italic; }
.msg-actions {
    display: none;
    position: absolute;
    top: 4px; right: 8px;
    background: var(--bg-3);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 2px;
}
.msg-attachments {
    display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px;
}
.att {
    background: var(--bg-3);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 12px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    max-width: 280px;
}
.att a { color: var(--text); }
.att-img {
    max-width: 320px;
    max-height: 260px;
    border-radius: 10px;
    cursor: zoom-in;
    display: block;
}

/* ============== Composer ============== */
.composer {
    padding: 10px 16px 16px;
    border-top: 1px solid var(--border);
    background: var(--bg-2);
    position: relative;
}
.composer-row {
    display: flex;
    align-items: end;
    gap: 8px;
}
.composer textarea {
    resize: none;
    max-height: 180px;
    padding: 10px 12px;
    border-radius: 10px;
}
.send-btn { white-space: nowrap; }
.file-btn { font-size: 18px; padding: 8px 10px; }
.composer-attachments {
    display: flex; flex-wrap: wrap; gap: 6px;
    margin-bottom: 8px;
}
.pending-file {
    background: var(--bg-3);
    border: 1px solid var(--border);
    padding: 4px 8px;
    border-radius: 6px;
    font-size: 12px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.pending-file button {
    padding: 0 4px; background: transparent; border: 0; color: var(--danger);
    font-size: 14px;
}

/* ============== Emoji picker ============== */
.emoji-picker {
    position: absolute;
    bottom: 70px; left: 16px;
    background: var(--bg-3);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px;
    width: 300px;
    max-height: 260px;
    overflow-y: auto;
    box-shadow: var(--shadow);
    z-index: 10;
}
.emoji-picker button {
    background: transparent;
    border: 0;
    font-size: 20px;
    padding: 4px;
    cursor: pointer;
    border-radius: 6px;
}
.emoji-picker button:hover { background: var(--bg-2); }

/* ============== Modal ============== */
.modal {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
    padding: 20px;
}
.modal[hidden] { display: none !important; }
.emoji-picker[hidden] { display: none !important; }
.modal-card {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 22px;
    width: 100%;
    max-width: 420px;
    box-shadow: var(--shadow);
}
.modal-card h3 { margin-top: 0; }
.modal-actions {
    display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px;
}
.toggle-row { display: flex; align-items: center; gap: 8px; }
.toggle-row input { width: auto; }

/* ============== Admin ============== */
body.admin-body { display: block; min-height: 100vh; }
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-2);
}
.topbar nav { display: flex; align-items: center; gap: 12px; }
.admin-grid {
    padding: 20px;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
    gap: 20px;
    max-width: 1600px;
    margin: 0 auto;
}
.admin-grid .card { max-width: none; }
.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 12px;
    font-size: 13px;
}
.data-table th, .data-table td {
    padding: 8px 10px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.data-table th { color: var(--text-dim); font-weight: 500; font-size: 11px; text-transform: uppercase; }
.row-form { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; }
.row-form input { flex: 1; min-width: 140px; }
.accordion {
    background: var(--bg-3);
    border-radius: 10px;
    padding: 10px 14px;
    margin-bottom: 10px;
}
.accordion summary { cursor: pointer; font-weight: 500; padding: 4px 0; }
.accordion[open] summary { margin-bottom: 10px; }
.permanent-link {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px;
    margin-top: 10px;
    font-family: ui-monospace, monospace;
    font-size: 12px;
    word-break: break-all;
}
.copy-link {
    margin-left: 8px; font-size: 11px; padding: 3px 8px;
}

/* Image lightbox */
.lightbox {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.85);
    display: flex; align-items: center; justify-content: center;
    z-index: 1000; cursor: zoom-out;
}
.lightbox img { max-width: 95vw; max-height: 95vh; }

/* Subkategorier + system-kategori */
.category-list li.nested {
    padding-left: 24px;
    font-size: 13px;
    opacity: 0.9;
}
.category-list li.system {
    background: linear-gradient(90deg, rgba(255,200,80,0.10), transparent);
    border-left: 3px solid var(--primary);
}
.category-list li.system.active {
    background: var(--primary);
}
.category-list li .cat-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Velkomst-visning */
.welcome-card {
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    margin: 0 auto 20px;
    max-width: 720px;
}
.welcome-msg {
    white-space: pre-wrap;
    font-family: inherit;
    font-size: 15px;
    line-height: 1.55;
    color: var(--text);
    margin: 0 0 12px;
}

.member-list {
    max-width: 960px;
    margin: 0 auto;
}
.member-list h3 {
    margin: 8px 0 16px;
    color: var(--text-dim);
    font-weight: 500;
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}
.member-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 10px;
}
.member {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
}
.member-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 13px;
    flex: 0 0 auto;
}
.member-info { min-width: 0; }
.member-name {
    font-weight: 500;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 6px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.member-since { margin-top: 2px; }

.role-badge {
    display: inline-block;
    font-size: 10px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    padding: 1px 6px;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    font-weight: 600;
}

/* Mobile */
@media (max-width: 720px) {
    .app { grid-template-columns: 1fr; }
    .sidebar { height: auto; max-height: 50vh; }
    .admin-grid { grid-template-columns: 1fr; }
}

