/* ============================================================
   Launcher Admin — design system
   ------------------------------------------------------------
   - Token-driven (spacing/colors)
   - 8px spacing scale
   - Single source of truth for cards, inputs, buttons, modals
   ============================================================ */

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

:root {
    /* Color palette */
    --bg:           #0e1015;
    --surface:      #181b22;
    --surface-2:    #21252e;
    --surface-3:    #2a2f3a;
    --primary:      #ff6a00;
    --primary-fade: rgba(255, 106, 0, 0.18);
    --accent:       #ffb066;
    --text:         #e9ecf1;
    --muted:        #8a93a3;
    --border:       #2a2f3a;
    --border-strong:#3a4150;
    --danger:       #ef4444;
    --danger-fade:  rgba(239, 68, 68, 0.15);
    --success:      #10b981;
    --warning:      #f59e0b;

    /* Spacing scale (8px base) */
    --s-1: 4px;
    --s-2: 8px;
    --s-3: 12px;
    --s-4: 16px;
    --s-5: 20px;
    --s-6: 24px;
    --s-8: 32px;
    --s-10: 40px;
    --s-12: 48px;

    /* Other */
    --radius-sm: 4px;
    --radius:    8px;
    --radius-lg: 12px;
    --shadow-md: 0 4px 12px rgba(0,0,0,0.25);
    --shadow-lg: 0 12px 40px rgba(0,0,0,0.45);
    --font: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;

    /* Layout */
    --sidebar-w: 240px;
}

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ----- App shell ----- */

.app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    min-height: 100vh;
}

@media (max-width: 768px) {
    .app { grid-template-columns: 1fr; }
}

.sidebar {
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: var(--s-5) var(--s-4);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
}

@media (max-width: 768px) {
    .sidebar { position: static; height: auto; }
}

.brand {
    display: flex;
    align-items: baseline;
    gap: var(--s-2);
    padding: var(--s-2) var(--s-3) var(--s-6);
    border-bottom: 1px solid var(--border);
    margin-bottom: var(--s-4);
}
.brand strong { color: var(--primary); font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.brand span   { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 1.5px; }

.sidebar nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.sidebar nav a {
    display: block;
    padding: var(--s-3) var(--s-4);
    color: var(--text);
    text-decoration: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    transition: background 120ms ease, color 120ms ease;
}
.sidebar nav a:hover { background: var(--surface-2); }
.sidebar nav a.active {
    background: var(--primary-fade);
    color: var(--primary);
}

.lang-picker {
    margin-top: auto;
    padding-top: var(--s-4);
    border-top: 1px solid var(--border);
}
.lang-picker select {
    width: 100%;
    padding: var(--s-2) var(--s-3);
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    cursor: pointer;
}
.lang-picker select:focus { outline: 2px solid var(--primary-fade); outline-offset: 1px; border-color: var(--primary); }

.content {
    padding: var(--s-8);
    /* No max-width: admin tables benefit from the full available width.
       overflow-x:auto on .table-wrap handles narrow viewports. */
    width: 100%;
    min-width: 0;
    overflow-x: hidden;
}

@media (max-width: 768px) {
    .content { padding: var(--s-5); }
}

/* ----- Typography ----- */

h1 {
    margin: 0 0 var(--s-6) 0;
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.01em;
}
h3 { margin: 0 0 var(--s-4) 0; font-size: 18px; font-weight: 600; }
p, li { margin: 0; }
p.muted { color: var(--muted); }

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--s-4);
    margin-bottom: var(--s-6);
    flex-wrap: wrap;
}
.page-header h1 { margin: 0; }

/* ----- Cards (dashboard) ----- */

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: var(--s-4);
}
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--s-6);
    transition: transform 120ms ease, border-color 120ms ease;
}
.card:hover { border-color: var(--border-strong); }
.card .num {
    font-size: 36px;
    font-weight: 700;
    color: var(--primary);
    line-height: 1.1;
}
.card .lbl {
    color: var(--muted);
    margin-top: var(--s-2);
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ----- Buttons ----- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--s-2);
    padding: var(--s-2) var(--s-4);
    border: 1px solid var(--border-strong);
    background: var(--surface-2);
    color: var(--text);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
    white-space: nowrap;
}
.btn:hover  { background: var(--surface-3); border-color: var(--primary); }
.btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:disabled:hover { background: var(--surface-2); border-color: var(--border-strong); }

.btn.primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
    font-weight: 600;
}
.btn.primary:hover { background: var(--accent); border-color: var(--accent); }

.btn.danger {
    background: transparent;
    color: var(--danger);
    border-color: var(--danger);
}
.btn.danger:hover { background: var(--danger-fade); }

