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

:root {
    --bg: #2c2c2c;
    --bg-surface: #373737;
    --bg-card: #252525; /* darker grey for cards */
    --bg-hover: #3f3f3f;
    --text: #e2e2e2;
    --text-muted: #a8a8a8;
    --primary: #4f6df5; /* calm blue accent */
    --primary-hover: #3b57d1;
    --danger: #e74c5a;
    --danger-hover: #c93842;
    --warning: #e67e22;
    --warning-hover: #d7620f;
    --success: #2ecc71;
    --success-hover: #27ae60;
    --border: #2f2f3f;
    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 2px 10px rgba(0,0,0,0.25);
    --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
    --transition: 0.2s ease;
}

html { font-size: 15px; }

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

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Navbar ───────────────────────────────────────────────────────────────── */
.navbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* leave extra room on right so fixed toggle button doesn't sit over links */
    padding: 0.75rem 4rem 0.75rem 2rem;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
    backdrop-filter: blur(12px);
}
.navbar.collapsed {
    display: none; /* remove entire bar when collapsed */
}

#navbarToggle {
    position: fixed;
    top: 0.5rem;
    right: 1rem;
    z-index: 110;
    background: none;
    border: none;
    color: var(--text);
    font-size: 1.4rem;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
}

.nav-brand {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--text);
    text-decoration: none;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.nav-link {
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 500;
    padding: 0.4rem 0.8rem;
    border-radius: var(--radius-sm);
    transition: all var(--transition);
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 0.3rem;
}
.nav-link:hover {
    color: var(--text);
    background: var(--bg-hover);
    text-decoration: none;
}

/* ── Container ────────────────────────────────────────────────────────────── */
.container {
    /* reduce the max width so tiles span more of the viewport, but keep
       some sensible limit on wide screens */
    max-width: 90vw;
    margin: 0 auto;
    padding: 2rem 1.5rem; /* slightly smaller horizontal padding */
}

/* When the viewport is extremely wide, still cap the container so cards don’t
   become ridiculously stretched */
@media (min-width: 1600px) {
    .container { max-width: 1600px; }
}

/* ── Search Bar ───────────────────────────────────────────────────────────── */
.search-bar {
    max-width: 600px;
    margin: 0 auto 2.5rem;
}

.search-form {
    position: relative;
    display: flex;
    align-items: center;
}

.search-icon {
    position: absolute;
    left: 1rem;
    color: var(--text-muted);
    pointer-events: none;
}

.search-input {
    width: 100%;
    padding: 0.9rem 1rem 0.9rem 3rem;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 50px;
    color: var(--text);
    font-size: 1rem;
    outline: none;
    transition: border-color var(--transition), box-shadow var(--transition);
}
.search-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 109, 245, 0.15);
}

/* ── Sections ─────────────────────────────────────────────────────────────── */
.section { margin-bottom: 2.5rem; }

.section-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 1rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* ── App Grid ─────────────────────────────────────────────────────────────── */
.app-grid {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    align-content: center;
    padding: 30px 10px;
    gap: 20px;
}

/* ── Mobile adjustments ──────────────────────────────────────────────────── */
html { font-size: 15px; }
@media (max-width: 900px) {
    html { font-size: 14px; }
}
@media (max-width: 600px) {
    html { font-size: 13px; }
}

/* container padding on small screens (widened for mobile) */
@media (max-width: 800px) {
    .container { padding: 1.5rem; }
}
@media (max-width: 500px) {
    .container { padding: 1rem; }
}

/* ensure grid and cards don't kiss screen edges */
@media (max-width: 600px) {
    .app-grid { padding: 0 0.75rem; gap: 1.5rem; }
}

/* For very narrow viewports, fall back to a single column to keep cards readable */
@media (max-width: 480px) {
    .app-card { flex: 0 0 100%; width: 100%; max-width: none; }
}

/* hide title when card too narrow for text */
@container (max-width: 140px) {
    .app-info { display: none; }
}

/* allow title to wrap onto two lines once the card is at least 160px wide */
@container (min-width: 160px) {
    .app-title { white-space: normal; }
}

/* navbar becomes vertical on narrow viewports */
@media (max-width: 768px) {
    .navbar {
        flex-direction: column;
        align-items: flex-start;
        padding: 0.75rem 1rem;
    }
    .nav-links {
        flex-direction: column;
        width: 100%;
        margin-top: 0.5rem;
    }
    .nav-link {
        width: 100%;
        padding: 0.75rem 1rem;
        border-radius: 0;
    }
    #navbarToggle { display: block; }
}

/* hide toggle button on larger screens unless nav is collapsed via settings */
#navbarToggle { display: none; }
.navbar.collapsed + #navbarToggle {
    /* when the bar is hidden by the "show navigation bar" setting, show the
       toggle so the user can open it manually even on desktop */
    display: block;
}

