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

:root {
  --accent: #6366f1;
  --accent-dark: #4f46e5;
  --accent-light: #e0e7ff;
  --bg: #f8fafc;
  --surface: #ffffff;
  --sidebar-bg: #1e1b4b;
  --sidebar-text: #c7d2fe;
  --sidebar-hover: #312e81;
  --sidebar-active: #6366f1;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --green: #10b981;
  --green-light: #d1fae5;
  --red: #ef4444;
  --red-light: #fee2e2;
  --yellow: #f59e0b;
  --yellow-light: #fef3c7;
  --gray-light: #f1f5f9;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
  --shadow-md: 0 4px 16px rgba(0,0,0,.10);
}

body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; }

/* Layout */
.layout { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar { width: 220px; background: var(--sidebar-bg); display: flex; flex-direction: column; flex-shrink: 0; }
.sidebar-logo { display: flex; align-items: center; gap: 10px; padding: 24px 20px 20px; color: #fff; font-size: 16px; font-weight: 700; letter-spacing: -.3px; }
.sidebar-logo svg { color: var(--accent); }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; padding: 8px 12px; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; color: var(--sidebar-text); text-decoration: none; font-weight: 500; transition: background .15s, color .15s; }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--sidebar-active); color: #fff; }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 20px 28px; background: var(--surface); border-bottom: 1px solid var(--border); }
.page-title { font-size: 20px; font-weight: 700; color: var(--text); }
.content { padding: 28px; flex: 1; }

/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px; border-radius: 8px; font-size: 13px; font-weight: 600; border: none; cursor: pointer; transition: background .15s, opacity .15s; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-secondary { background: var(--gray-light); color: var(--text); }
.btn-secondary:hover { background: var(--border); }
.btn-success { background: var(--green); color: #fff; }
.btn-success:hover { opacity: .85; }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover { opacity: .85; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn svg { width: 14px; height: 14px; }

/* Cards */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; margin-bottom: 28px; }
.card { background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); border: 1px solid var(--border); }
.card-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); margin-bottom: 8px; }
.card-value { font-size: 28px; font-weight: 800; color: var(--text); }
.card-sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.card-icon { width: 36px; height: 36px; border-radius: 8px; display: flex; align-items: center; justify-content: center; margin-bottom: 12px; }
.card-icon svg { width: 18px; height: 18px; }
.card-icon.green { background: var(--green-light); color: var(--green); }
.card-icon.indigo { background: var(--accent-light); color: var(--accent); }
.card-icon.yellow { background: var(--yellow-light); color: var(--yellow); }
.card-icon.red { background: var(--red-light); color: var(--red); }

/* Table */
.table-wrap { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--border); overflow: hidden; }
.table-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.table-header h2 { font-size: 15px; font-weight: 700; }
table { width: 100%; border-collapse: collapse; }
th { padding: 10px 16px; text-align: left; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); background: var(--gray-light); border-bottom: 1px solid var(--border); }
td { padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #fafbfc; }
tr.row-selected td { background: #eff6ff; }
tr.row-selected:hover td { background: #dbeafe; }
.actions { display: flex; gap: 6px; }

/* Badge */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: 99px; font-size: 11px; font-weight: 600; }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-paid { background: var(--green-light); color: var(--green); }
.badge-unpaid { background: var(--yellow-light); color: var(--yellow); }
.badge-cancelled { background: var(--gray-light); color: var(--text-muted); }

/* Modal */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 100; align-items: center; justify-content: center; }
.modal-overlay.open { display: flex; }
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 500px; box-shadow: var(--shadow-md); animation: slideUp .2s ease; }
@keyframes slideUp { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 24px 16px; border-bottom: 1px solid var(--border); }
.modal-header h2 { font-size: 16px; font-weight: 700; }
.modal-close { background: none; border: none; font-size: 22px; color: var(--text-muted); cursor: pointer; line-height: 1; }
.modal-close:hover { color: var(--text); }
.modal-body { padding: 20px 24px 24px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* Form */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 12px; font-weight: 600; color: var(--text-muted); margin-bottom: 5px; }
.form-group input, .form-group select, .form-group textarea { width: 100%; padding: 8px 11px; border: 1px solid var(--border); border-radius: 7px; font-size: 13px; font-family: inherit; background: var(--surface); color: var(--text); transition: border .15s; }
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(99,102,241,.15); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* Invoice lines */
.lines-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.line-row { display: grid; grid-template-columns: 1fr 70px 90px 28px; gap: 6px; align-items: center; }
.line-row input { padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; font-family: inherit; }
.line-row input:focus { outline: none; border-color: var(--accent); }
.remove-line { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 18px; display: flex; align-items: center; justify-content: center; }
.remove-line:hover { color: var(--red); }
.add-line-btn { background: none; border: 1px dashed var(--border); border-radius: 7px; color: var(--text-muted); font-size: 12px; font-weight: 600; padding: 7px; cursor: pointer; width: 100%; text-align: center; }
.add-line-btn:hover { border-color: var(--accent); color: var(--accent); }

