:root {
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-light: #eef2ff;
    --success: #10b981;
    --success-light: #d1fae5;
    --danger: #ef4444;
    --danger-light: #fee2e2;
    --warning: #f59e0b;
    --warning-light: #fef3c7;
    --info: #3b82f6;
    --info-light: #dbeafe;
    --bg: #f1f5f9;
    --white: #ffffff;
    --sidebar-bg: #1e293b;
    --sidebar-hover: #334155;
    --sidebar-text: #94a3b8;
    --sidebar-active: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-secondary: #64748b;
    --radius: 8px;
    --radius-lg: 12px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
    --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
    --sidebar-w: 260px;
    --topbar-h: 60px;
    --transition: .2s ease;
}

/* === Reset === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; min-height: 100vh; }
a { text-decoration: none; color: inherit; }
img { max-width: 100%; }
input, select, textarea, button { font: inherit; }

/* === Login Page === */
.login-container {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 1rem;
}
.login-card {
    background: var(--white); border-radius: var(--radius-lg); padding: 2.5rem;
    width: 100%; max-width: 420px; box-shadow: var(--shadow-lg);
}
.login-card h2 { text-align: center; margin-bottom: .25rem; font-size: 1.6rem; }
.login-card .subtitle { text-align: center; color: var(--text-secondary); margin-bottom: 2rem; font-size: .9rem; }
.login-logo { text-align: center; font-size: 3rem; margin-bottom: .5rem; animation: slideUp .4s ease; }
.login-footer {
    display: flex; align-items: center; justify-content: center;
    gap: .75rem; margin-top: 1.5rem; padding-top: 1rem;
    border-top: 1px solid var(--border);
}
.login-divider { color: var(--border); font-size: .85rem; }
.btn-lg { padding: .75rem 1.25rem; font-size: 1rem; }

/* === Filter Tabs === */
.filter-tabs {
    display: flex; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap;
}
.filter-tab {
    padding: .5rem 1.2rem; border-radius: 999px; font-size: .85rem; font-weight: 500;
    cursor: pointer; transition: var(--transition); border: 1px solid var(--border);
    background: var(--white); color: var(--text-secondary);
}
.filter-tab:hover { border-color: var(--primary); color: var(--primary); }
.filter-tab.active {
    background: var(--primary); color: #fff; border-color: var(--primary);
}

/* === App Layout === */
.app-layout { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
    width: var(--sidebar-w); background: var(--sidebar-bg); color: var(--sidebar-text);
    display: flex; flex-direction: column; position: fixed; top: 0; left: 0; bottom: 0;
    z-index: 100; transition: transform .3s ease;
}
.sidebar-header {
    padding: 1.5rem; border-bottom: 1px solid rgba(255,255,255,.08);
}
.sidebar-header h3 { color: #fff; font-size: 1.3rem; letter-spacing: 1px; }
.sidebar-header .subtitle { font-size: .8rem; color: var(--sidebar-text); }
.sidebar-nav { flex: 1; padding: 1rem 0; overflow-y: auto; }
.nav-item {
    display: flex; align-items: center; gap: .75rem;
    padding: .75rem 1.5rem; cursor: pointer; transition: var(--transition);
    color: var(--sidebar-text); font-size: .95rem; border: none; background: none; width: 100%; text-align: left;
}
.nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; border-radius: 0; }
.nav-item .icon { font-size: 1.2rem; width: 1.5rem; text-align: center; }
.sidebar-footer { padding: 1rem 1.5rem; border-top: 1px solid rgba(255,255,255,.08); }

/* Main Content */
.main-content { flex: 1; margin-left: var(--sidebar-w); display: flex; flex-direction: column; min-height: 100vh; }
.topbar {
    height: var(--topbar-h); background: var(--white); border-bottom: 1px solid var(--border);
    display: flex; align-items: center; padding: 0 1.5rem; gap: 1rem;
    position: sticky; top: 0; z-index: 50;
}
.topbar h2 { font-size: 1.15rem; flex: 1; }
.topbar-user { font-size: .85rem; color: var(--text-secondary); }
.hamburger {
    display: none; background: none; border: none; font-size: 1.5rem;
    cursor: pointer; color: var(--text); padding: .25rem;
}
.content-area { padding: 1.5rem; flex: 1; }

/* === Stats Grid === */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.stat-card {
    background: var(--white); border-radius: var(--radius); padding: 1.25rem;
    box-shadow: var(--shadow-sm); text-align: center;
}
.stat-value { font-size: 1.8rem; font-weight: 700; color: var(--primary); }
.stat-label { font-size: .85rem; color: var(--text-secondary); margin-top: .25rem; }