/* search bar full width on small viewports */
@media (max-width: 600px) {
    .search-bar { margin: 0 auto 1.5rem; padding: 0 0.5rem; }
}

/* hide brand text on very narrow screens */
@media (max-width: 500px) {
    .nav-brand span { display: none; }
}

.app-card {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    padding: 0;

    /* Tile size + layout (Heimdall-style) */
    width: 280px;
    min-height: 90px;
    margin: 12px;
    flex: 0 0 280px;

    /* subtle tint based on the app accent color, while keeping the overall dark theme */
    background-image: linear-gradient(90deg, rgba(255,255,255,0), rgba(255,255,255,0.25));
    background-color: color-mix(in srgb, var(--card-color, var(--primary)) 12%, var(--bg-card));
    border-radius: 6px;
    border: 1px solid rgba(76, 76, 76, 0.4);

    box-shadow: 0 0 20px 2px rgba(0,0,0,0.3);
    text-decoration: none;
    color: var(--text);
    transition: all 0.35s ease-in-out;
    overflow: hidden;
    will-change: transform, filter, box-shadow;
}


.app-card:focus-visible {
    outline: 0;
    box-shadow: 0 0 0 3px rgba(79, 109, 245, 0.45), 0 6px 18px rgba(0,0,0,0.45);
}

.app-card-footer {
    display: none;
}
.app-stats {
    /* kept for compatibility when debugging; primary output is in .app-meta now */
    display: none;
}
.app-card:hover {
    /* Subtle lift + brightness for a tactile feel */
    transform: translateY(-2px);
    filter: brightness(1.14);
    box-shadow: 0 10px 28px rgba(0,0,0,0.45);
    text-decoration: none;
    color: var(--text);
}

/* Blink the tile when alerts are present */
.app-card.has-alerts-warning {
    border-color: var(--warning);
    animation: alertPulseWarning 1.4s infinite ease-in-out;
}

@keyframes alertPulseWarning {
    0%, 100% { box-shadow: 0 4px 16px rgba(230, 126, 34, 0.25); }
    50% { box-shadow: 0 0 20px rgba(230, 126, 34, 0.65); }
}

.app-card.has-alerts-danger {
    border-color: var(--danger);
    animation: alertPulseDanger 1.4s infinite ease-in-out;
}

@keyframes alertPulseDanger {
    0%, 100% { box-shadow: 0 4px 16px rgba(231, 76, 90, 0.25); }
    50% { box-shadow: 0 0 20px rgba(231, 76, 90, 0.65); }
}

.app-card-main {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 11px 12px;
    width: 100%;
    container-type: inline-size;
}

/* Hide the right-side arrow panel */
.app-arrow {
    display: none !important;
}

.app-icon-wrap {
    width: 72px;
    height: 72px;
    border-radius: 0; /* square container for icon */
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
    box-shadow: 0 1px 4px rgba(0,0,0,0.25);
    border: 1px solid rgba(255,255,255,0.12);
}

.app-icon {
    width: 64px;
    height: 64px;
    object-fit: contain;
}
/* Circle wrapper for icon */
.app-icon-circle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 64px;
    height: 64px;
    border-radius: 0; /* full square container */
    background: rgba(0,0,0,0.18);
    margin-right: 1rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.18);
}

.app-arrow {
    position: absolute;
    top: 0;
    bottom: 0;
    right: -30px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100px;
    opacity: 0.9;
    background: rgba(0,0,0,0.18);
    border-left: 1px solid rgba(255,255,255,0.08);
    backdrop-filter: blur(12px);
    border-top-left-radius: 999px;
    border-bottom-left-radius: 999px;
}

.app-arrow svg {
    width: 24px;
    height: 24px;
    padding: 0.25rem;
    border-radius: 50%;
    background: rgba(255,255,255,0.08);
    filter: drop-shadow(2px 0 8px rgba(0,0,0,0.4));
    animation: glowPulse 2.5s infinite ease-in-out;
}

@keyframes glowPulse {
    0%, 100% { filter: drop-shadow(0 0 6px rgba(255,255,255,0.22)); }
    50% { filter: drop-shadow(0 0 8px rgba(255,255,255,0.28)); }
}
.app-icon-letter {
    font-size: 1rem;
    font-weight: 700;
    color: white;
}

