:root {
  --navy: #10243e;
  --navy-light: #1b3a5c;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --bg: #f4f6f9;
  --card-bg: #ffffff;
  --border: #dfe3ea;
  --text: #1a2233;
  --text-muted: #667085;
  --danger: #d92d20;
  --danger-bg: #fee4e2;
  --warn: #b54708;
  --warn-bg: #fef0c7;
  --success: #067647;
  --success-bg: #dcfae6;
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.screen { min-height: 100vh; }

button {
  font-family: inherit;
  cursor: pointer;
}

input, select, textarea {
  font-family: inherit;
  font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 100%;
  background: #fff;
  color: var(--text);
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 12px;
}

label > input, label > select, label > textarea {
  margin-top: 4px;
  font-weight: normal;
  color: var(--text);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.btn {
  border: none;
  border-radius: 6px;
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-secondary { background: #eef1f6; color: var(--text); }
.btn-secondary:hover { background: #e2e6ee; }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { color: var(--text); }
.btn-block { width: 100%; justify-content: center; }
.btn-icon {
  border: none; background: #eef1f6; border-radius: 6px;
  width: 24px; height: 24px; line-height: 1; font-weight: 700;
}

/* ---------- Login ---------- */
#login-screen {
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, var(--navy) 0%, var(--navy-light) 100%);
}
#login-screen[hidden] { display: none; }
.login-card {
  background: #fff; padding: 40px; border-radius: var(--radius);
  width: 340px; box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card .subtitle { margin: 0 0 24px; color: var(--text-muted); font-size: 13px; }
.error-text { color: var(--danger); font-size: 13px; margin-top: 8px; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px;
  background: var(--navy); color: #fff; padding: 12px 20px;
  position: sticky; top: 0; z-index: 20;
}
.topbar-title { font-weight: 700; font-size: 16px; white-space: nowrap; }
.topbar-search { flex: 1; }
.topbar-search input {
  background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.2);
  color: #fff;
}
.topbar-search input::placeholder { color: rgba(255,255,255,0.6); }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.topbar-actions .btn-ghost { color: rgba(255,255,255,0.75); }
.topbar-actions .btn-ghost:hover { color: #fff; }

/* ---------- Layout ---------- */
.layout { display: flex; align-items: flex-start; }
.sidebar {
  width: 240px; flex-shrink: 0; background: #fff; border-right: 1px solid var(--border);
  min-height: calc(100vh - 56px); padding: 16px 12px;
}
.main-panel { flex: 1; padding: 20px 24px; }

.view-switch { display: flex; flex-direction: column; gap: 2px; margin-bottom: 20px; }
.nav-link {
  text-align: left; background: none; border: none; padding: 9px 10px; border-radius: 6px;
  font-size: 13px; font-weight: 600; color: var(--text-muted);
}
.nav-link:hover { background: var(--bg); }
.nav-link.active { background: #e8effe; color: var(--accent-dark); }

.sidebar-section { margin-bottom: 20px; }
.sidebar-heading {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--text-muted); font-weight: 700; margin-bottom: 8px;
}
.folder-tree { list-style: none; margin: 0; padding: 0; }
.folder-tree ul { list-style: none; margin: 0; padding-left: 14px; }
.folder-link {
  display: block; width: 100%; text-align: left; background: none; border: none;
  padding: 6px 8px; border-radius: 6px; font-size: 13px; color: var(--text);
}
.folder-link:hover { background: var(--bg); }
.folder-link.active { background: #e8effe; color: var(--accent-dark); font-weight: 600; }

/* ---------- Item grid ---------- */
.view-heading { display: flex; align-items: baseline; gap: 10px; margin-bottom: 16px; }
.view-heading h2 { margin: 0; font-size: 18px; }
.result-count { color: var(--text-muted); font-size: 13px; }

.item-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}
.item-card {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; transition: box-shadow .15s, transform .15s;
}
.item-card:hover { box-shadow: 0 6px 18px rgba(16,36,62,0.1); transform: translateY(-1px); }
.item-thumb {
  width: 100%; height: 130px; background: #eef1f6; display: flex; align-items: center;
  justify-content: center; overflow: hidden; color: var(--text-muted); font-size: 12px;
}
.item-thumb img { width: 100%; height: 100%; object-fit: cover; }
.item-card-body { padding: 10px 12px; }
.item-card-name { font-weight: 600; font-size: 13.5px; margin: 0 0 4px; line-height: 1.3; }
.item-card-meta { color: var(--text-muted); font-size: 12px; margin: 0 0 6px; }
.badge-row { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px;
}
.badge-active { background: var(--success-bg); color: var(--success); }
.badge-repair, .badge-missing { background: var(--warn-bg); color: var(--warn); }
.badge-disposed, .badge-expired { background: var(--danger-bg); color: var(--danger); }
.badge-flag { background: var(--warn-bg); color: var(--warn); }

.empty-state { color: var(--text-muted); text-align: center; padding: 60px 0; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(16,36,62,0.5);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto; z-index: 50;
}
/* The [hidden] attribute must win over the flex display above, otherwise a
   "hidden" modal still renders (invisible background aside) and blocks
   clicks on whatever is underneath it. */
.modal-overlay[hidden] { display: none; }
.modal {
  background: #fff; border-radius: var(--radius); width: 100%; max-width: 760px;
  padding: 28px; position: relative; box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal-small { max-width: 400px; }
.modal-close {
  position: absolute; top: 16px; right: 16px; border: none; background: none;
  font-size: 22px; color: var(--text-muted); line-height: 1;
}
.modal-header { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; padding-right: 24px; }
.modal-header h2 { margin: 0; font-size: 18px; }
.code-badge {
  background: #eef1f6; color: var(--text-muted); font-size: 12px; font-weight: 700;
  padding: 3px 9px; border-radius: 999px; letter-spacing: 0.02em;
}
.modal-body { display: flex; gap: 24px; }
.modal-col { flex-shrink: 0; width: 180px; }
.modal-col-wide { flex: 1; width: auto; }

.photo-box {
  width: 180px; height: 140px; border-radius: 8px; background: #eef1f6; overflow: hidden;
  display: flex; align-items: center; justify-content: center; margin-bottom: 8px;
}
.photo-box img { width: 100%; height: 100%; object-fit: cover; }
.photo-placeholder { color: var(--text-muted); font-size: 12px; }
.file-label {
  font-size: 12px; text-align: center; background: #eef1f6; padding: 7px; border-radius: 6px;
  margin-bottom: 16px; cursor: pointer;
}
.file-label input { display: none; }

.qr-box { text-align: center; border-top: 1px solid var(--border); padding-top: 14px; }
.qr-box canvas { margin-bottom: 8px; border: 1px solid var(--border); border-radius: 6px; }

.checkbox-label { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text); }
.checkbox-label input { width: auto; }

#flag-reason-wrap { display: none; }
#flag-reason-wrap.show { display: block; }

.hint { font-size: 12px; color: var(--text-muted); margin: 4px 0 0; }

.modal-footer {
  display: flex; align-items: center; justify-content: space-between;
  border-top: 1px solid var(--border); padding-top: 16px; margin-top: 12px;
}
.modal-footer-right { display: flex; gap: 8px; margin-left: auto; }

.history-section { border-top: 1px solid var(--border); margin-top: 16px; padding-top: 12px; }
.history-section h3 { margin: 0 0 8px; font-size: 13px; }
#history-list { list-style: none; margin: 0; padding: 0; font-size: 12.5px; color: var(--text-muted); }
#history-list li { padding: 4px 0; border-bottom: 1px dashed var(--border); }

/* ---------- Scan modal ---------- */
.scan-video-wrap { border-radius: 8px; overflow: hidden; background: #000; }
#scan-video { width: 100%; display: block; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--navy); color: #fff; padding: 10px 20px; border-radius: 8px;
  font-size: 13px; z-index: 100; box-shadow: 0 8px 24px rgba(0,0,0,0.25);
}
.toast.toast-error { background: var(--danger); }

@media (max-width: 800px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; min-height: auto; border-right: none; border-bottom: 1px solid var(--border); }
  .modal-body { flex-direction: column; }
  .modal-col { width: 100%; }
  .field-row { grid-template-columns: 1fr; }
}
