* { box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-2: #242837;
  --border: #2e3344;
  --text: #e8eaed;
  --text-dim: #9aa0aa;
  --primary: #5b9bff;
  --primary-text: #fff;
  --danger: #ff5b5b;
  --success: #4ade80;
  --warning: #fbbf24;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

body { padding-bottom: env(safe-area-inset-bottom); }
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; font-size: 16px; }
img { max-width: 100%; display: block; }

.screen { display: none; min-height: 100vh; flex-direction: column; }
.screen[data-active] { display: flex; }

.loading {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 100vh; gap: 16px; color: var(--text-dim);
}
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--surface-2); border-top-color: var(--primary);
  border-radius: 50%; animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.login {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 100vh; gap: 12px; padding: 24px; max-width: 400px; margin: 0 auto;
}
.brand { text-align: center; margin-bottom: 24px; color: var(--primary); }
.brand--denied { color: var(--danger); }
.brand-icon { display: inline-flex; margin-bottom: 12px; }
.brand h1 { margin: 0 0 4px; font-size: 32px; color: var(--text); }
.brand p { margin: 0; color: var(--text-dim); font-size: 15px; }
.hint { color: var(--text-dim); font-size: 13px; text-align: center; margin: 4px 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 12px 18px; border-radius: var(--radius);
  border: 1px solid transparent;
  background: var(--surface-2); color: var(--text);
  font-size: 16px; font-weight: 500;
  text-decoration: none; min-height: 44px;
  transition: opacity 0.15s, transform 0.05s;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.4; pointer-events: none; }