.app-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.app-meta {
    margin-top: 0.15rem;
    font-size: 0.8rem;
    color: #ced3db;
    line-height: 1.1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-meta-inactive {
    opacity: 0.75;
}

.app-title {
    font-weight: 600;
    font-size: 0.95rem; /* smaller title */
    /* allow wrapping to two lines when container is a bit wider than minimum */
    white-space: normal;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

/* dragging styles */
.app-card.dragging { opacity: 0.5; }

/* highlight card when dragging over it */
.app-card.drop-target {
    box-shadow: 0 0 0 3px rgba(79, 109, 245, 0.85), 0 0 0 1px rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(79, 109, 245, 0.6);
}

.app-desc {
    font-size: 0.9rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ── Page Header ──────────────────────────────────────────────────────────── */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1.5rem;
}
.page-header h1 { font-size: 1.5rem; font-weight: 700; }

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.55rem 1.2rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-surface);
    color: var(--text);
    font-size: 0.88rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
    white-space: nowrap;
}
.btn:hover { background: var(--bg-hover); text-decoration: none; }

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

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

.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.82rem; }

/* ── Tables ───────────────────────────────────────────────────────────────── */
.table-wrap {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow-x: auto;
}

.table {
    width: 100%;
    border-collapse: collapse;
}
.table th, .table td {
    padding: 0.75rem 1rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}
.table th {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 0.04em;
}
.table th .sort-indicator {
    display: inline-block;
    width: 1.1em;
    text-align: center;
    opacity: 0.6;
    font-size: 0.9rem;
}
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: var(--bg-hover); }

.bulk-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    background: rgba(0,0,0,0.04);
}

.bulk-actions label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
}

.bulk-actions select {
    height: 2.4rem;
    background: #555;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0 0.5rem;
}

.table-icon {
    width: 64px;
    height: 64px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 700;
    overflow: hidden;
}
.table-icon img { object-fit: contain; }

.url-cell a { color: var(--text-muted); font-size: 0.85rem; }
.actions-cell { display: flex; gap: 0.4rem; align-items: center; }
.inline-form { display: inline; }

/* ── Forms ────────────────────────────────────────────────────────────────── */
.form-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    max-width: 700px;
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.2rem;
}
.form-group.full-width { grid-column: 1 / -1; }

.form-group { display: flex; flex-direction: column; gap: 0.4rem; }

.form-group label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
}

.form-input {
    padding: 0.6rem 0.8rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 0.92rem;
    outline: none;
    transition: border-color var(--transition);
}
.form-input:focus { border-color: var(--primary); }

.form-color {
    width: 48px;
    height: 38px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    background: transparent;
    padding: 2px;
}

.color-input-wrap {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.color-text { flex: 1; }

.form-hint { font-size: 0.78rem; color: var(--text-muted); margin-top: 0.2rem; }

.form-actions {
    display: flex;
    gap: 0.7rem;
    margin-top: 1.5rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border);
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    padding-top: 1.5rem;
}
.checkbox-label input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary);
}

.current-icon {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.4rem;
    font-size: 0.82rem;
    color: var(--text-muted);
}
.current-icon img { border-radius: 4px; }

/* ── Form Sections ─────────────────────────────────────────────────────── */
.form-section-title {
    font-size: 1rem;
    font-weight: 700;
    margin: 2rem 0 0.3rem;
    color: var(--text);
}
.form-section-desc {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-bottom: 1rem;
}

/* ── App Stats Bar ──────────────────────────────────────────────────────── */
.app-stats {
    margin-top: 0.3rem;
    font-size: 0.78rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-height: 18px;
    display: block;
}

.stats-loading {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    color: var(--text-muted);
    font-size: 0.76rem;
}

.stats-value {
    color: var(--text);
    font-weight: 500;
}

.stats-error {
    color: var(--danger);
    font-size: 0.76rem;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}
.spin {
    animation: spin 1s linear infinite;
}

/* ── Flash Messages ───────────────────────────────────────────────────────── */
.flash-container {
    max-width: 1400px;
    margin: 1rem auto 0;
    padding: 0 2rem;
}

.flash {
    padding: 0.7rem 1rem;
    border-radius: var(--radius-sm);
    font-size: 0.88rem;
    margin-bottom: 0.5rem;
    border: 1px solid;
}
.flash-success { background: rgba(46,204,113,0.1); border-color: var(--success); color: var(--success); }
.flash-error   { background: rgba(231,76,90,0.1);  border-color: var(--danger);  color: var(--danger); }

/* ── Empty State ──────────────────────────────────────────────────────────── */
.empty-state {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-muted);
}
.empty-state h3 { margin: 1rem 0 0.5rem; color: var(--text); }
.empty-state p { margin-bottom: 1.5rem; }

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 700px) {
    .navbar { padding: 0.75rem 1rem; }
    .container { padding: 1rem; }
    .form-grid { grid-template-columns: 1fr; }
    .form-card { padding: 1.2rem; }
    .page-header { flex-wrap: wrap; gap: 0.7rem; }
    #navbarToggle { display: block; }
    .nav-brand span { display: none; }
}

