:root {
    --bg: #eef1f8;
    --surface: #ffffff;
    --surface2: #f4f6fb;
    --border: #e2e6ef;
    --accent: #6d28d9;
    --accent-hover: #5b21b6;
    --accent-bg: #f3eeff;
    --ok: #15803d;
    --ok-bg: #ecfdf5;
    --warn: #c2410c;
    --warn-bg: #fff7ed;
    --error: #dc2626;
    --error-bg: #fef2f2;
    --text: #12151f;
    --muted: #64748b;
    --mono: 'IBM Plex Mono', monospace;
    --shadow-sm: 0 1px 2px rgba(15,23,42,0.05);
    --shadow-md: 0 4px 20px rgba(15,23,42,0.08);
    --shadow-lg: 0 20px 50px rgba(15,23,42,0.12);
    --radius: 12px;
    --radius-sm: 8px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0c1017;
        --surface: #151b26;
        --surface2: #1e2533;
        --border: #2a3344;
        --accent: #a78bfa;
        --accent-hover: #c4b5fd;
        --accent-bg: rgba(139,92,246,0.15);
        --ok: #4ade80; --ok-bg: rgba(34,197,94,0.12);
        --warn: #fb923c; --warn-bg: rgba(251,146,60,0.12);
        --error: #f87171; --error-bg: rgba(248,113,113,0.12);
        --text: #e8eaed;
        --muted: #94a3b8;
        --shadow-sm: 0 1px 2px rgba(0,0,0,0.4);
        --shadow-md: 0 4px 22px rgba(0,0,0,0.35);
        --shadow-lg: 0 22px 60px rgba(0,0,0,0.5);
    }
}

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

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: 'IBM Plex Sans', sans-serif;
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
    min-height: 100dvh;
}