.btn.ghost {
    background: transparent;
    border-color: transparent;
    color: var(--muted);
}
.btn.ghost:hover { background: var(--surface-2); color: var(--text); border-color: transparent; }

.btn.sm { padding: var(--s-1) var(--s-3); font-size: 12px; }

/* ----- Tables ----- */

.table-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
}
.table-wrap > .scroll {
    overflow-x: auto;
}

table.grid {
    width: 100%;
    min-width: 600px;
    border-collapse: collapse;
    font-size: 13px;
}
table.grid th, table.grid td {
    padding: var(--s-3) var(--s-4);
    text-align: left;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
table.grid tr:last-child td { border-bottom: none; }
table.grid th {
    background: var(--surface-2);
    color: var(--muted);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
}
table.grid tr:hover td { background: rgba(255,255,255,0.02); }
table.grid td.actions {
    text-align: right;
    white-space: nowrap;
}
table.grid td.actions .btn + .btn { margin-left: var(--s-2); }

/* ----- Forms ----- */

.field { display: flex; flex-direction: column; gap: var(--s-2); }
.field label, label.field-label {
    font-size: 12px;
    color: var(--muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.field-help { font-size: 12px; color: var(--muted); margin-top: var(--s-1); }
.field-error { font-size: 12px; color: var(--danger); margin-top: var(--s-1); }

input[type="text"], input[type="number"], input[type="password"], input[type="email"],
input:not([type]), textarea, select {
    width: 100%;
    padding: var(--s-3) var(--s-4);
    background: var(--bg);
    border: 1px solid var(--border-strong);
    color: var(--text);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: inherit;
    transition: border-color 120ms ease, box-shadow 120ms ease;
}
input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-fade);
}
textarea { min-height: 96px; resize: vertical; }
select { cursor: pointer; }

.checkbox-row {
    display: flex;
    align-items: center;
    gap: var(--s-3);
}
.checkbox-row input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--primary);
    cursor: pointer;
}
.checkbox-row label { color: var(--text); font-size: 13px; cursor: pointer; }

/* ----- Modal ----- */

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: var(--s-5);
    animation: fadeIn 120ms ease;
}
.modal-card {
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    padding: var(--s-8);
    width: 100%;
    max-width: 520px;
    max-height: calc(100vh - 80px);
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    gap: var(--s-4);
    animation: slideUp 180ms ease;
}
.modal-card h3 { margin-bottom: var(--s-2); }
.modal-actions {
    display: flex;
    gap: var(--s-3);
    justify-content: flex-end;
    margin-top: var(--s-4);
    padding-top: var(--s-4);
    border-top: 1px solid var(--border);
}

@keyframes fadeIn  { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ----- Toast notifications ----- */

.toast-stack {
    position: fixed;
    bottom: var(--s-5);
    right: var(--s-5);
    display: flex;
    flex-direction: column;
    gap: var(--s-3);
    z-index: 200;
    max-width: calc(100vw - 40px);
}
.toast {
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-left: 3px solid var(--primary);
    border-radius: var(--radius);
    padding: var(--s-3) var(--s-4);
    min-width: 280px;
    box-shadow: var(--shadow-md);
    display: flex;
    align-items: flex-start;
    gap: var(--s-3);
    animation: slideIn 180ms ease;
}
.toast.success { border-left-color: var(--success); }
.toast.error   { border-left-color: var(--danger); }
.toast.warning { border-left-color: var(--warning); }
.toast .body { flex: 1; font-size: 13px; }
.toast .body strong { display: block; margin-bottom: 2px; font-size: 13px; }
.toast .body small { color: var(--muted); }
.toast button.close {
    background: none; border: none; color: var(--muted); cursor: pointer;
    font-size: 16px; line-height: 1; padding: 0;
}
.toast button.close:hover { color: var(--text); }
@keyframes slideIn { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: none; } }

/* ----- Empty state ----- */

.empty {
    text-align: center;
    padding: var(--s-12) var(--s-5);
    background: var(--surface);
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-lg);
    color: var(--muted);
}
.empty .icon { font-size: 32px; margin-bottom: var(--s-3); opacity: 0.6; }
.empty h3 { color: var(--text); margin-bottom: var(--s-2); }
.empty p { margin-bottom: var(--s-5); }

/* ----- Misc ----- */

code {
    background: var(--surface-2);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    font-family: 'JetBrains Mono', 'Cascadia Code', Consolas, ui-monospace, monospace;
}

