:root {
  --bg:         #080c18;
  --surface:    #0e1528;
  --card:       #131c32;
  --border:     #1c2845;
  --border2:    #253358;
  --accent:     #8b5cf6;
  --accent-lt:  #a78bfa;
  --accent-dim: #2d1f5e;
  --blue:       #3b82f6;
  --blue-dim:   #1a3060;
  --text:       #e2e8f8;
  --muted:      #4a5a80;
  --danger:     #f87171;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; background: var(--bg); font-family: 'Plus Jakarta Sans', sans-serif; color: var(--text); overscroll-behavior: none; overflow-x: hidden; }

input, button { font-family: inherit; }
input::placeholder { color: var(--muted); }
input { caret-color: var(--accent); }
::-webkit-scrollbar { width: 3px; }
::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 2px; }

#app { width: 100%; max-width: 430px; margin: 0 auto; height: 100%; display: flex; flex-direction: column; overflow: hidden; }

/* ── TOP BAR ── */
#topbar { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 14px; border-bottom: 1px solid var(--border); background: var(--surface); flex-shrink: 0; }
#topbar-left { display: flex; align-items: center; gap: 10px; }
#topbar-icon { width: 30px; height: 30px; border-radius: 9px; background: linear-gradient(135deg, var(--accent), var(--blue)); display: flex; align-items: center; justify-content: center; }
#topbar-title { font-size: 16px; font-weight: 700; }
#back-btn { background: none; border: none; color: var(--muted); cursor: pointer; padding: 6px; display: none; align-items: center; }