/* === Login === */
#login-screen {
    position: fixed; inset: 0;
    background: var(--bg);
    background-image: radial-gradient(1200px 600px at 10% -10%, rgba(124,58,237,0.07), transparent 50%),
                      radial-gradient(800px 400px at 100% 0%, rgba(59,130,246,0.05), transparent 45%);
    display: flex; align-items: center; justify-content: center;
    z-index: 2000; padding: 20px;
}
.login-card {
    background: var(--surface); padding: 40px 28px; border-radius: 20px;
    box-shadow: var(--shadow-lg); width: 100%; max-width: 380px;
    text-align: center; border: 1px solid var(--border); position: relative; overflow: hidden;
}
.login-card::before { content: ''; position: absolute; top: 0; left: 0; right: 0; height: 4px; background: linear-gradient(90deg, var(--accent), #a78bfa); }
.login-icon { font-size: 36px; color: var(--accent); margin-bottom: 16px; }
.login-card h2 { margin: 0 0 8px; font-size: 1.4rem; font-weight: 700; }
.login-sub { margin: 0 0 24px; font-size: 14px; color: var(--muted); }
#login-error { display: none; color: var(--error); font-size: 13px; margin-bottom: 16px; text-align: left; padding: 12px; background: var(--error-bg); border-radius: var(--radius-sm); border: 1px solid rgba(220,38,38,0.15); }
.login-card .input { margin-bottom: 12px; }

/* === Common === */
.input {
    width: 100%; padding: 14px; border: 1px solid var(--border);
    border-radius: var(--radius-sm); font-family: inherit; font-size: 16px;
    background: var(--surface); color: var(--text); transition: all 0.15s;
    -webkit-appearance: none;
}
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(109,40,217,0.15); }
.textarea { resize: vertical; min-height: 80px; font-size: 15px; }
.select-sm { padding: 10px 14px; font-size: 14px; flex-shrink: 0; }

.btn-primary {
    width: 100%; background: linear-gradient(180deg, #7c3aed, var(--accent));
    color: white; border: none; padding: 14px 16px; border-radius: var(--radius-sm);
    cursor: pointer; font-weight: 600; font-size: 15px;
    box-shadow: 0 4px 14px rgba(109,40,217,0.3); transition: all 0.15s;
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(109,40,217,0.4); }

.btn-secondary {
    padding: 14px 20px; background: var(--surface2); color: var(--error);
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    cursor: pointer; font-weight: 600; font-size: 14px; transition: all 0.15s;
    display: inline-flex; align-items: center; gap: 8px;
}
.btn-secondary:hover { background: var(--error-bg); border-color: var(--error); }

.btn-sm {
    padding: 8px 14px; font-size: 13px; font-weight: 600;
    background: var(--surface2); color: var(--muted); border: 1px solid var(--border);
    border-radius: 6px; cursor: pointer; transition: all 0.15s;
    display: inline-flex; align-items: center; gap: 6px;
}
.btn-sm:hover { color: var(--accent); background: var(--accent-bg); border-color: rgba(109,40,217,0.3); }

/* === App Header === */
.app-header {
    background: linear-gradient(180deg, var(--surface) 0%, var(--surface2) 100%);
    border-bottom: 1px solid var(--border);
    padding: 14px 16px; display: flex; align-items: center;
    justify-content: space-between; position: sticky; top: 0; z-index: 100;
    box-shadow: var(--shadow-sm);
}
.header-title {
    margin: 0; font-size: 18px; font-weight: 700; letter-spacing: 0.04em;
    color: var(--accent); display: flex; align-items: center; gap: 10px;
}
.header-right { display: flex; gap: 8px; }
.btn-header {
    width: 40px; height: 40px; border-radius: var(--radius-sm);
    border: 1px solid var(--border); background: var(--surface2);
    color: var(--muted); cursor: pointer; display: flex; align-items: center;
    justify-content: center; font-size: 16px; transition: all 0.15s;
}
.btn-header:hover { color: var(--accent); background: var(--accent-bg); }
.btn-header.btn-scan-qr { background: linear-gradient(145deg, #7c3aed, var(--accent)); color: white; border-color: var(--accent); }
.btn-header.btn-scan-qr:hover { background: linear-gradient(145deg, #6d28d9, #5b21b6); color: white; }
.btn-header.btn-danger:hover { color: var(--error); background: var(--error-bg); }
.qr-scan-modal { width: min(400px, 95%); }
.qr-scan-modal .modal-body-inner { background: #000; }
#qr-scan-reader { width: 100%; min-height: 300px; }
#qr-scan-reader video { width: 100% !important; }
#qr-scan-reader img { display: none !important; }

/* === Filter Bar === */
.filter-bar { padding: 12px 16px; display: flex; flex-direction: column; gap: 10px; }
.search-wrap { position: relative; }
.search-icon { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 14px; }
.search-input { padding-left: 40px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
    padding: 6px 14px; border-radius: 999px; font-size: 12px; font-weight: 600;
    border: 1px solid var(--border); background: var(--surface);
    color: var(--muted); cursor: pointer; transition: all 0.15s;
    display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.chip:hover, .chip.active { background: var(--accent-bg); color: var(--accent); border-color: var(--accent); }
.chip i { font-size: 11px; }

/* === Card Grid === */
.card-grid { padding: 0 16px 100px; display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .card-grid { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1000px) { .card-grid { grid-template-columns: repeat(3, 1fr); } }

.device-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 16px; padding: 0; cursor: pointer;
    transition: all 0.25s cubic-bezier(0.22,1,0.36,1); box-shadow: var(--shadow-sm);
    display: flex; flex-direction: column; overflow: hidden; position: relative;
}
.device-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-3px); border-color: var(--accent); }
.device-card::before {
    content: ''; position: absolute; top: 0; left: 0; right: 0; height: 3px;
    background: linear-gradient(90deg, var(--accent), #a78bfa); opacity: 0;
    transition: opacity 0.25s;
}
.device-card:hover::before { opacity: 1; }

.card-top {
    display: flex; align-items: center; gap: 14px; padding: 16px 16px 12px;
}
.card-icon {
    width: 48px; height: 48px; border-radius: 14px; flex-shrink: 0;
    background: linear-gradient(145deg, var(--accent-bg), rgba(109,40,217,0.08));
    color: var(--accent); border: 1px solid rgba(109,40,217,0.12);
    display: flex; align-items: center; justify-content: center; font-size: 20px;
    transition: all 0.2s;
}
.device-card:hover .card-icon {
    background: linear-gradient(145deg, var(--accent), #7c3aed);
    color: white; border-color: var(--accent);
    box-shadow: 0 4px 12px rgba(109,40,217,0.3);
}
.card-header { min-width: 0; flex: 1; }
.card-name {
    font-weight: 700; font-size: 16px; margin-bottom: 2px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    letter-spacing: -0.01em;
}
.card-badge {
    display: inline-flex; padding: 3px 10px; border-radius: 999px;
    font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
    background: var(--accent-bg); color: var(--accent); border: 1px solid rgba(109,40,217,0.15);
}

.card-details {
    padding: 0 16px 14px; display: flex; flex-direction: column; gap: 6px;
}
.card-detail-row {
    display: flex; align-items: center; gap: 8px;
    font-size: 13px; color: var(--muted);
}
.card-detail-row i { width: 16px; text-align: center; font-size: 11px; opacity: 0.7; }
.card-detail-row span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.card-footer {
    padding: 10px 16px; border-top: 1px solid var(--border);
    background: var(--surface2); display: flex; align-items: center;
    justify-content: space-between; font-size: 11px; color: var(--muted);
}
.card-footer-nets { display: flex; align-items: center; gap: 4px; }
.card-footer-nets i { font-size: 10px; }
.card-arrow { font-size: 14px; color: var(--border); transition: all 0.2s; }
.device-card:hover .card-arrow { color: var(--accent); transform: translateX(2px); }

/* === Empty State === */
.empty-state { text-align: center; padding: 80px 20px; color: var(--muted); }
.empty-state i { font-size: 56px; margin-bottom: 20px; opacity: 0.4; display: block; }
.empty-state p { font-size: 16px; font-weight: 500; }
.empty-state .empty-hint { font-size: 13px; color: var(--muted); margin-top: 8px; opacity: 0.7; }

/* === FAB === */
.fab {
    position: fixed; bottom: 24px; right: 24px; z-index: 200;
    width: 60px; height: 60px; border-radius: 50%;
    background: linear-gradient(145deg, #7c3aed, var(--accent));
    color: white; border: none; font-size: 24px; cursor: pointer;
    box-shadow: 0 8px 24px rgba(109,40,217,0.45), 0 2px 6px rgba(109,40,217,0.2);
    transition: all 0.25s cubic-bezier(0.22,1,0.36,1);
    display: flex; align-items: center; justify-content: center;
}
.fab:hover { transform: scale(1.1) translateY(-2px); box-shadow: 0 12px 32px rgba(109,40,217,0.55); }
.fab:active { transform: scale(0.95); }

/* === Form View === */
.form-header {
    padding: 12px 16px; display: flex; align-items: center; justify-content: space-between;
    border-bottom: 1px solid var(--border); background: var(--surface); position: sticky; top: 56px; z-index: 50;
}
.form-header h2 { margin: 0; font-size: 18px; font-weight: 700; }
.btn-back {
    background: none; border: none; color: var(--accent); cursor: pointer;
    font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 6px;
    padding: 8px 0;
}
.detail-actions { display: flex; gap: 8px; }

.device-form { padding: 20px 16px 100px; max-width: 600px; }
.form-label { display: block; font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--muted); letter-spacing: 0.05em; margin: 16px 0 6px; }
.form-label:first-of-type { margin-top: 0; }
.device-form .input { margin-bottom: 0; }

/* === Type Grid === */
.type-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 8px; }
@media (min-width: 400px) { .type-grid { grid-template-columns: repeat(5, 1fr); } }
.type-tile {
    display: flex; flex-direction: column; align-items: center; gap: 6px;
    padding: 12px 4px; border-radius: var(--radius-sm); border: 2px solid var(--border);
    background: var(--surface2); cursor: pointer; transition: all 0.15s; color: var(--muted);
}
.type-tile:hover { border-color: var(--accent); color: var(--accent); }
.type-tile.active { border-color: var(--accent); background: var(--accent-bg); color: var(--accent); }
.type-tile i { font-size: 20px; }
.type-tile span { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; }

/* === Photo Upload === */
.photo-upload { margin-bottom: 0; }
.photo-preview {
    border: 2px dashed var(--border); border-radius: var(--radius);
    padding: 24px; text-align: center; cursor: pointer; color: var(--muted);
    transition: all 0.15s; display: flex; flex-direction: column; align-items: center; gap: 8px;
    min-height: 80px; justify-content: center;
}
.photo-preview:hover { border-color: var(--accent); color: var(--accent); }
.photo-preview i { font-size: 24px; }
.photo-preview span { font-size: 13px; }
.photo-preview img { max-width: 100%; max-height: 200px; border-radius: var(--radius-sm); object-fit: contain; }

/* === Scanner === */
#scanner-container { margin: 8px 0; border-radius: var(--radius-sm); overflow: hidden; min-height: 250px; background: #000; position: relative; }
#scanner-container video { width: 100% !important; border-radius: var(--radius-sm); }
#scanner-container img { display: none !important; }
.scanner-hint {
    position: absolute; top: 10px; left: 0; right: 0; z-index: 10; margin: 0;
    text-align: center; font-size: 12px; font-weight: 600; color: rgba(255,255,255,0.92);
    text-shadow: 0 1px 4px rgba(0,0,0,0.8); pointer-events: none;
}
.scanner-stop-btn {
    position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); z-index: 10;
    padding: 10px 24px; background: rgba(0,0,0,0.7); color: white; border: 1px solid rgba(255,255,255,0.3);
    border-radius: 999px; font-size: 13px; font-weight: 600; cursor: pointer; backdrop-filter: blur(4px);
}

.input-with-btn { display: flex; gap: 8px; }
.input-with-btn .input { flex: 1; }
.btn-scan {
    width: 50px; flex-shrink: 0; border: 1px solid var(--border); background: var(--surface2);
    border-radius: var(--radius-sm); color: var(--muted); cursor: pointer; font-size: 18px;
    display: flex; align-items: center; justify-content: center; transition: all 0.15s;
}
.btn-scan:hover { color: var(--accent); background: var(--accent-bg); border-color: var(--accent); }

/* === Network Assignments === */
.form-section { margin-top: 24px; }
.form-section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.form-section-head h3 { margin: 0; font-size: 14px; font-weight: 700; color: var(--text); }

.net-row {
    background: var(--surface2); border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 12px; margin-bottom: 8px; position: relative;
}
.net-row select, .net-row input { margin-bottom: 8px; font-size: 14px; padding: 10px; }
.net-row-remove {
    position: absolute; top: 8px; right: 8px; width: 28px; height: 28px;
    border: none; background: transparent; color: var(--muted); cursor: pointer; font-size: 14px;
    display: flex; align-items: center; justify-content: center; border-radius: 4px;
}
.net-row-remove:hover { color: var(--error); background: var(--error-bg); }

.form-actions { display: flex; gap: 12px; margin-top: 24px; padding-bottom: 20px; }
.form-actions .btn-primary { flex: 1; }

/* === Detail View === */
#detail-content { padding: 20px 16px 100px; max-width: 600px; }
.detail-photo { width: 100%; max-height: 300px; object-fit: contain; border-radius: 16px; margin-bottom: 20px; border: 1px solid var(--border); background: var(--surface2); box-shadow: var(--shadow-md); }
.detail-name { font-size: 24px; font-weight: 700; margin: 0 0 6px; letter-spacing: -0.02em; }
.detail-type-badge {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 5px 16px; border-radius: 999px; font-size: 11px; font-weight: 700;
    text-transform: uppercase; letter-spacing: 0.03em;
    background: linear-gradient(145deg, var(--accent-bg), rgba(109,40,217,0.08));
    color: var(--accent); border: 1px solid rgba(109,40,217,0.15);
    margin-bottom: 20px;
}
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 20px; }
.detail-tile {
    background: var(--surface2); padding: 14px 16px; border-radius: var(--radius);
    border: 1px solid var(--border); transition: all 0.15s;
}
.detail-tile small { color: var(--muted); font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; display: block; margin-bottom: 4px; }
.detail-tile strong { font-size: 14px; word-break: break-word; line-height: 1.4; }
.detail-tile--wide { grid-column: span 2; }
.detail-nets { margin-top: 16px; }
.detail-nets h3 { font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--muted); letter-spacing: 0.05em; margin: 0 0 10px; }
.detail-net-card { background: var(--surface2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; margin-bottom: 8px; }
.detail-net-name { font-weight: 700; margin-bottom: 4px; }
.detail-net-meta { font-size: 12px; color: var(--muted); font-family: var(--mono); }

/* === Modal === */
.modal-bg {
    position: fixed; inset: 0; background: rgba(15,23,42,0.4);
    backdrop-filter: blur(8px); display: flex; align-items: center; justify-content: center;
    z-index: 1000; padding: 16px; opacity: 0; visibility: hidden; pointer-events: none;
    transition: all 0.25s;
}
.modal-bg.open { opacity: 1; visibility: visible; pointer-events: auto; }
.modal {
    background: var(--surface); border-radius: 18px; width: min(500px, 100%);
    max-height: 85vh; overflow: hidden; display: flex; flex-direction: column;
    box-shadow: var(--shadow-lg); border: 1px solid var(--border);
    transform: translateY(20px); opacity: 0; transition: all 0.3s cubic-bezier(0.22,1,0.36,1);
}
.modal-bg.open .modal { transform: none; opacity: 1; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 18px; font-weight: 700; }
.modal-close { width: 36px; height: 36px; border: none; border-radius: 8px; background: var(--surface2); color: var(--muted); cursor: pointer; font-size: 20px; display: flex; align-items: center; justify-content: center; }
.modal-close:hover { background: var(--border); color: var(--text); }
.modal-body-inner { padding: 20px; overflow-y: auto; flex: 1; }
.modal-hint { font-size: 13px; color: var(--muted); margin: 0 0 16px; }

.label-select-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.label-device-list { max-height: 300px; overflow-y: auto; margin-bottom: 16px; }
.label-device-item { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.label-device-item:last-child { border-bottom: none; }
.label-device-item input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }

/* === Toast === */
#toast-host { position: fixed; bottom: 90px; left: 16px; right: 16px; z-index: 4000; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast { pointer-events: auto; padding: 14px 18px; border-radius: var(--radius-sm); box-shadow: var(--shadow-lg); font-size: 13px; font-weight: 600; border: 1px solid var(--border); background: var(--surface); color: var(--text); animation: toast-in 0.25s; }
.toast-ok { background: var(--ok-bg); color: var(--ok); border-color: rgba(34,197,94,0.2); }
.toast-err { background: var(--error-bg); color: var(--error); border-color: rgba(220,38,38,0.15); }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