.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-secondary { background: var(--surface-2); border-color: var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-link { background: transparent; color: var(--primary); padding: 8px; border: none; min-height: auto; }
.btn-block { width: 100%; }
.btn-small { padding: 6px 14px; min-height: 36px; font-size: 14px; }

.icon-btn {
  background: transparent; border: none; color: var(--text);
  width: 40px; height: 40px; border-radius: var(--radius-sm);
  display: inline-flex; align-items: center; justify-content: center; padding: 0;
}
.icon-btn:active { background: var(--surface-2); }

.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 12px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar h1 {
  margin: 0; font-size: 17px; font-weight: 600;
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-actions { display: flex; gap: 2px; }

.custom-controls {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.group-by-label {
  display: flex; align-items: center; gap: 10px;
  flex: 1; min-width: 0;
}
.group-by-label > span {
  font-size: 13px;
  color: var(--text-dim);
  flex-shrink: 0;
}
.group-by-label > select { flex: 1; min-width: 0; }

.grouped-list {
  flex: 1;
  padding: 12px 16px 96px;
  display: flex; flex-direction: column; gap: 10px;
}
.group-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.group-header {
  display: flex; align-items: center;
  padding: 12px 14px;
  cursor: pointer;
  list-style: none;
  user-select: none;
  font-weight: 600;
}
.group-header::-webkit-details-marker { display: none; }
.group-header::marker { content: ''; }
.group-header::before {
  content: '▸';
  margin-right: 10px;
  color: var(--text-dim);
  transition: transform 0.15s ease;
  display: inline-block;
}
.group-section[open] .group-header::before { transform: rotate(90deg); }
.group-name { flex: 1; text-transform: capitalize; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.group-count { font-size: 13px; color: var(--text-dim); font-weight: 500; }
.group-items {
  padding: 0 12px 12px;
  display: flex; flex-direction: column; gap: 8px;
}

.search-bar { padding: 12px 16px; background: var(--surface); }
.search-bar input {
  width: 100%; padding: 10px 14px;
  border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}
.search-bar input:focus { outline: none; border-color: var(--primary); }

.list-controls {
  display: flex; gap: 8px;
  padding: 4px 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.list-select {
  flex: 1; min-width: 0;
  padding: 8px 28px 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path d='M1 1l4 4 4-4' fill='none' stroke='%239aa0aa' stroke-width='1.5' stroke-linecap='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  text-overflow: ellipsis;
}
.list-select:focus { outline: none; border-color: var(--primary); }

.filter-button {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  cursor: pointer;
  text-align: center;
  background-image: none;
  padding-right: 12px;
}
.filter-button .filter-count {
  background: var(--primary);
  color: var(--primary-text);
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  min-width: 18px;
  text-align: center;
}

.filter-pane {
  position: fixed; inset: 0;
  background: var(--bg);
  z-index: 250;
  display: flex; flex-direction: column;
}
.filter-pane-header {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 12px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.filter-pane-header h2 {
  margin: 0; font-size: 17px; font-weight: 600;
  flex: 1; min-width: 0;
}
.filter-pane-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: 24px;
}
.filter-section { margin-bottom: 24px; }
.filter-section h3 {
  font-size: 12px; font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase; letter-spacing: 0.6px;
  margin: 0 0 10px;
}
.filter-options { display: flex; flex-wrap: wrap; gap: 8px; }
.filter-chip {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text);
  font-size: 14px;
  white-space: nowrap;
  cursor: pointer;
  font-family: inherit;
}
.filter-chip[aria-pressed="true"] {
  background: var(--primary); border-color: var(--primary); color: var(--primary-text);
}
.filter-pane-footer {
  display: flex; gap: 8px;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.filter-pane-footer .btn { flex: 1; }
.filter-empty {
  color: var(--text-dim);
  font-size: 13px;
  margin: 4px 0;
}

.list {
  flex: 1; padding: 12px 16px 96px;
  display: flex; flex-direction: column; gap: 10px;
}
.list-empty {
  text-align: center; padding: 64px 16px; color: var(--text-dim);
}
.list-empty p { margin: 4px 0; }
.item-card {
  display: flex; gap: 12px; padding: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  align-items: center;
  text-align: left;
  cursor: pointer;
  color: var(--text);
}
.item-card:active { background: var(--surface-2); }
.item-card .thumb {
  width: 56px; height: 56px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  object-fit: cover;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; color: var(--text-dim);
}
.item-card .info { flex: 1; min-width: 0; }
.item-card .info .name {
  font-weight: 600; margin: 0 0 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.item-card .info .meta {
  font-size: 13px; color: var(--text-dim);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.state-badge {
  font-size: 11px; padding: 3px 8px;
  border-radius: 999px;
  font-weight: 600; flex-shrink: 0;
  text-transform: uppercase; letter-spacing: 0.5px;
}
.state-badge.in { background: rgba(74, 222, 128, 0.15); color: var(--success); }
.state-badge.out { background: rgba(251, 191, 36, 0.15); color: var(--warning); }
.state-badge.disabled { background: rgba(154, 160, 170, 0.15); color: var(--text-dim); }

.item-card.is-disabled { opacity: 0.55; }
.item-card.is-disabled .name { text-decoration: line-through; text-decoration-color: var(--text-dim); }

.fab {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  right: 24px;
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--primary); color: var(--primary-text);
  border: none;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow);
  z-index: 5;
}
.fab:active { transform: scale(0.95); }

.detail { flex: 1; padding: 16px; padding-bottom: 100px; }
.detail-photo {
  width: 100%; aspect-ratio: 4/3;
  object-fit: cover;
  border-radius: var(--radius);
  background: var(--surface-2);
  margin-bottom: 20px;
  display: flex; align-items: center; justify-content: center;
  font-size: 48px; color: var(--text-dim);
}
.detail-section { margin-bottom: 24px; }
.detail-section h3 {
  font-size: 12px; font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase; letter-spacing: 0.6px;
  margin: 0 0 8px;
}
.detail-section p { margin: 4px 0; }
.qty-display strong { font-size: 28px; font-weight: 700; color: var(--primary); }
.detail-meta {
  display: grid; grid-template-columns: max-content 1fr; gap: 8px 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px;
}
.detail-meta dt { color: var(--text-dim); font-size: 14px; }
.detail-meta dd { margin: 0; font-size: 14px; word-break: break-word; }

.timeline { display: flex; flex-direction: column; gap: 8px; }
.event-row {
  display: flex; gap: 12px;
  padding: 10px 12px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  border-left: 3px solid var(--border);
}
.event-row.deposit { border-left-color: var(--success); }
.event-row.withdrawal { border-left-color: var(--warning); }
.event-row.created { border-left-color: var(--primary); }
.event-row.disabled { border-left-color: var(--text-dim); }
.event-row.enabled { border-left-color: var(--primary); }
.event-row .when { font-size: 13px; color: var(--text-dim); margin-top: 2px; }
.event-row .what { font-weight: 500; font-size: 15px; }
.event-row .note { font-size: 13px; color: var(--text-dim); margin-top: 4px; }

.action-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; gap: 8px;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 10;
}
.action-bar .btn { flex: 1; }

.form {
  padding: 16px; padding-bottom: 48px;
  display: flex; flex-direction: column; gap: 16px;
}
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--text-dim); font-weight: 500; }
.field input, .field textarea, .field select {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}
.field input:focus, .field textarea:focus, .field select:focus {
  outline: none; border-color: var(--primary);
}
.field textarea { resize: vertical; min-height: 72px; }

.photo-edit { display: flex; flex-direction: column; gap: 8px; }
.photo-edit img {
  max-height: 240px; width: 100%;
  object-fit: contain;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}
.photo-edit img[hidden] { display: none; }
.photo-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.photo-actions .btn { flex: 1; min-width: 100px; }
.photo-actions .btn-link { flex: 0 0 auto; }

.custom-field-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px; margin-bottom: 8px;
  align-items: center;
}
.custom-field-row input {
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
  min-width: 0;
}
.custom-field-row .remove {
  background: transparent; border: none;
  color: var(--danger); font-size: 22px;
  padding: 0 6px; line-height: 1;
}

.pin {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 100vh; gap: 16px; padding: 24px;
}
.pin h2 { margin: 0; font-size: 20px; }
.pin-dots { display: flex; gap: 12px; min-height: 14px; }
.pin-dots .dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--border);
  transition: all 0.15s;
}
.pin-dots .dot.filled { background: var(--primary); border-color: var(--primary); }
.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 72px);
  gap: 12px;
}
.pin-key {
  width: 72px; height: 72px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 24px; font-weight: 500;
}
.pin-key:active { background: var(--surface-2); transform: scale(0.95); }
.pin-key.action { background: transparent; border: none; font-size: 18px; color: var(--text-dim); }
.pin-key.placeholder { visibility: hidden; pointer-events: none; }

