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

:root,
[data-theme="dark"] {
    --bg: #0f1419;
    --bg-surface: #1a1f2e;
    --bg-surface-hover: #242b3d;
    --bg-card: #1e2536;
    --bg-input: #151b28;
    --border: #2a3245;
    --border-focus: #4a7dff;
    --text: #e1e4ea;
    --text-muted: #8892a4;
    --text-dim: #5a6478;
    --primary: #4a7dff;
    --primary-hover: #6090ff;
    --primary-bg: rgba(74, 125, 255, 0.1);
    --danger: #ff4d6a;
    --danger-bg: rgba(255, 77, 106, 0.1);
    --success: #34d399;
    --success-bg: rgba(52, 211, 153, 0.1);
    --warning: #fbbf24;
    --warning-bg: rgba(251, 191, 36, 0.1);
    --radius: 8px;
    --radius-sm: 4px;
    --shadow: 0 2px 8px rgba(0,0,0,0.3);
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
}

[data-theme="light"] {
    --bg: #f0f2f5;
    --bg-surface: #ffffff;
    --bg-surface-hover: #f5f6f8;
    --bg-card: #f8f9fb;
    --bg-input: #ffffff;
    --border: #d0d5dd;
    --border-focus: #3b6de0;
    --text: #1a1d23;
    --text-muted: #5f6775;
    --text-dim: #8c95a6;
    --primary: #3b6de0;
    --primary-hover: #2f5ec7;
    --primary-bg: rgba(59, 109, 224, 0.08);
    --danger: #dc2e4a;
    --danger-bg: rgba(220, 46, 74, 0.08);
    --success: #16a368;
    --success-bg: rgba(22, 163, 104, 0.08);
    --warning: #ca8a04;
    --warning-bg: rgba(202, 138, 4, 0.08);
    --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

[data-theme="dark"] .theme-icon-dark { display: inline; }
[data-theme="dark"] .theme-icon-light { display: none; }
[data-theme="light"] .theme-icon-dark { display: none; }
[data-theme="light"] .theme-icon-light { display: inline; }
.theme-toggle { font-size: 18px; }

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    font-size: 14px;
    min-height: 100vh;
}

/* === Navbar === */
.navbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    height: 56px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}
.nav-brand a {
    color: var(--text);
    text-decoration: none;
    font-weight: 700;
    font-size: 15px;
    letter-spacing: -0.02em;
}
.nav-links { display: flex; gap: 4px; align-items: center; }
.nav-link {
    color: var(--text-muted);
    text-decoration: none;
    padding: 8px 16px;
    border-radius: var(--radius);
    font-size: 13px;
    font-weight: 500;
    transition: all 0.15s;
}
.nav-link:hover { color: var(--text); background: var(--bg-surface-hover); }
.nav-link.active { color: var(--primary); background: var(--primary-bg); }

/* === Container === */
.container { max-width: 1600px; margin: 0 auto; padding: 24px; }

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-surface);
    color: var(--text);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
    font-family: var(--font);
}
.btn:hover { background: var(--bg-surface-hover); border-color: var(--text-dim); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-secondary { background: transparent; }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-bg); }
.btn-small { padding: 4px 10px; font-size: 12px; }
.btn-large { padding: 10px 24px; font-size: 14px; }
.btn-icon { padding: 4px 8px; border: none; background: none; color: var(--text-muted); font-size: 16px; cursor: pointer; border-radius: var(--radius-sm); }
.btn-icon:hover { color: var(--text); background: var(--bg-surface-hover); }
.btn-icon.btn-danger:hover { color: var(--danger); background: var(--danger-bg); }

/* === Forms === */
.form-section {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    margin-bottom: 16px;
}
.section-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.section-title { font-size: 16px; font-weight: 600; margin-bottom: 16px; }
.section-header .section-title { margin-bottom: 0; }
.section-hint { color: var(--text-muted); font-size: 13px; margin-bottom: 16px; }