/* ── CONTENT ── */
#content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; position: relative; }
.view { display: none; padding: 24px 20px; animation: fadeIn .18s ease; }
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ── PIN VIEW ── */
#view-pin { display: none; flex-direction: column; align-items: center; padding: 60px 20px 20px; }
#view-pin.active { display: flex; }
#pin-lock { width: 56px; height: 56px; border-radius: 50%; background: var(--accent-dim); border: 1px solid var(--accent); display: flex; align-items: center; justify-content: center; color: var(--accent-lt); margin-bottom: 20px; }
#pin-heading { font-size: 13px; color: var(--muted); margin-bottom: 28px; letter-spacing: .12em; }
#pin-dots { display: flex; gap: 14px; margin-bottom: 8px; }
.dot { width: 14px; height: 14px; border-radius: 50%; background: transparent; border: 2px solid var(--border2); transition: all .15s; }
.dot.filled { background: var(--accent); border-color: var(--accent); box-shadow: 0 0 10px #8b5cf680; }
#pin-error { color: var(--danger); font-size: 12px; margin-bottom: 12px; letter-spacing: .08em; display: none; }
#numpad { margin-top: 24px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; width: 260px; }
.nk { background: var(--card); border: 1px solid var(--border2); border-radius: 14px; padding: 18px 0; color: var(--text); font-size: 22px; font-weight: 600; cursor: pointer; font-family: monospace; transition: background .15s; }
.nk:active { background: var(--border2); }
.nk.empty { background: transparent; border: none; cursor: default; }

/* ── BOTTOM NAV ── */
#bottomnav { display: flex; border-top: 1px solid var(--border); background: var(--surface); flex-shrink: 0; }
.nav-btn { flex: 1; padding: 13px 0 10px; background: none; border: none; border-top: 2px solid transparent; color: var(--muted); cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 10px; letter-spacing: .1em; font-family: inherit; transition: color .2s, border-color .2s; }
.nav-btn.active { color: var(--accent-lt); border-top-color: var(--accent); }

/* ── SEARCH ── */
.search-wrap { position: relative; margin-bottom: 28px; }
.search-wrap input { width: 100%; background: var(--card); border: 1px solid var(--border2); border-radius: 14px; padding: 14px 46px 14px 18px; color: var(--text); font-size: 15px; outline: none; }
.search-icon { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }

/* ── SCANNER ── */
#scan-card { background: var(--card); border: 1px solid var(--border2); border-radius: 20px; padding: 32px 20px; text-align: center; cursor: pointer; margin-bottom: 16px; transition: all .25s; }
#scan-card.scanning { background: linear-gradient(135deg, var(--accent-dim), var(--blue-dim)); border-color: var(--accent); }
#scan-icon-wrap { width: 64px; height: 64px; border-radius: 50%; background: linear-gradient(135deg, var(--accent-dim), var(--blue-dim)); display: flex; align-items: center; justify-content: center; margin: 0 auto 16px; color: var(--accent-lt); border: 1px solid var(--border2); }
#reader-wrap { display: none; width: 200px; height: 200px; margin: 0 auto 16px; border: 2px solid var(--accent); border-radius: 16px; overflow: hidden; position: relative; }
#reader-wrap.on { display: block; }
#html5-qr-reader { width: 100%; height: 100%; }
#scan-line { position: absolute; left: 12px; right: 12px; height: 2px; background: linear-gradient(90deg, transparent, var(--accent), transparent); top: 45%; animation: scanAnim 2s ease-in-out infinite; }
@keyframes scanAnim { 0%, 100% { top: 20%; opacity: .4; } 50% { top: 75%; opacity: 1; } }
.corner { position: absolute; width: 22px; height: 22px; }
.corner.tl { top: 8px; left: 8px; border-top: 3px solid var(--accent); border-left: 3px solid var(--accent); }
.corner.tr { top: 8px; right: 8px; border-top: 3px solid var(--accent); border-right: 3px solid var(--accent); }
.corner.bl { bottom: 8px; left: 8px; border-bottom: 3px solid var(--accent); border-left: 3px solid var(--accent); }
.corner.br { bottom: 8px; right: 8px; border-bottom: 3px solid var(--accent); border-right: 3px solid var(--accent); }

/* ── MANUAL INPUT ── */
#manual-card { background: var(--card); border: 1px solid var(--border2); border-radius: 16px; padding: 18px 20px; }
.slabel { font-size: 11px; color: var(--muted); letter-spacing: .12em; margin-bottom: 14px; }
#manual-row { display: flex; gap: 10px; align-items: stretch; justify-content: center; }
#manual-input { width: 150px; flex-shrink: 0; background: var(--bg); border: 1px solid var(--border2); border-radius: 12px; padding: 14px 0; color: var(--accent-lt); font-size: 32px; font-weight: 800; text-align: center; outline: none; letter-spacing: .2em; font-family: monospace; transition: border-color .2s; }
#manual-input.rdy { border-color: var(--accent); }
#manual-go { width: 52px; flex-shrink: 0; background: var(--surface); border: none; border-radius: 12px; color: var(--muted); font-size: 22px; cursor: pointer; font-weight: 700; transition: all .2s; }
#manual-go.rdy { background: linear-gradient(135deg, var(--accent), var(--blue)); color: #fff; }

/* ── SEARCH RESULTS ── */
#search-results { display: none; }
.r-item { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; margin-bottom: 8px; cursor: pointer; display: flex; justify-content: space-between; align-items: center; }
.r-item:active { opacity: .7; }
.r-desc { font-size: 14px; color: var(--text); }
.r-box { font-size: 11px; color: var(--muted); margin-top: 3px; }
.r-code { background: var(--accent-dim); border-radius: 8px; padding: 5px 11px; font-size: 15px; font-weight: 700; color: var(--accent-lt); font-family: monospace; flex-shrink: 0; }
.no-results { color: var(--muted); text-align: center; padding: 48px 0; font-size: 14px; }

/* ── BOX VIEW ── */
#box-hero { background: linear-gradient(135deg, var(--accent-dim) 0%, var(--blue-dim) 100%); border: 1px solid var(--border2); border-radius: 20px; padding: 28px 24px; text-align: center; margin-bottom: 16px; }
#box-code-display { font-size: 80px; font-weight: 800; color: var(--accent-lt); letter-spacing: .12em; line-height: 1; font-family: monospace; text-shadow: 0 0 40px #8b5cf640; }

/* ── QR CODE ── */
#qr-toggle-btn { width: 100%; background: var(--card); border: 1px solid var(--border2); border-radius: 12px; padding: 13px 16px; color: var(--muted); font-size: 13px; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px; margin-bottom: 20px; font-family: inherit; }
#qr-panel { display: none; background: var(--card); border: 1px solid var(--border); border-radius: 16px; padding: 24px; text-align: center; margin-bottom: 16px; }
#qr-panel.on { display: block; }
#qrcode-el { display: flex; justify-content: center; }
#qrcode-el canvas, #qrcode-el img { border-radius: 8px; }
#qr-hint { font-size: 11px; color: var(--muted); margin-top: 12px; letter-spacing: .1em; }
#qr-close { background: none; border: none; color: var(--muted); font-size: 12px; cursor: pointer; margin-top: 8px; font-family: inherit; }

/* ── ITEMS ── */
#items-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
#items-count { font-size: 11px; color: var(--muted); letter-spacing: .12em; }
#delete-box-btn { background: none; border: 1px solid #7f1d1d; border-radius: 8px; padding: 4px 10px; color: var(--danger); font-size: 11px; cursor: pointer; font-family: inherit; }
.item-row { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 13px 16px; margin-bottom: 7px; display: flex; justify-content: space-between; align-items: center; }
.item-inner { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.item-text { font-size: 14px; color: var(--text); flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.item-edit-input { width: 100%; background: var(--surface); border: 1px solid var(--accent); border-radius: 6px; padding: 6px 8px; color: var(--text); font-size: 14px; outline: none; font-family: inherit; }
.item-del { background: none; border: none; color: var(--muted); cursor: pointer; padding: 4px; border-radius: 6px; display: flex; flex-shrink: 0; margin-left: 8px; }
#add-item-row { display: flex; gap: 10px; margin-top: 16px; }
#new-item-input { flex: 1; background: var(--card); border: 1px solid var(--accent); border-radius: 10px; padding: 13px 14px; color: var(--text); font-size: 14px; outline: none; font-family: inherit; }
#add-item-btn { background: linear-gradient(135deg, var(--accent), var(--blue)); border: none; border-radius: 10px; padding: 0 16px; color: #fff; cursor: pointer; font-size: 22px; }

/* ── NEW BOX FAB ── */
#new-box-fab { display: none; position: fixed; bottom: 80px; right: calc(50% - 215px + 20px); width: 52px; height: 52px; border-radius: 50%; background: linear-gradient(135deg, var(--accent), var(--blue)); border: none; color: #fff; font-size: 26px; cursor: pointer; box-shadow: 0 4px 20px #8b5cf640; align-items: center; justify-content: center; z-index: 10; }

/* ── BOX LIST ── */
#list-count { font-size: 11px; color: var(--muted); letter-spacing: .12em; margin-bottom: 16px; }
.box-row { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 16px; margin-bottom: 10px; cursor: pointer; display: flex; align-items: center; gap: 14px; }
.box-row:active { opacity: .7; }
.box-thumb { width: 54px; height: 54px; flex-shrink: 0; background: linear-gradient(135deg, var(--accent-dim), var(--blue-dim)); border-radius: 12px; display: flex; align-items: center; justify-content: center; border: 1px solid var(--border2); font-size: 14px; font-weight: 800; color: var(--accent-lt); font-family: monospace; }
.box-name { font-size: 15px; font-weight: 600; margin-bottom: 3px; }
.box-cnt { font-size: 12px; color: var(--muted); }
.box-chev { color: var(--muted); font-size: 20px; }

/* ── LOADING / TOAST ── */
#toast { position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--card); border: 1px solid var(--border2); border-radius: 12px; padding: 10px 20px; font-size: 13px; color: var(--text); opacity: 0; transition: all .3s; pointer-events: none; white-space: nowrap; z-index: 100; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--border2); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── MODAL ── */
#modal-overlay { display: none; position: fixed; inset: 0; background: #00000080; z-index: 50; align-items: flex-end; justify-content: center; }
#modal-overlay.on { display: flex; }
#modal { background: var(--surface); border: 1px solid var(--border2); border-radius: 20px 20px 0 0; padding: 28px 24px 40px; width: 100%; max-width: 430px; }
#modal h3 { font-size: 17px; font-weight: 700; margin-bottom: 20px; }
.modal-field { margin-bottom: 14px; }
.modal-field label { font-size: 11px; color: var(--muted); letter-spacing: .1em; display: block; margin-bottom: 8px; }
.modal-field input { width: 100%; background: var(--card); border: 1px solid var(--border2); border-radius: 10px; padding: 13px 14px; color: var(--text); font-size: 15px; outline: none; font-family: inherit; }
.modal-field input:focus { border-color: var(--accent); }
#modal-code-input { font-size: 28px; font-weight: 800; text-align: center; letter-spacing: .2em; font-family: monospace; color: var(--accent-lt); }
.modal-row { display: flex; gap: 12px; margin-top: 20px; }
.btn-cancel { flex: 1; background: var(--card); border: 1px solid var(--border2); border-radius: 12px; padding: 14px; color: var(--muted); cursor: pointer; font-size: 14px; font-family: inherit; }
.btn-confirm { flex: 2; background: linear-gradient(135deg, var(--accent), var(--blue)); border: none; border-radius: 12px; padding: 14px; color: #fff; cursor: pointer; font-size: 14px; font-weight: 700; font-family: inherit; }