/* Invoice detail */
.detail-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--border); padding: 24px; }
.detail-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 24px; }
.detail-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 24px; }
.meta-item label { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); display: block; margin-bottom: 3px; }
.meta-item span { font-size: 14px; font-weight: 500; }
.invoice-lines-table { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
.invoice-lines-table th { padding: 8px 12px; font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); background: var(--gray-light); border-bottom: 1px solid var(--border); text-align: left; }
.invoice-lines-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 13px; }
.invoice-lines-table tr:last-child td { border-bottom: none; }
.invoice-total { text-align: right; font-size: 18px; font-weight: 800; color: var(--accent); padding-top: 12px; border-top: 2px solid var(--border); }
.back-btn { display: inline-flex; align-items: center; gap: 6px; color: var(--text-muted); font-size: 13px; font-weight: 600; cursor: pointer; margin-bottom: 20px; }
.back-btn:hover { color: var(--text); }
.detail-actions { display: flex; gap: 8px; }

/* Nav divider */
.nav-divider { height: 1px; background: rgba(255,255,255,.08); margin: 10px 12px; }

/* Nav section header */
.nav-section { padding: 16px 12px 5px; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: rgba(255,255,255,.3); user-select: none; }

/* Settings page */
.settings-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--border); padding: 28px; }
.settings-card h2 { font-size: 15px; font-weight: 700; margin-bottom: 20px; padding-bottom: 14px; border-bottom: 1px solid var(--border); }
.settings-hint { font-size: 12px; color: var(--text-muted); margin-top: 16px; padding: 10px 12px; background: var(--gray-light); border-radius: 7px; }

/* Connection status pill */
.status-pill { display: inline-flex; align-items: center; gap: 5px; padding: 4px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; }
.status-pill::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-ok { background: var(--green-light); color: var(--green); }
.status-err { background: var(--red-light); color: var(--red); }
.status-warn { background: var(--yellow-light); color: var(--yellow); }