.form-group { margin-bottom: 16px; }
.form-group:last-child { margin-bottom: 0; }
.form-row { display: flex; gap: 16px; }
.form-row .form-group { margin-bottom: 16px; }
.flex-1 { flex: 1; }

.form-label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text);
}

.form-input, .form-select {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 13px;
    font-family: var(--font);
    transition: border-color 0.15s;
}
.form-input:focus, .form-select:focus {
    outline: none;
    border-color: var(--border-focus);
    box-shadow: 0 0 0 2px rgba(74, 125, 255, 0.15);
}
.form-input::placeholder { color: var(--text-dim); }
.form-textarea { resize: vertical; min-height: 60px; }
.form-select { cursor: pointer; }
.form-select option { background: var(--bg-surface); color: var(--text); }

.form-input.is-invalid, .form-select.is-invalid { border-color: var(--danger); }

.input-group { display: flex; gap: 8px; }
.input-group .form-input { flex: 1; }

.field-error { color: var(--danger); font-size: 12px; margin-top: 4px; min-height: 0; }

/* === Tooltips === */
.tooltip-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    font-size: 13px;
    color: var(--text-dim);
    cursor: help;
    flex-shrink: 0;
}
.tooltip-trigger:hover { color: var(--primary); }

.tooltip-popup {
    position: fixed;
    z-index: 9999;
    background: var(--bg-card);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 12px;
    font-size: 12px;
    font-weight: 400;
    line-height: 1.5;
    white-space: normal;
    width: 300px;
    max-width: calc(100vw - 24px);
    box-shadow: var(--shadow);
    pointer-events: none;
}

/* === Badges === */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 99px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.badge-submodule { background: var(--primary-bg); color: var(--primary); }
.badge-user { background: var(--success-bg); color: var(--success); }

.meta-badge {
    display: inline-block;
    padding: 2px 8px;
    background: var(--bg-surface-hover);
    border-radius: var(--radius-sm);
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 500;
}

/* === Tag inputs === */
.tag-input-wrapper {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 4px 8px;
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
    min-height: 38px;
    cursor: text;
    transition: border-color 0.15s;
}
.tag-input-wrapper:focus-within { border-color: var(--border-focus); box-shadow: 0 0 0 2px rgba(74, 125, 255, 0.15); }

.tag-list { display: contents; }
.tag-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    background: var(--bg-surface-hover);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 12px;
    white-space: nowrap;
}
.tag-remove {
    cursor: pointer;
    color: var(--text-dim);
    font-size: 14px;
    line-height: 1;
}
.tag-remove:hover { color: var(--danger); }
.tag-input {
    border: none !important;
    background: none !important;
    box-shadow: none !important;
    padding: 4px !important;
    min-width: 120px;
    flex: 1;
    font-size: 12px;
    color: var(--text);
}
.tag-input:focus { outline: none; }

/* === Flags / toggle labels === */
.flags-row { display: flex; flex-wrap: wrap; gap: 16px; }
.toggle-label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    cursor: pointer;
    color: var(--text-muted);
}
.toggle-label:hover { color: var(--text); }
.toggle-label input:checked + span { color: var(--primary); }

/* === Editor layout === */
.editor-page { height: calc(100vh - 56px - 48px); }
.editor-layout {
    display: grid;
    grid-template-columns: 1fr 420px;
    gap: 24px;
    height: 100%;
    align-items: start;
}
.editor-form {
    overflow-y: auto;
    max-height: calc(100vh - 56px - 48px);
    padding-right: 8px;
}
.editor-preview {
    position: sticky;
    top: 80px;
    max-height: calc(100vh - 56px - 48px);
    display: flex;
    flex-direction: column;
}

.preview-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
}
.preview-header h2 { font-size: 14px; font-weight: 600; }

.json-preview {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    padding: 16px;
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 1.6;
    overflow: auto;
    flex: 1;
    white-space: pre;
    color: var(--text-muted);
    tab-size: 2;
}