.callout {
    background: var(--surface);
    border-left: 3px solid var(--primary);
    padding: var(--s-4) var(--s-5);
    margin-bottom: var(--s-5);
    border-radius: var(--radius);
    display: flex;
    flex-direction: column;
    gap: var(--s-3);
}
.callout.warning { border-left-color: var(--warning); }
.callout strong { font-size: 13px; }

.token-field {
    display: flex;
    gap: var(--s-2);
}
.token-field input {
    font-family: 'JetBrains Mono', 'Cascadia Code', Consolas, monospace;
    font-size: 12px;
}

.badge {
    display: inline-flex;
    align-items: center;
    gap: var(--s-1);
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.badge.live    { background: rgba(16, 185, 129, 0.15); color: var(--success); }
.badge.draft   { background: var(--surface-2); color: var(--muted); }
.badge.danger  { background: var(--danger-fade); color: var(--danger); }

.muted { color: var(--muted); }
.dim   { opacity: 0.7; }

.row { display: flex; gap: var(--s-3); align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }

/* ----- Tabs ----- */

nav.tabs {
    display: flex;
    gap: var(--s-1);
    border-bottom: 1px solid var(--border);
    margin-bottom: var(--s-6);
    overflow-x: auto;
}
nav.tabs a.tab {
    padding: var(--s-3) var(--s-4);
    color: var(--muted);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    white-space: nowrap;
    transition: color 120ms, border-color 120ms;
}
nav.tabs a.tab:hover { color: var(--text); }
nav.tabs a.tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.tab-content { animation: fadeIn 120ms ease; }

/* ----- Layout editor ----- */

.layout-editor {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: var(--s-5);
    align-items: start;
}
@media (max-width: 1100px) {
    .layout-editor { grid-template-columns: 1fr; }
}

.layout-tree {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--s-4);
    position: sticky;
    top: var(--s-5);
    max-height: calc(100vh - 80px);
    overflow-y: auto;
}
.layout-tree .tree-root { list-style: none; padding: 0; margin: 0; }
.layout-tree .tree-node {
    list-style: none;
    padding: 6px 8px;
    cursor: pointer;
    border-radius: 4px;
    font-size: 13px;
    line-height: 1.4;
    user-select: none;
}
.layout-tree .tree-node:hover { background: var(--surface-2); }
.layout-tree .tree-node.selected {
    background: var(--primary-fade);
    color: var(--primary);
}
.layout-tree .kind { font-weight: 600; }
.layout-tree .name { color: var(--muted); }

.layout-detail { min-width: 0; }

.launcher-frame {
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--s-5);
    margin-top: var(--s-3);
    box-shadow: var(--shadow-md);
    aspect-ratio: 16 / 9;
    overflow: hidden;
}

/* ----- Layout editor v3: palette + drag-and-drop ----- */

.layout-editor3 {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: var(--s-5);
    align-items: start;
    outline: none;
}
@media (max-width: 1100px) {
    .layout-editor3 { grid-template-columns: 1fr; }
}

.layout-editor3 .layout-tree { /* reuse the existing layout-tree styles above */ }

.palette {
    margin-bottom: var(--s-4);
    background: var(--surface-2);
    border-radius: var(--radius);
    padding: var(--s-2) var(--s-3);
}
.palette > summary {
    cursor: pointer;
    font-size: 12px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    padding: var(--s-1) 0;
    list-style: none;
    user-select: none;
}
.palette > summary::-webkit-details-marker { display: none; }
.palette > summary::before { content: "▸ "; transition: transform 120ms; display: inline-block; }
.palette[open] > summary::before { transform: rotate(90deg); }

.palette-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: var(--s-2);
    margin-top: var(--s-2);
}
.palette-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: var(--s-2) var(--s-1);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: grab;
    user-select: none;
    transition: border-color 120ms, transform 120ms;
}
.palette-item:hover { border-color: var(--primary); transform: translateY(-1px); }
.palette-item:active { cursor: grabbing; }
.palette-icon { font-size: 18px; line-height: 1; }
.palette-label { font-size: 10px; color: var(--muted); }

.tree-node[draggable] { cursor: grab; }
.tree-node[draggable]:active { cursor: grabbing; }
.tree-node.drag-over {
    background: var(--primary-fade);
    outline: 1px dashed var(--primary);
    outline-offset: -1px;
}

/* ----- Layout preview: selectable nodes ----- */

.lp-node {
    cursor: pointer;
    outline-offset: -1px;
    transition: outline-color 120ms;
}
.lp-node:hover {
    outline: 1px solid var(--primary);
}
.lp-node.lp-selected {
    outline: 2px solid var(--primary);
    outline-offset: -2px;
}

.spinner {
    display: inline-block;
    width: 14px; height: 14px;
    border: 2px solid var(--border-strong);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