/* Data table extras */
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar input[type=text] { padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 13px; font-family: inherit; flex: 1; min-width: 200px; }
.toolbar input[type=text]:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(99,102,241,.15); }
.loading { text-align: center; padding: 60px 20px; color: var(--text-muted); font-size: 14px; }
.loading-spinner { width: 32px; height: 32px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; margin: 0 auto 12px; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { background: var(--red-light); color: var(--red); border-radius: var(--radius); padding: 16px 20px; font-size: 13px; font-weight: 500; }
.cell-mono { font-family: 'SF Mono', 'Consolas', monospace; font-size: 12px; }

/* Empty state */
.empty { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty svg { width: 48px; height: 48px; margin: 0 auto 12px; opacity: .3; }
.empty p { font-size: 15px; font-weight: 500; }

/* Toast */
.toast { position: fixed; bottom: 24px; right: 24px; background: #1e293b; color: #fff; padding: 12px 18px; border-radius: 9px; font-size: 13px; font-weight: 500; box-shadow: var(--shadow-md); z-index: 200; animation: slideUp .2s ease; }

/* Login screen */
.login-overlay { display: flex; position: fixed; inset: 0; background: #0f172a; z-index: 999; align-items: center; justify-content: center; }
.login-box { background: var(--surface); border-radius: 16px; width: 100%; max-width: 400px; padding: 40px 36px; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.login-logo { display: flex; align-items: center; gap: 10px; margin-bottom: 24px; color: var(--accent); font-size: 20px; font-weight: 800; }
.login-logo svg { color: var(--accent); }
.login-title { font-size: 18px; font-weight: 700; color: var(--text); margin-bottom: 20px; }
.login-error { background: var(--red-light); color: var(--red); border-radius: 8px; padding: 10px 14px; font-size: 13px; font-weight: 500; margin-bottom: 14px; }

/* Sidebar user */
.sidebar { display: flex; flex-direction: column; }
.sidebar-nav { flex: 1; }
.sidebar-user { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-top: 1px solid rgba(255,255,255,.08); margin-top: auto; }
.sidebar-user-info { flex: 1; min-width: 0; }
.sidebar-user-name { display: block; font-size: 13px; font-weight: 600; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-user-role { display: block; font-size: 11px; color: var(--sidebar-text); margin-top: 1px; }
.sidebar-logout { background: none; border: none; cursor: pointer; color: var(--sidebar-text); display: flex; align-items: center; justify-content: center; padding: 6px; border-radius: 6px; transition: background .15s, color .15s; flex-shrink: 0; }
.sidebar-logout:hover { background: rgba(255,255,255,.1); color: #fff; }

/* Hamburger button — hidden on desktop */
.hamburger { display: none; background: none; border: none; cursor: pointer; padding: 6px; color: var(--text); border-radius: 8px; transition: background .15s; }
.hamburger:hover { background: var(--gray-light); }

/* Mobile overlay */
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 49; }
.sidebar-overlay.open { display: block; }

/* ── Responsive ────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  /* Sidebar becomes a slide-in drawer */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    z-index: 50; width: 260px;
    transform: translateX(-100%);
    transition: transform .25s ease;
  }
  .sidebar.open { transform: translateX(0); }

  .hamburger { display: flex; align-items: center; justify-content: center; }

  .topbar { padding: 14px 16px; gap: 10px; }
  .page-title { font-size: 16px; }

  .content { padding: 16px; }

  /* Stack form rows */
  .form-row { grid-template-columns: 1fr; }

  /* Make cards smaller */
  .cards { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin-bottom: 16px; }
  .card { padding: 14px; }
  .card-value { font-size: 22px; }

  /* Tables: horizontal scroll */
  .table-wrap { overflow-x: auto; }
  table { min-width: 500px; }

  /* Settings cards */
  .settings-card { padding: 16px; }
  .settings-card h2 { font-size: 14px; }

  /* Modal */
  .modal { max-width: calc(100vw - 32px); margin: 16px; }

  /* Toolbar */
  .toolbar { gap: 8px; }
  .toolbar input[type=text] { min-width: 0; }

  /* Hide some topbar elements on very small screens */
  #connectionStatus { display: none; }
}

@media (max-width: 480px) {
  .cards { grid-template-columns: 1fr 1fr; }
  .topbar { padding: 12px; }
  .content { padding: 12px; }
  .btn { padding: 7px 12px; font-size: 12px; }
}

/* ── Kunnskapsdatabase ──────────────────────────────────────────────────── */

/* Sub-nav item (slightly indented for KB categories) */
.nav-item-sub { padding-left: 24px; font-size: 13px; }

/* Article card wrapper */
.kb-article-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--border); padding: 32px 36px; }
.kb-article-title { font-size: 26px; font-weight: 800; color: var(--text); margin-bottom: 24px; padding-bottom: 16px; border-bottom: 1px solid var(--border); }

/* Article content rendering */
.kb-article-content { font-size: 15px; line-height: 1.75; color: var(--text); }
.kb-article-content h1 { font-size: 24px; font-weight: 800; margin: 28px 0 12px; color: var(--text); }
.kb-article-content h2 { font-size: 20px; font-weight: 700; margin: 24px 0 10px; color: var(--text); }
.kb-article-content h3 { font-size: 17px; font-weight: 700; margin: 20px 0 8px; color: var(--text); }
.kb-article-content h4 { font-size: 15px; font-weight: 700; margin: 16px 0 6px; color: var(--text); }
.kb-article-content p { margin: 0 0 14px; }
.kb-article-content ul, .kb-article-content ol { margin: 0 0 14px 22px; }
.kb-article-content li { margin-bottom: 4px; }
.kb-article-content blockquote { border-left: 4px solid var(--accent); padding: 10px 16px; margin: 16px 0; background: var(--accent-light); border-radius: 0 6px 6px 0; color: var(--text); }
.kb-article-content pre { background: #1e293b; color: #e2e8f0; padding: 16px; border-radius: 8px; font-family: 'SF Mono', 'Consolas', monospace; font-size: 13px; overflow-x: auto; margin: 16px 0; }
.kb-article-content code { background: var(--gray-light); padding: 2px 6px; border-radius: 4px; font-family: 'SF Mono', 'Consolas', monospace; font-size: 13px; }
.kb-article-content a { color: var(--accent); text-decoration: underline; }
.kb-article-content img { max-width: 100%; border-radius: 8px; margin: 8px 0; display: block; }
.kb-article-content strong { font-weight: 700; }
.kb-article-content em { font-style: italic; }

/* Tables in articles */
.kb-article-content table, .kb-table { width: 100%; border-collapse: collapse; margin: 16px 0; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); }
.kb-article-content th, .kb-table th { background: var(--gray-light); padding: 10px 14px; text-align: left; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); border-bottom: 1px solid var(--border); }
.kb-article-content td, .kb-table td { padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 14px; }
.kb-article-content tr:last-child td, .kb-table tr:last-child td { border-bottom: none; }
.kb-article-content tr:hover td, .kb-table tr:hover td { background: #fafbfc; }

/* Info boxes */
.kb-info-box { display: block; border-radius: 8px; padding: 14px 16px; margin: 16px 0; border-left: 4px solid; font-size: 14px; line-height: 1.6; }
.kb-info-box--info    { background: #eff6ff; border-color: #3b82f6; color: #1e40af; }
.kb-info-box--tip     { background: #f0fdf4; border-color: #22c55e; color: #15803d; }
.kb-info-box--warning { background: #fffbeb; border-color: #f59e0b; color: #92400e; }
.kb-info-box--danger  { background: #fef2f2; border-color: #ef4444; color: #991b1b; }

/* Editor container */
.kb-editor-container { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }

/* Editor toolbar */
.kb-editor-toolbar { display: flex; flex-wrap: wrap; gap: 4px; padding: 10px 12px; background: var(--gray-light); border-bottom: 1px solid var(--border); align-items: center; }
.kb-toolbar-group { display: flex; gap: 2px; align-items: center; }
.kb-toolbar-group + .kb-toolbar-group { margin-left: 8px; padding-left: 8px; border-left: 1px solid var(--border); }
.kb-editor-toolbar button { display: inline-flex; align-items: center; gap: 4px; padding: 5px 8px; border: 1px solid transparent; border-radius: 5px; font-size: 12px; font-weight: 600; cursor: pointer; background: transparent; color: var(--text); white-space: nowrap; transition: background .12s, border-color .12s; }
.kb-editor-toolbar button:hover { background: var(--border); border-color: var(--border); }
.kb-editor-toolbar select { padding: 5px 8px; border: 1px solid var(--border); border-radius: 5px; font-size: 12px; font-family: inherit; background: var(--surface); color: var(--text); cursor: pointer; }
.kb-btn-info    { background: #eff6ff !important; color: #1e40af !important; }
.kb-btn-tip     { background: #f0fdf4 !important; color: #15803d !important; }
.kb-btn-warning { background: #fffbeb !important; color: #92400e !important; }
.kb-btn-danger  { background: #fef2f2 !important; color: #991b1b !important; }
.kb-btn-info:hover    { background: #dbeafe !important; }
.kb-btn-tip:hover     { background: #dcfce7 !important; }
.kb-btn-warning:hover { background: #fef3c7 !important; }
.kb-btn-danger:hover  { background: #fee2e2 !important; }

/* Editor writing area */
.kb-edit-area { min-height: 420px; padding: 24px 28px; outline: none; font-size: 15px; line-height: 1.75; color: var(--text); }
.kb-edit-area:focus { box-shadow: inset 0 0 0 2px rgba(99,102,241,.2); }

/* Placeholder */
.kb-edit-area:empty::before { content: 'Skriv innhold her...'; color: var(--text-muted); pointer-events: none; }

/* Media library */
.kb-media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.kb-media-item { position: relative; aspect-ratio: 1; border-radius: 8px; overflow: hidden; border: 2px solid transparent; background: var(--gray-light); cursor: pointer; transition: border-color .15s; }
.kb-media-item:hover { border-color: var(--accent); }
.kb-media-item img { width: 100%; height: 100%; object-fit: cover; }
.kb-media-item--selected { border-color: var(--accent) !important; }
.kb-media-delete { position: absolute; top: 4px; right: 4px; background: rgba(239,68,68,.9); color: #fff; border: none; border-radius: 4px; width: 22px; height: 22px; font-size: 11px; cursor: pointer; display: none; align-items: center; justify-content: center; padding: 0; }
.kb-media-item:hover .kb-media-delete { display: flex; }
.kb-media-name { position: absolute; bottom: 0; left: 0; right: 0; background: rgba(0,0,0,.55); color: #fff; font-size: 10px; padding: 3px 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Wide modal variant */
.modal--wide { max-width: 760px !important; }

@media (max-width: 768px) {
  .kb-article-card { padding: 20px 16px; }
  .kb-article-title { font-size: 20px; }
  .kb-editor-toolbar { gap: 3px; }
  .kb-edit-area { padding: 16px; }
  .modal--wide { max-width: calc(100vw - 32px) !important; }
}