.validation-badge { display: flex; align-items: center; gap: 8px; }
.validation-errors {
    background: var(--danger-bg);
    border: 1px solid var(--border);
    border-top: none;
    padding: 12px 16px;
    max-height: 200px;
    overflow-y: auto;
}
.validation-error-item {
    font-size: 12px;
    padding: 4px 0;
    color: var(--danger);
    cursor: pointer;
}
.validation-error-item:hover { text-decoration: underline; }
.validation-warning-item {
    font-size: 12px;
    padding: 4px 0;
    color: var(--warning);
}

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}
.page-header h1 { font-size: 22px; font-weight: 700; }

.actions-section {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: flex-start;
}

/* === Value cards === */
.value-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 8px;
    transition: border-color 0.15s;
}
.value-card:hover { border-color: var(--text-dim); }
.value-card.collapsed .value-card-body { display: none; }

.value-card-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    cursor: pointer;
    user-select: none;
}
.value-card-header:hover { background: var(--bg-surface-hover); border-radius: var(--radius); }
.value-card.collapsed .value-card-header { border-bottom: none; }
.value-card:not(.collapsed) .value-card-header { border-bottom: 1px solid var(--border); }

.value-name { font-size: 13px; font-weight: 500; color: var(--primary); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.value-meta-hint { font-size: 11px; color: var(--text-dim); }
.value-card-actions { display: flex; gap: 2px; margin-left: auto; }
.value-card-body { padding: 16px; }
.value-revoked { opacity: 0.5; }
.value-revoked .value-name { text-decoration: line-through; }

.collapse-toggle { transition: transform 0.15s; font-size: 10px; display: inline-block; }
.value-card.collapsed .collapse-toggle { transform: rotate(-90deg); }

/* === Meta editor rows === */
.meta-row {
    display: flex;
    gap: 8px;
    align-items: flex-start;
    margin-bottom: 8px;
}
.meta-row .meta-key { width: 180px; flex-shrink: 0; }
.meta-row .meta-value { flex: 1; }

/* === Related editor rows === */
.related-row {
    display: flex;
    gap: 8px;
    align-items: flex-start;
    margin-bottom: 8px;
    padding: 8px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

/* === Kill chain editor === */
.kc-scope {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    margin-bottom: 8px;
}
.kc-scope-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.kc-scope-header input { flex: 1; }
.kc-phases { display: flex; flex-direction: column; gap: 4px; }
.kc-phase {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: grab;
}
.kc-phase:active { cursor: grabbing; }
.kc-phase input { flex: 1; border: none; background: none; color: var(--text); font-size: 13px; font-family: var(--font); padding: 2px; }
.kc-phase input:focus { outline: none; }
.kc-phase .drag-handle { color: var(--text-dim); cursor: grab; font-size: 12px; }

/* === Matrix view === */
.matrix-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.matrix-tab {
    padding: 6px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-surface);
    color: var(--text-muted);
    cursor: pointer;
    font-size: 13px;
    transition: all 0.15s;
}
.matrix-tab:hover { border-color: var(--text-dim); color: var(--text); }
.matrix-tab.active { background: var(--primary-bg); border-color: var(--primary); color: var(--primary); }

.matrix-grid {
    display: grid;
    gap: 8px;
    overflow-x: auto;
    padding-bottom: 16px;
}
.matrix-column {
    min-width: 160px;
}
.matrix-column-header {
    font-size: 12px;
    font-weight: 600;
    padding: 8px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
    text-align: center;
    position: sticky;
    top: 0;
    z-index: 10;
}
.matrix-cell {
    min-height: 60px;
    padding: 6px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius) var(--radius);
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.matrix-cell.drag-over {
    background: var(--primary-bg);
    border-color: var(--primary);
}

.matrix-card {
    padding: 6px 10px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 12px;
    cursor: grab;
    transition: all 0.15s;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.matrix-card:active { cursor: grabbing; opacity: 0.7; }
.matrix-card:hover { border-color: var(--primary); }
.matrix-card.dragging { opacity: 0.3; }

.matrix-unplaced {
    margin-top: 16px;
    padding: 12px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.matrix-unplaced-header {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.matrix-unplaced-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    max-height: 200px;
    overflow-y: auto;
}

/* === Pagination === */
.pagination {
    display: flex;
    gap: 4px;
    align-items: center;
    justify-content: center;
    margin-top: 12px;
}
.pagination button {
    padding: 4px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-surface);
    color: var(--text-muted);
    font-size: 12px;
    cursor: pointer;
}
.pagination button:hover { border-color: var(--text-dim); color: var(--text); }
.pagination button.active { background: var(--primary-bg); border-color: var(--primary); color: var(--primary); }
.pagination button:disabled { opacity: 0.4; cursor: default; }
.pagination .page-info { font-size: 12px; color: var(--text-dim); margin: 0 8px; }

/* === Browser === */
.browser-page { padding-top: 8px; }
.subtitle { color: var(--text-muted); font-size: 14px; margin-top: 4px; }

.browser-controls {
    display: flex;
    gap: 12px;
    align-items: center;
    margin: 20px 0;
}
.search-input { max-width: 400px; }
.result-count { color: var(--text-muted); font-size: 13px; margin-left: auto; }

.templates-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 12px;
}
.template-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    transition: border-color 0.15s;
}
.template-card:hover { border-color: var(--text-dim); }
.template-card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.template-name { font-size: 14px; font-weight: 600; }
.template-desc {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 12px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.template-meta { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
.meta-info { font-size: 11px; color: var(--text-dim); }
.template-actions { display: flex; gap: 8px; }

/* === Modal === */
.modal {
    position: fixed;
    inset: 0;
    z-index: 500;
    display: flex;
    align-items: center;
    justify-content: center;
}
.modal[hidden] { display: none; }
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
}
.modal-content {
    position: relative;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    width: 90%;
    max-width: 700px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 16px; }
.modal-body { padding: 20px; overflow-y: auto; flex: 1; }
.modal-footer { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }

.modal .json-preview {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin: 0 20px;
    flex: 1;
    overflow: auto;
    min-height: 200px;
}

.divider-text {
    text-align: center;
    color: var(--text-dim);
    font-size: 12px;
    margin: 12px 0;
    position: relative;
}
.divider-text::before, .divider-text::after {
    content: '';
    position: absolute;
    top: 50%;
    width: calc(50% - 20px);
    height: 1px;
    background: var(--border);
}
.divider-text::before { left: 0; }
.divider-text::after { right: 0; }

/* === Toast === */
#toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 999;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    padding: 10px 20px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    box-shadow: var(--shadow);
    animation: toast-in 0.2s ease-out;
}
.toast.success { border-color: var(--success); color: var(--success); }
.toast.error { border-color: var(--danger); color: var(--danger); }

@keyframes toast-in {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* === Load existing list === */
.load-templates-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-height: 50vh;
    overflow-y: auto;
}
.load-template-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.1s;
}
.load-template-row:hover { background: var(--bg-surface-hover); border-color: var(--text-dim); }
.load-template-name { font-weight: 600; font-size: 13px; min-width: 180px; }
.load-template-desc { font-size: 12px; color: var(--text-muted); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.load-template-meta { font-size: 11px; color: var(--text-dim); white-space: nowrap; }

/* === Loading / empty states === */
.loading { text-align: center; padding: 40px; color: var(--text-muted); }
.empty-state { text-align: center; padding: 40px; color: var(--text-dim); grid-column: 1 / -1; }

/* === Scrollbar === */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* === Responsive === */
@media (max-width: 1100px) {
    .editor-layout { grid-template-columns: 1fr; }
    .editor-preview { position: static; max-height: 400px; }
    .editor-form { max-height: none; }
}
