:root {
    --bg: #0f1117;
    --bg-card: #1a1b23;
    --bg-card-hover: #22232d;
    --bg-input: #13141b;
    --border: #2a2b35;
    --text: #e4e4e7;
    --text-muted: #71717a;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --green: #22c55e;
    --red: #ef4444;
    --yellow: #eab308;
    --orange: #f59e0b;
    --purple: #a855f7;
    --radius: 12px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    font-size: 15px;
    line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85em;
       background: var(--bg-input); padding: 1px 6px; border-radius: 4px; border: 1px solid var(--border); }
h1 { font-size: 1.5rem; font-weight: 650; }
h3 { font-size: 1rem; font-weight: 600; margin-bottom: 10px; }
h4 { font-size: .9rem; font-weight: 600; margin: 16px 0 6px; }
.muted { color: var(--text-muted); }
.small { font-size: .82rem; }
.prewrap { white-space: pre-wrap; }
[hidden] { display: none !important; }

/* ---- Login ---- */
.login-page { display: flex; align-items: center; justify-content: center; }
.login-container { width: 100%; max-width: 400px; padding: 20px; }
.login-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px; padding: 40px 32px; }
.login-header { text-align: center; margin-bottom: 32px; }
.login-header h1 { font-size: 1.8rem; margin-bottom: 8px; }
.login-header p { color: var(--text-muted); font-size: .9rem; }

/* ---- Layout ---- */
.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 24px;
    padding: 0 24px; height: 58px;
    background: rgba(19, 20, 27, .92); backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--border);
}
.brand { color: var(--text); font-weight: 700; font-size: 1.1rem; display: flex; align-items: center; gap: 8px; }
.brand:hover { color: var(--text); }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a { color: var(--text-muted); padding: 6px 12px; border-radius: 8px; font-size: .92rem; }
.nav a:hover { color: var(--text); background: var(--bg-card); }
.nav a.active { color: var(--text); background: var(--bg-card); }
.logout { color: var(--text-muted); font-size: .9rem; }
.logout:hover { color: var(--red); }
.container { max-width: 1280px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 20px; }

/* ---- Forms & buttons ---- */
input, select, textarea {
    width: 100%; background: var(--bg-input); color: var(--text);
    border: 1px solid var(--border); border-radius: 8px;
    padding: 9px 12px; font: inherit; font-size: .92rem; outline: none;
    transition: border-color .15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; }
input[type=checkbox] { width: auto; accent-color: var(--accent); }
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: .82rem; color: var(--text-muted); margin-bottom: 6px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.span-2 { grid-column: span 2; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 9px 16px; border-radius: 8px; border: 1px solid var(--border);
    background: var(--bg-card); color: var(--text); font: inherit; font-size: .9rem; font-weight: 500;
    cursor: pointer; white-space: nowrap; transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--bg-card-hover); color: var(--text); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); color: #fff; }