/* === Toolbar === */
.toolbar {
    display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem;
    flex-wrap: wrap;
}
.search-box { flex: 1; min-width: 200px; }
.search-box input {
    width: 100%; padding: .6rem 1rem; border: 1px solid var(--border);
    border-radius: var(--radius); background: var(--white); outline: none;
    transition: var(--transition);
}
.search-box input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,.15); }
.toolbar-actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* === Buttons === */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
    padding: .55rem 1.1rem; border-radius: var(--radius); font-weight: 500;
    font-size: .9rem; cursor: pointer; transition: var(--transition);
    border: 1px solid transparent; white-space: nowrap; line-height: 1.4;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-secondary { background: var(--bg); color: var(--text); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #dc2626; }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: #059669; }
.btn-outline { background: transparent; color: var(--sidebar-text); border-color: rgba(255,255,255,.2); }
.btn-outline:hover { background: rgba(255,255,255,.1); color: #fff; }
.btn-sm { padding: .35rem .7rem; font-size: .8rem; }
.btn-block { width: 100%; }
.btn-link { background: none; border: none; color: var(--primary); padding: 0; text-decoration: underline; cursor: pointer; font-size: .85rem; }

/* === Forms === */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-weight: 500; margin-bottom: .35rem; font-size: .9rem; }
.form-group input,
.form-group select,
.form-group textarea {
    width: 100%; padding: .6rem .85rem; border: 1px solid var(--border);
    border-radius: var(--radius); outline: none; transition: var(--transition);
    background: var(--white);
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,.15); }
.form-group textarea { resize: vertical; min-height: 80px; }
.form-row { display: flex; gap: 1rem; }
.form-row .form-group { flex: 1; }
.form-hint { font-size: .8rem; color: var(--text-secondary); margin-top: .25rem; }
.form-card {
    background: var(--white); border-radius: var(--radius-lg); padding: 2rem;
    box-shadow: var(--shadow-sm); max-width: 500px;
}
.form-card h3 { margin-bottom: 1.5rem; }
.inline-code {
    display: flex; align-items: center; gap: .5rem;
}
.inline-code input { flex: 1; }

/* === Tables === */
.table-container { background: var(--white); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); overflow: hidden; }
.table { width: 100%; border-collapse: collapse; }
.table thead { background: var(--bg); }
.table th {
    padding: .75rem 1rem; text-align: left; font-size: .8rem; font-weight: 600;
    color: var(--text-secondary); text-transform: uppercase; letter-spacing: .5px;
    white-space: nowrap;
}
.table td { padding: .7rem 1rem; border-bottom: 1px solid var(--border); font-size: .9rem; vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: rgba(79,70,229,.02); }
.table input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; accent-color: var(--primary); }
.key-code {
    font-family: "SF Mono", "Cascadia Code", "Consolas", monospace;
    font-size: .85rem; color: var(--primary-dark); cursor: pointer;
    background: var(--primary-light); padding: .15rem .4rem; border-radius: 4px;
}
.key-code:hover { background: #ddd6fe; }
.actions-cell { display: flex; gap: .35rem; flex-wrap: wrap; }
.empty-state { text-align: center; padding: 3rem 1rem; color: var(--text-secondary); }
.empty-state .icon { font-size: 3rem; margin-bottom: .5rem; }

/* Attributes tags */
.attrs-tags { display: flex; flex-wrap: wrap; gap: .25rem; max-width: 220px; }
.attr-tag {
    display: inline-flex; align-items: center; gap: .2rem;
    padding: .1rem .45rem; border-radius: 4px; font-size: .75rem;
    background: var(--info-light); color: #1e40af; font-family: monospace;
    white-space: nowrap; max-width: 180px; overflow: hidden; text-overflow: ellipsis;
}
.attr-tag .attr-key { font-weight: 600; }
.attr-tag .attr-val { opacity: .8; }

/* Pagination */
.pagination {
    display: flex; align-items: center; justify-content: center;
    gap: .35rem; padding: 1rem; border-top: 1px solid var(--border);
}
.pagination button {
    padding: .35rem .7rem; border-radius: var(--radius); font-size: .85rem;
    border: 1px solid var(--border); background: var(--white); cursor: pointer;
    transition: var(--transition); color: var(--text); min-width: 32px;
}
.pagination button:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.pagination button.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.pagination button:disabled { opacity: .4; cursor: not-allowed; }
.pagination .page-info { font-size: .8rem; color: var(--text-secondary); margin: 0 .5rem; }

/* Custom filter row */
.custom-filter-row {
    display: flex; align-items: center; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap;
}
.custom-filter-row select,
.custom-filter-row input {
    padding: .45rem .7rem; border: 1px solid var(--border); border-radius: var(--radius);
    font-size: .85rem; outline: none; background: var(--white); transition: var(--transition);
}
.custom-filter-row select:focus,
.custom-filter-row input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,.15); }
.custom-filter-row select { min-width: 120px; }
.custom-filter-row input { min-width: 140px; flex: 1; max-width: 240px; }
.custom-filter-row .filter-label { font-size: .85rem; color: var(--text-secondary); white-space: nowrap; }