/* ── Quake Terminal ───────────────────────────────────────────────────────── */
.quake-terminal.qt-open {
    transform: translateY(0);
}
.qt-header {
    display: flex;
    align-items: center;
    padding: 0.4rem 1rem;
    background: rgba(0, 0, 0, 0.45);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    gap: 1rem;
    flex-shrink: 0;
}
.qt-title {
    font-weight: bold;
    color: var(--qt-accent, var(--primary, #6c8ebf));
    flex: 0 0 auto;
}
.qt-hint {
    font-size: 0.78rem;
    color: #8899bb;
    flex: 1;
}
.qt-hint kbd {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 3px;
    padding: 0 5px;
    font-size: 0.75rem;
    font-family: inherit;
}
.qt-close {
    background: none;
    border: none;
    color: #8899bb;
    font-size: 1.3rem;
    cursor: pointer;
    line-height: 1;
    padding: 0 0.25rem;
    flex-shrink: 0;
}
.qt-close:hover { color: #ff6b6b; }

.qt-output {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 1rem;
    min-height: 0;
    scrollbar-width: thin;
    scrollbar-color: #2a3450 transparent;
}
.qt-output::-webkit-scrollbar { width: 5px; }
.qt-output::-webkit-scrollbar-thumb { background: #2a3450; border-radius: 3px; }

.qt-line {
    padding: 1px 0;
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.55;
}
.qt-line.qt-info    { color: #9ddaf0; }
.qt-line.qt-success { color: #8fedb8; }
.qt-line.qt-error   { color: #ff9090; }
.qt-line.qt-cmd     { color: #ffe499; }
.qt-line.qt-muted   { color: #7888aa; }
.qt-line.qt-ai      { color: #d8e4ff; white-space: pre-wrap; }
.qt-line.qt-ai a    { color: #7ec8f5; text-decoration: underline; }
.qt-line.qt-ai a:hover { color: #b8e4ff; }

.qt-app-row {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    cursor: pointer;
    color: #7eaadd;
    padding: 1px 6px;
    border-radius: 3px;
    transition: background 0.15s, color 0.15s;
    user-select: none;
}
.qt-app-row:hover {
    background: rgba(100, 180, 255, 0.12);
    color: #c8e8ff;
}
.qt-app-icon { font-size: 0.7rem; opacity: 0.6; }
.qt-muted-inline { color: #7888aa; font-size: 0.82em; margin-left: 0.3em; }

.qt-custom-row { display: flex; align-items: center; gap: 0.4rem; }
.qt-del-btn {
    color: #5a4a4a;
    cursor: pointer;
    padding: 0 4px;
    border-radius: 3px;
    font-size: 0.75rem;
    line-height: 1.6;
    flex-shrink: 0;
}
.qt-del-btn:hover { color: #ff6b6b; background: rgba(255, 80, 80, 0.1); }

.qt-input-row {
    display: flex;
    align-items: center;
    padding: 0.4rem 1rem;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    background: rgba(0, 0, 0, 0.3);
    flex-shrink: 0;
    gap: 0.6rem;
}
.qt-prompt {
    color: var(--qt-accent, var(--primary, #6c8ebf));
    font-weight: bold;
    font-size: 1rem;
    user-select: none;
    transition: color 0.2s;
}
.quake-terminal.qt-chat-mode .qt-prompt {
    color: #78dba4;
}
.qt-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: #e0e8ff;
    font-family: inherit;
    font-size: inherit;
    caret-color: var(--primary, #6c8ebf);
}
.qt-input::placeholder { color: #7888aa; }

.qt-resize-handle {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 6px;
    cursor: ns-resize;
    background: transparent;
    z-index: 1;
    transition: background 0.15s;
}
.qt-resize-handle:hover,
.qt-resize-handle.resizing {
    background: rgba(108, 142, 191, 0.35);
}
.quake-terminal.qt-maximized .qt-resize-handle { display: none; }
/* panel must be position:relative so the handle sits at exact bottom */
.quake-terminal { position: fixed; top: 0; left: 0; width: 100%; z-index: 9999;
    background: rgba(8,10,18,0.97); border-bottom: 2px solid var(--qt-accent, var(--primary,#6c8ebf));
    font-family: 'Courier New',Courier,monospace; font-size: 0.875rem; color: #e0e8ff;
    display: flex; flex-direction: column;
    transform: translateY(-100%);
    transition: transform 0.28s cubic-bezier(0.4,0,0.2,1);
    box-shadow: 0 6px 40px rgba(0,0,0,0.7);
    backdrop-filter: blur(6px);
}