.btn-danger { color: var(--red); }
.btn-danger:hover { background: rgba(239, 68, 68, .12); border-color: var(--red); color: var(--red); }
.btn-full { width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.icon-btn { background: none; border: none; color: var(--text-muted); font-size: 1.1rem; cursor: pointer; padding: 4px 8px; }
.icon-btn:hover { color: var(--text); }

.alert { padding: 12px 14px; border-radius: 8px; margin-bottom: 18px; font-size: .9rem; }
.alert-error { background: rgba(239, 68, 68, .1); border: 1px solid rgba(239, 68, 68, .35); color: #fca5a5; }

/* ---- Stats ---- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
        padding: 14px 18px; display: flex; flex-direction: column; }
.stat-value { font-size: 1.5rem; font-weight: 700; }
.stat-label { color: var(--text-muted); font-size: .8rem; }

/* ---- Toolbar ---- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }
.toolbar .search { flex: 1 1 260px; }
.toolbar select { width: auto; flex: 0 1 auto; }
.check { display: flex; align-items: center; gap: 6px; font-size: .88rem; color: var(--text-muted); cursor: pointer; white-space: nowrap; }
.spacer { flex: 1; }
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { background: var(--bg-card); color: var(--text-muted); border: none; padding: 8px 12px; cursor: pointer; font-size: 1rem; }
.seg button.active { background: var(--bg-card-hover); color: var(--text); }
.result-info { font-size: .82rem; margin-bottom: 12px; min-height: 1.2em; }

/* ---- Grid ---- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px; }
.card {
    position: relative; display: flex; flex-direction: column;
    background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
    overflow: hidden; color: var(--text); transition: border-color .15s, transform .15s;
}
.card:hover { border-color: #3a3b48; color: var(--text); transform: translateY(-1px); }
.card-img { aspect-ratio: 4 / 3; background: #121319; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.card-img img { width: 100%; height: 100%; object-fit: contain; }
.card-img .ph { font-size: 2.4rem; opacity: .35; }
.card-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.card-title { font-weight: 600; font-size: .93rem; line-height: 1.3; }
.card-sub { color: var(--text-muted); font-size: .8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: auto; padding-top: 6px; }
.qty-badge {
    position: absolute; top: 8px; right: 8px; min-width: 30px; text-align: center;
    background: rgba(15, 17, 23, .85); border: 1px solid var(--border);
    padding: 2px 9px; border-radius: 999px; font-weight: 700; font-size: .85rem;
}
.qty-badge.zero { color: var(--red); border-color: rgba(239, 68, 68, .5); }

/* ---- Chips & badges ---- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0; }
.chip { display: inline-block; font-size: .75rem; padding: 2px 9px; border-radius: 999px;
        background: var(--bg-card-hover); border: 1px solid var(--border); color: var(--text-muted); white-space: nowrap; }
a.chip:hover { color: var(--text); }
.chip-tag { color: #93c5fd; }
.status-available { color: var(--green); border-color: rgba(34, 197, 94, .35); }
.status-in_use { color: var(--accent); border-color: rgba(59, 130, 246, .4); }
.status-broken { color: var(--red); border-color: rgba(239, 68, 68, .4); }
.status-ordered { color: var(--yellow); border-color: rgba(234, 179, 8, .4); }
.badge { font-size: .72rem; padding: 1px 8px; border-radius: 6px; background: var(--bg-card-hover); color: var(--text-muted); white-space: nowrap; }
.badge-src { margin-left: auto; }
.delta { font-weight: 700; font-size: .85rem; }
.delta.pos { color: var(--green); }
.delta.neg { color: var(--red); }

/* ---- Table ---- */
.table-wrap { overflow-x: auto; background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); }
table.inv { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.inv th { text-align: left; font-weight: 500; color: var(--text-muted); font-size: .78rem; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.inv td { padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.inv tr:last-child td { border-bottom: none; }
table.inv tr:hover td { background: var(--bg-card-hover); }
table.inv .t-img { width: 48px; }
table.inv .t-img img, table.inv .t-img .ph { width: 40px; height: 40px; object-fit: contain; border-radius: 6px; background: #121319; display: flex; align-items: center; justify-content: center; }
table.inv a { color: var(--text); font-weight: 500; }
table.inv a:hover { color: var(--accent); }
.t-qty { white-space: nowrap; }
.t-qty span { display: inline-block; min-width: 28px; text-align: center; font-weight: 700; }
.t-qty span.zero { color: var(--red); }
.mini-btn { background: var(--bg-input); color: var(--text); border: 1px solid var(--border); border-radius: 6px;
            width: 26px; height: 26px; cursor: pointer; font-size: .9rem; line-height: 1; }
.mini-btn:hover { border-color: var(--accent); }
.mini-btn.danger:hover { border-color: var(--red); color: var(--red); }

/* ---- Empty ---- */
.empty { text-align: center; padding: 70px 20px; color: var(--text-muted); }
.empty-icon { font-size: 2.6rem; margin-bottom: 10px; }
.empty h2 { color: var(--text); margin-bottom: 14px; font-size: 1.2rem; }
.empty p { margin-bottom: 6px; }

/* ---- Item page ---- */
.back-link { display: inline-block; color: var(--text-muted); margin-bottom: 16px; font-size: .9rem; }
.item-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 28px; align-items: start; }
.gallery-main { background: #121319; border: 1px solid var(--border); border-radius: var(--radius); aspect-ratio: 4 / 3;
                display: flex; align-items: center; justify-content: center; overflow: hidden; cursor: zoom-in; }
.gallery-main img { max-width: 100%; max-height: 100%; object-fit: contain; }
.gallery-thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.gthumb { position: relative; width: 78px; height: 78px; border: 2px solid var(--border); border-radius: 8px;
          overflow: hidden; cursor: pointer; background: #121319; }
.gthumb.is-primary { border-color: var(--accent); }
.gthumb.selected { border-color: var(--text); }
.gthumb img { width: 100%; height: 100%; object-fit: cover; }
.gthumb-actions { position: absolute; inset: auto 2px 2px auto; display: none; gap: 2px; }
.gthumb:hover .gthumb-actions { display: flex; }
.gthumb-actions .mini-btn { width: 22px; height: 22px; font-size: .75rem; background: rgba(15, 17, 23, .9); }
.gallery-empty { background: var(--bg-card); border: 1px dashed var(--border); border-radius: var(--radius);
                 aspect-ratio: 4 / 3; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 20px; }
.item-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.qty-box { display: flex; align-items: center; gap: 12px; margin: 6px 0 18px; }
.qty-btn { width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg-card);
           color: var(--text); font-size: 1.3rem; cursor: pointer; }
.qty-btn:hover { border-color: var(--accent); }
.qty-value { font-size: 2rem; font-weight: 750; min-width: 48px; text-align: center; }
.qty-value.zero { color: var(--red); }
.info-list { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; font-size: .9rem; margin-bottom: 18px; }
.info-list dt { color: var(--text-muted); }
.info-list dd { overflow-wrap: anywhere; }
.block { margin-bottom: 18px; }
.block h3 { color: var(--text-muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; }
table.specs { width: 100%; border-collapse: collapse; font-size: .88rem; background: var(--bg-card); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
table.specs th, table.specs td { padding: 7px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
table.specs th { color: var(--text-muted); font-weight: 500; width: 40%; }
table.specs tr:last-child th, table.specs tr:last-child td { border-bottom: none; }
.item-actions { display: flex; gap: 8px; margin-top: 8px; }
.history { margin-top: 32px; }

/* ---- Log ---- */
.log-list { list-style: none; }
.log-list li { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-bottom: 1px solid var(--border); font-size: .87rem; flex-wrap: wrap; }
.log-list li:last-child { border-bottom: none; }
.card-list, .history .log-list { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); }
.log-time { color: var(--text-muted); font-size: .8rem; font-variant-numeric: tabular-nums; min-width: 112px; }
.log-name { font-weight: 600; color: var(--text); }
.log-details { color: var(--text-muted); overflow-wrap: anywhere; }

/* ---- Agent page ---- */
.agent-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.panel { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 16px; }
.panel p { margin-bottom: 12px; }
.link-list { list-style: none; font-size: .9rem; }
.link-list li { margin-bottom: 6px; overflow-wrap: anywhere; }
pre.code { background: var(--bg-input); border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px;
           font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .8rem; overflow-x: auto; line-height: 1.55; }
.tools-table { width: 100%; border-collapse: collapse; font-size: .86rem; }
.tools-table th, .tools-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.tools-table th { white-space: nowrap; font-weight: 500; }
.tools-table td { color: var(--text-muted); }

/* ---- Modal ---- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; }
.modal-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .6); }
.modal-card { position: relative; width: 100%; max-width: 640px; background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px; padding: 22px 24px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; }
.modal-head h2 { font-size: 1.15rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* ---- Lightbox / toast ---- */
.lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .9); display: flex; align-items: center; justify-content: center; cursor: zoom-out; padding: 20px; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
         background: var(--bg-card-hover); border: 1px solid var(--border); padding: 10px 18px; border-radius: 10px; font-size: .9rem;
         transition: opacity .2s, transform .2s; z-index: 70; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: var(--red); color: #fca5a5; }

/* ---- Responsive ---- */
@media (max-width: 860px) {
    .item-layout, .agent-grid { grid-template-columns: 1fr; }
    .stats { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
    .container { padding: 16px; }
    .topbar { padding: 0 14px; gap: 12px; }
    .nav a { padding: 6px 8px; font-size: .85rem; }
    .brand-text { display: none; }
    .form-grid { grid-template-columns: 1fr; }
    .span-2 { grid-column: span 1; }
    .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
    .toolbar select { flex: 1 1 45%; }
    .badge-src { margin-left: 0; }
}