/* === Badges === */
.badge {
    display: inline-flex; align-items: center; padding: .2rem .6rem;
    border-radius: 999px; font-size: .75rem; font-weight: 600; white-space: nowrap;
}
.badge-success { background: var(--success-light); color: #065f46; }
.badge-danger { background: var(--danger-light); color: #991b1b; }
.badge-warning { background: var(--warning-light); color: #92400e; }
.badge-info { background: var(--info-light); color: #1e40af; }
.badge-secondary { background: var(--bg); color: var(--text-secondary); }

/* === Modals === */
.modal {
    display: none; position: fixed; inset: 0; z-index: 200;
    align-items: center; justify-content: center; padding: 1rem;
}
.modal.active { display: flex; }
.modal-overlay {
    position: absolute; inset: 0; background: rgba(0,0,0,.45);
    animation: fadeIn .2s ease;
}
.modal-dialog {
    position: relative; background: var(--white); border-radius: var(--radius-lg);
    width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto;
    box-shadow: var(--shadow-lg); animation: slideUp .25s ease;
}
.modal-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 1.25rem 1.5rem; border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 1.1rem; }
.modal-close {
    background: none; border: none; font-size: 1.4rem; cursor: pointer;
    color: var(--text-secondary); padding: 0; line-height: 1;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 1.5rem; }
.modal-footer {
    display: flex; justify-content: flex-end; gap: .5rem;
    padding: 1rem 1.5rem; border-top: 1px solid var(--border);
}

/* New key display */
.new-key-display {
    text-align: center; padding: 1.5rem; background: var(--success-light);
    border-radius: var(--radius); margin-top: 1rem;
}
.new-key-display .key { font-size: 1.2rem; font-family: monospace; font-weight: 700; color: #065f46; margin: .5rem 0; }

/* === Toast === */
#toast-container {
    position: fixed; top: 1rem; right: 1rem; z-index: 999;
    display: flex; flex-direction: column; gap: .5rem; pointer-events: none;
}
.toast {
    pointer-events: auto; display: flex; align-items: center; gap: .75rem;
    padding: .85rem 1.25rem; border-radius: var(--radius); color: #fff;
    font-size: .9rem; font-weight: 500; animation: slideIn .3s ease;
    box-shadow: var(--shadow-lg); min-width: 280px; max-width: 420px;
}
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-warning { background: var(--warning); color: #78350f; }
.toast-info { background: var(--info); }

/* Machines list */
.machine-list { display: flex; flex-direction: column; gap: .15rem; }
.machine-item { font-size: .8rem; font-family: monospace; color: var(--text-secondary); }

/* === Responsive === */
@media (max-width: 768px) {
    html { font-size: 14px; }
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); }
    .sidebar-backdrop {
        display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 99;
    }
    .sidebar-backdrop.show { display: block; }
    .main-content { margin-left: 0; }
    .hamburger { display: block; }
    .content-area { padding: 1rem; }
    .topbar { padding: 0 1rem; }

    /* responsive table */
    .table thead { display: none; }
    .table, .table tbody { display: block; }
    .table tbody tr {
        display: block; margin-bottom: .75rem; background: var(--white);
        border-radius: var(--radius); padding: .85rem 1rem;
        border: 1px solid var(--border);
    }
    .table td {
        display: flex; justify-content: space-between; align-items: flex-start;
        padding: .35rem 0; border: none; border-bottom: 1px solid var(--bg);
        gap: .5rem;
    }
    .table td:last-child { border-bottom: none; }
    .table td::before {
        content: attr(data-label); font-weight: 600; font-size: .8rem;
        color: var(--text-secondary); flex-shrink: 0; min-width: 80px;
    }
    .table td.checkbox-cell { justify-content: flex-start; }
    .table td.checkbox-cell::before { display: none; }
    .actions-cell { justify-content: flex-end; }
    .table-container { background: transparent; box-shadow: none; }

    .form-row { flex-direction: column; gap: 0; }
    .toolbar { flex-direction: column; align-items: stretch; }
    .toolbar-actions { justify-content: stretch; }
    .toolbar-actions .btn { flex: 1; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .filter-tabs { gap: .35rem; }
    .filter-tab { padding: .4rem .8rem; font-size: .8rem; flex: 1; text-align: center; }
    .modal-dialog { max-height: 85vh; margin: auto; }
}

/* === Animations === */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
@keyframes slideIn { from { opacity: 0; transform: translateX(60px); } to { opacity: 1; transform: translateX(0); } }

/* === Utilities === */
.hidden { display: none !important; }
.text-center { text-align: center; }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.text-secondary { color: var(--text-secondary); }
.mt-1 { margin-top: .5rem; }
.mt-2 { margin-top: 1rem; }
.mb-1 { margin-bottom: .5rem; }
.mb-2 { margin-bottom: 1rem; }
.gap-05 { gap: .5rem; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
