:root {
    --bg: #0f1417;
    --card: #172026;
    --sidebar: #131c21;
    --border: #26333b;
    --text: #e8eef1;
    --muted: #8fa3ad;
    --accent: #00a884;
    --accent-dark: #008069;
    --danger: #e5534b;
    --warn: #d29922;
}

:root[data-theme="light"] {
    --bg: #f5f7fa;
    --card: #ffffff;
    --sidebar: #e8ecf1;
    --border: #d1d9e0;
    --text: #1f2937;
    --muted: #6b7280;
    --accent: #00a884;
    --accent-dark: #008069;
    --danger: #e5534b;
    --warn: #d29922;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
    font-size: 14px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--sidebar);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.menu-toggle {
    display: none;
    flex-direction: column;
    gap: 4px;
    background: transparent;
    border: none;
    padding: 8px;
    cursor: pointer;
}

.menu-toggle span {
    width: 24px;
    height: 2px;
    background: var(--text);
    border-radius: 2px;
}

.app-header h1 {
    font-size: 18px;
    margin: 0;
    font-weight: 600;
}

.pill {
    padding: 5px 14px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}
.pill-waiting { background: #3a2f12; color: var(--warn); }
.pill-ready   { background: #10352c; color: var(--accent); }
.status-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: middle; box-shadow: 0 0 6px currentColor; }
.pill-ready .status-dot { background: #10b981; color: #10b981; }
.pill-waiting .status-dot { background: #ef4444; color: #ef4444; }

.app-layout {
    display: flex;
    min-height: calc(100vh - 57px);
    position: relative;
}

.sidebar {
    width: 260px;
    background: var(--sidebar);
    border-right: 1px solid var(--border);
    position: sticky;
    top: 57px;
    height: calc(100vh - 57px);
    overflow-y: auto;
    flex-shrink: 0;
}

.nav {
    display: flex;
    flex-direction: column;
    padding: 12px 8px;
    gap: 4px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    background: transparent;
    border: none;
    color: #b9c7cf;
    font-size: 14px;
    font-weight: 500;
    text-align: left;
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.2s ease;
}

.nav-item:hover:not(:disabled) {
    background: var(--card);
    color: var(--text);
}

.nav-item.active {
    background: var(--card);
    color: var(--accent);
    font-weight: 600;
}

.nav-icon {
    font-size: 18px;
    flex-shrink: 0;
}

.nav-label {
    white-space: nowrap;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 90;
}

.main-content {
    flex: 1;
    min-width: 0;
    padding: 16px;
    max-width: 1100px;
    margin: 0 auto;
    width: 100%;
}

#consent-banner {
    margin-bottom: 16px;
    padding: 12px 16px;
    background: #10222b;
    border: 1px solid #1d4251;
    border-radius: 10px;
    color: #a8cfdd;
    font-size: 13px;
}

.card {
    margin-bottom: 16px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

h2 {
    font-size: 16px;
    margin: 0 0 10px;
    font-weight: 600;
}

.muted { color: var(--muted); font-size: 13px; }

label {
    display: block;
    margin: 14px 0 6px;
    font-size: 13px;
    color: var(--muted);
    font-weight: 500;
}

input[type="text"], input:not([type]), textarea, select {
    width: 100%;
    padding: 10px 12px;
    background: #0d1519;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-family: inherit;
    font-size: 14px;
    transition: border-color 0.2s ease;
}

input[type="text"]:focus, input:not([type]):focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--accent);
}

textarea { resize: vertical; font-family: inherit; min-height: 100px; }

input[type="file"] {
    flex: 1;
    min-width: 200px;
    padding: 8px 10px;
    background: #0d1519;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    cursor: pointer;
}

button {
    padding: 10px 18px;
    background: var(--accent);
    border: none;
    border-radius: 8px;
    color: #fff;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s ease;
    white-space: nowrap;
}
button:hover:not(:disabled) { background: var(--accent-dark); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.secondary { background: #2b3a43; }
button.secondary:hover:not(:disabled) { background: #37474f; }
button.danger { background: var(--danger); }
button.danger:hover:not(:disabled) { background: #d63f38; }

.row {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: nowrap;
    margin-top: 14px;
}
.row.wrap {
    flex-wrap: wrap;
}

#qr {
    display: block;
    width: 260px;
    max-width: 100%;
    margin: 16px 0;
    border-radius: 12px;
    background: #fff;
    padding: 10px;
}

.pairing-code {
    font-size: 28px;
    font-weight: 700;
    letter-spacing: 4px;
    color: var(--accent);
    font-family: monospace;
}

.stats {
    display: flex;
    gap: 24px;
    flex-wrap: wrap;
    margin: 16px 0;
}
.stat { font-size: 13px; }
.stat b { display: block; font-size: 22px; font-weight: 700; line-height: 1.2; }
.stat span { color: var(--muted); }

.table-wrapper {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    overflow-x: auto;
    margin-top: 14px;
    border-radius: 8px;
    border: 1px solid var(--border);
}

/* Virtualized tables scroll inside a fixed-height box so only the visible
   window of rows is rendered. */
.table-wrapper.v-scroll {
    max-height: min(62vh, 640px);
    overflow-y: auto;
    overflow-x: auto;
}

.vt-row td {
    height: 46px;
    padding-top: 6px;
    padding-bottom: 6px;
    vertical-align: middle;
}

.vt-row td button,
.vt-row td input {
    min-height: 0;
}

.vt-row td button.tiny {
    padding-top: 5px;
    padding-bottom: 5px;
}

.vt-spacer {
    pointer-events: none;
}

.vt-spacer td {
    padding: 0 !important;
    border: 0 !important;
}

tbody tr.vt-spacer:hover {
    background: transparent;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    min-width: 600px;
}
th, td {
    text-align: left;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
th {
    color: var(--muted);
    font-weight: 600;
    background: #0d1519;
    position: sticky;
    top: 0;
}
tbody tr:hover { background: #1c262c; }
tbody tr:last-child td { border-bottom: none; }

.badge {
    padding: 3px 10px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 600;
    display: inline-block;
}
.badge-granted { background: #10352c; color: var(--accent); }
.badge-unknown { background: #33302a; color: #c9b458; }
.badge-denied  { background: #3a1f1d; color: #e5836f; }
.badge-ok      { background: #10352c; color: var(--accent); }
.badge-off     { background: #3a1f1d; color: #e5836f; }
.badge-wa-yes  { background: #10352c; color: var(--accent); }
.badge-wa-no   { background: #3a1f1d; color: #e5836f; }
.badge-wa-unknown { background: #33302a; color: #c9b458; }

.result { margin-top: 14px; font-size: 13px; }
.result .msg { padding: 10px 14px; border-radius: 8px; margin-top: 8px; }
.msg-info { background: #10222b; color: #a8cfdd; }
.msg-ok   { background: #10352c; color: #7fe0c0; }
.msg-err  { background: #3a1f1d; color: #f0a08f; }
.msg-warn { background: #3a2f12; color: #e8c96a; }

pre.sample {
    background: #0d1519;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 14px;
    white-space: pre-wrap;
    word-break: break-word;
    margin: 10px 0 0;
    font-size: 13px;
    font-family: "Consolas", "Monaco", monospace;
}

.progress-bar {
    height: 10px;
    background: #0d1519;
    border: 1px solid var(--border);
    border-radius: 5px;
    overflow: hidden;
    margin: 14px 0;
}
#progressFill {
    height: 100%;
    width: 0;
    background: linear-gradient(90deg, var(--accent), #00c997);
    transition: width .3s ease;
}

.log {
    max-height: 320px;
    overflow-y: auto;
    font-family: "Consolas", "Monaco", monospace;
    font-size: 12px;
    background: #0d1519;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
}
.log div { padding: 3px 0; }
.log .ok { color: #7fe0c0; }
.log .err { color: #f0a08f; }
.log .skip { color: #c9b458; }

button.tiny {
    padding: 5px 12px;
    font-size: 12px;
    border-radius: 6px;
}

.filters {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
    margin-bottom: 16px;
    padding: 12px;
    background: #0d1519;
    border-radius: 8px;
    border: 1px solid var(--border);
}
.filters input {
    flex: 1 1 200px;
    min-width: 150px;
}
.filters select {
    flex: 0 1 auto;
    width: auto;
    min-width: 130px;
}

.sep {
    border: none;
    border-top: 1px solid var(--border);
    margin: 24px 0;
}

label.inline { margin: 0; }

.notice {
    background: #10222b;
    border: 1px solid #1d4251;
    border-radius: 8px;
    padding: 12px 16px;
    color: #a8cfdd;
    font-size: 13px;
    margin-bottom: 14px;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Responsive Design */

/* Tablets and smaller laptops */
@media (max-width: 1024px) {
    .sidebar {
        width: 220px;
    }
    .nav-label {
        font-size: 13px;
    }
}

/* Tablets */
@media (max-width: 768px) {
    .menu-toggle {
        display: flex;
    }
    
    .sidebar {
        position: fixed;
        left: -280px;
        top: 57px;
        height: calc(100vh - 57px);
        width: 260px;
        transition: left 0.3s ease;
        z-index: 95;
    }
    
    .sidebar.open {
        left: 0;
    }
    
    .sidebar-overlay.active {
        display: block;
    }
    
    .main-content {
        padding: 12px;
        max-width: 100%;
    }
    
    .card {
        padding: 16px;
        border-radius: 10px;
    }
    
    .filters {
        padding: 10px;
    }
    
    .filters input,
    .filters select {
        flex: 1 1 100%;
        width: 100%;
    }
    
    .row {
        flex-direction: column;
        align-items: stretch;
    }
    
    .row button,
    .row input,
    .row select {
        width: 100%;
    }
    
    .row.wrap {
        flex-direction: row;
    }
    
    .row.wrap button {
        flex: 1;
        min-width: 0;
    }
}

/* Mobile phones */
@media (max-width: 480px) {
    .app-header {
        padding: 10px 12px;
    }
    
    .app-header h1 {
        font-size: 16px;
    }
    
    .pill {
        font-size: 11px;
        padding: 4px 10px;
    }
    
    .main-content {
        padding: 10px;
    }
    
    .card {
        padding: 14px;
    }
    
    h2 {
        font-size: 15px;
    }
    
    .stats {
        gap: 16px;
    }
    
    .stat b {
        font-size: 20px;
    }
    
    table {
        font-size: 12px;
        min-width: 500px;
    }
    
    th, td {
        padding: 8px 10px;
    }
    
    .pairing-code {
        font-size: 22px;
        letter-spacing: 2px;
    }
    
    #qr {
        width: 220px;
    }
    
    .row.wrap {
        flex-direction: column;
    }
    
    .row.wrap button {
        width: 100%;
    }
}

/* Tour overlay */
.tour-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 9999;
    display: none;
}
.tour-overlay.active { display: block; }
.tour-overlay[hidden] { display: none !important; }
body.tour-open { overflow: hidden; }
.tour-highlight {
    position: absolute;
    border: 2px solid var(--accent);
    border-radius: 8px;
    box-shadow: 0 0 0 9999px rgba(0,0,0,0.6);
    pointer-events: none;
    z-index: 10000;
    transition: all 0.2s ease;
}
.tour-tooltip {
    position: absolute;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px;
    max-width: 320px;
    z-index: 10001;
    box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}
.tour-tooltip h4 { margin: 0 0 8px; font-size: 15px; }
.tour-tooltip p { margin: 0 0 12px; font-size: 13px; color: var(--muted); }
.tour-tooltip .row { margin-top: 8px; }
.tour-tooltip button { padding: 6px 12px; font-size: 12px; }

.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.account-info {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    line-height: 1.2;
    margin-right: 4px;
}

.account-name {
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.account-plan {
    color: #b9c4d4;
    font-size: 11px;
}

.banner-warn {
    border-left: 4px solid #f0a500;
    background: #fffaf0;
}

.connect-actions {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin: 12px 0;
}

.app-dialog {
    border: 0;
    border-radius: 12px;
    padding: 0;
    width: min(420px, 92vw);
    max-width: 92vw;
    max-height: 90vh;
    overflow-y: auto;
    background: #fff;
    color: #1f2c33;
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.25);
}

.app-dialog form {
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.app-dialog h2 {
    margin: 0 0 8px;
    font-size: 20px;
}

.app-dialog label {
    font-size: 13px;
    font-weight: 600;
    margin-top: 6px;
    color: #3b4a54;
}

.app-dialog input {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
}

.app-dialog .row {
    margin-top: 12px;
    gap: 8px;
}

@media (max-width: 768px) {
    .account-info {
        display: none;
    }
}

/* Toast notifications */
.toast-container { position: fixed; bottom: 20px; right: 20px; z-index: 10002; display: flex; flex-direction: column; gap: 8px; max-width: 360px; }
.toast { padding: 12px 16px; border-radius: 8px; font-size: 13px; color: #fff; box-shadow: 0 4px 12px rgba(0,0,0,0.3); animation: toastIn 0.2s ease; display: flex; align-items: center; gap: 8px; }
.toast-ok { background: #10352c; border-left: 3px solid var(--accent); }
.toast-err { background: #3a1f1d; border-left: 3px solid var(--danger); }
.toast-warn { background: #3a2f12; border-left: 3px solid var(--warn); }
.toast-info { background: #10222b; border-left: 3px solid #5ba4cf; }
.toast.fade-out { animation: toastOut 0.2s ease forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { from { opacity: 1; } to { opacity: 0; transform: translateY(10px); } }
/* Spinner */
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid rgba(255,255,255,0.3); border-top-color: #fff; border-radius: 50%; animation: spin 0.6s linear infinite; vertical-align: middle; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Header: prevent overflow and improve mobile layout */
@media (max-width: 600px) {
    .app-header { flex-wrap: wrap; gap: 8px; }
    .header-left { flex: 1; min-width: 0; }
    .header-right { width: 100%; justify-content: flex-start; gap: 4px; }
    .header-right button.tiny { padding: 4px 6px; font-size: 10px; }
    .account-info { display: none; }
}

/* Sidebar: ensure all items visible and scrollable */
.sidebar {
    overflow-y: auto;
    scrollbar-width: thin;
}
.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Nav items: compact on mobile */
@media (max-width: 768px) {
    .nav-item { padding: 8px 12px; gap: 8px; }
    .nav-icon { font-size: 16px; }
    .nav-label { font-size: 13px; }
}
@media (max-width: 480px) { .toast-container { left: 10px; right: 10px; max-width: none; bottom: 10px; } .toast { font-size: 12px; padding: 10px 12px; } }
@media (max-width: 600px) { .app-header { padding: 8px 10px; } .app-header h1 { font-size: 15px; } .header-right { gap: 4px; } .header-right button.tiny { padding: 4px 8px; font-size: 11px; } .pill { font-size: 10px; padding: 3px 8px; } .status-dot { width: 7px; height: 7px; margin-right: 4px; } }
#pairing-form input, #pairing-form button { width: 100%; margin-top: 8px; }
@media (max-width: 768px) { table { min-width: 400px; font-size: 12px; } th, td { padding: 6px 8px; } .filters { padding: 8px; gap: 6px; } .filters input, .filters select { min-width: 0; } }
@media (max-width: 480px) { .app-dialog { width: 96vw; } .app-dialog form { padding: 16px; gap: 6px; } .app-dialog h2 { font-size: 18px; } .card { padding: 12px; margin-bottom: 10px; } .main-content { padding: 8px; } .connect-actions { flex-direction: column; align-items: stretch; } .connect-actions button { width: 100%; } }
.table-wrapper { -webkit-overflow-scrolling: touch; }
@media (max-width: 768px) { input, select, textarea, button { min-height: 40px; } button.tiny { min-height: 32px; } }
@media (max-width: 360px) { .stats { flex-direction: column; gap: 8px; } .toast-container { left: 5px; right: 5px; } }
@media (max-width: 480px) { .log { max-height: 200px; font-size: 11px; } .progress-bar { height: 8px; } }

/* Header overflow fix */
@media (max-width: 600px) { .app-header { flex-wrap: wrap; gap: 8px; } .header-left { flex: 1; min-width: 0; } .header-right { width: 100%; justify-content: flex-start; gap: 4px; } .header-right button.tiny { padding: 4px 6px; font-size: 10px; } .pill { font-size: 10px; padding: 3px 8px; } }
/* Sidebar scroll */
.sidebar { overflow-y: auto; scrollbar-width: thin; }
.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
@media (max-width: 768px) { .nav-item { padding: 8px 12px; gap: 8px; } .nav-icon { font-size: 16px; } .nav-label { font-size: 13px; } }

/* === Final responsive fixes: prevent squeezed labels / overflow === */
@media (max-width: 768px) {
    .row.wrap button { flex: 1 1 calc(50% - 6px); min-width: 0; white-space: normal; }
    .row.wrap input[type="file"] { flex: 1 1 100%; min-width: 0; width: 100%; }
    .connect-actions button { white-space: normal; }
}
@media (max-width: 480px) {
    .row.wrap button { flex: 1 1 100%; }
}

/* === Light theme: fix controls that use hard-coded dark surfaces === */
:root[data-theme="light"] input[type="text"],
:root[data-theme="light"] input:not([type]),
:root[data-theme="light"] input[type="file"],
:root[data-theme="light"] input[type="number"],
:root[data-theme="light"] input[type="tel"],
:root[data-theme="light"] input[type="email"],
:root[data-theme="light"] input[type="password"],
:root[data-theme="light"] input[type="datetime-local"],
:root[data-theme="light"] textarea,
:root[data-theme="light"] select,
:root[data-theme="light"] .filters,
:root[data-theme="light"] th {
    background: #ffffff;
    color: var(--text);
}
:root[data-theme="light"] .nav-item { color: #4b5563; }
:root[data-theme="light"] tbody tr:hover { background: #eef3f7; }
:root[data-theme="light"] button.secondary { background: #e5eaf0; color: #1f2937; }
:root[data-theme="light"] button.secondary:hover:not(:disabled) { background: #d7dee6; }
:root[data-theme="light"] #consent-banner { background: #e8f4fa; border-color: #b9dbe9; color: #23566b; }
:root[data-theme="light"] .pill-waiting { background: #fef3c7; color: #92400e; }
:root[data-theme="light"] .pill-ready { background: #d1fae5; color: #065f46; }
:root[data-theme="light"] .log { background: #ffffff; color: #1f2937; }
:root[data-theme="light"] .tour-tooltip { background: #ffffff; }
:root[data-theme="light"] .app-dialog input { background: #ffffff; color: #1f2c33; }