.toast {
  position: fixed;
  bottom: calc(100px + env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  background: var(--surface-2); color: var(--text);
  padding: 12px 18px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 100;
  max-width: 90vw; text-align: center;
  font-size: 14px;
}

.modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center;
  padding: 16px; z-index: 200;
}
.modal[hidden] { display: none; }

.camera-modal {
  position: fixed; inset: 0;
  background: #000;
  z-index: 300;
  display: flex; flex-direction: column;
}
.camera-modal[hidden] { display: none; }
.camera-modal video {
  flex: 1; width: 100%; height: 100%;
  object-fit: cover;
  background: #000;
}
.camera-close, .camera-switch {
  position: absolute; top: calc(16px + env(safe-area-inset-top));
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  border: none;
  width: 44px; height: 44px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.camera-close { left: 16px; }
.camera-switch { right: 16px; }
.camera-controls {
  position: absolute; bottom: 0; left: 0; right: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, rgba(0,0,0,0.6), transparent);
}
.camera-shutter {
  width: 76px; height: 76px;
  border-radius: 50%;
  background: #fff;
  border: 5px solid rgba(255, 255, 255, 0.95);
  box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.35);
  padding: 0;
}
.camera-shutter:active { transform: scale(0.94); background: #ddd; }
.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 320px; width: 100%;
}
.modal-card h3 { margin: 0 0 8px; font-size: 17px; }
.modal-card p { margin: 0 0 16px; color: var(--text-dim); font-size: 14px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

.type-picker { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }
.type-card {
  display: flex; flex-direction: column; align-items: flex-start;
  padding: 14px 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.type-card:hover { border-color: var(--primary); }
.type-card:active { background: var(--bg); }
.type-card .title { font-weight: 600; font-size: 15px; }
.type-card .desc { font-size: 13px; color: var(--text-dim); margin-top: 4px; }
