:root {
  --bg: #0f1420;
  --bg-elev: #171d2b;
  --bg-card: #1c2435;
  --bg-input: #131927;
  --border: #2a3344;
  --border-soft: #222b3c;
  --text: #e8edf6;
  --text-dim: #93a0b8;
  --text-faint: #647088;
  --primary: #6c8cff;
  --primary-hover: #5878ff;
  --danger: #ff5c6c;
  --success: #3ecf8e;
  --warn: #ffb454;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

/* The hidden attribute must always win over component display rules. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 20px; margin: 0; letter-spacing: -0.02em; }
.logo { font-size: 26px; }
.header-actions { display: flex; gap: 8px; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
  color: var(--text);
  background: var(--bg-card);
  border-color: var(--border);
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--bg-card); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.icon-btn {
  background: transparent; border: none; color: var(--text-dim);
  font-size: 18px; cursor: pointer; padding: 4px 8px; border-radius: 6px;
}
.icon-btn:hover { background: var(--bg-card); color: var(--text); }

/* Tabs */
.tabs { display: flex; gap: 6px; padding: 16px 24px 0; }
.tab {
  font: inherit; font-weight: 600; cursor: pointer;
  background: transparent; border: none; color: var(--text-dim);
  padding: 9px 14px; border-radius: var(--radius-sm);
  display: inline-flex; align-items: center; gap: 7px;
}
.tab:hover { background: var(--bg-card); color: var(--text); }
.tab.active { background: var(--bg-card); color: var(--text); }
.tab-count {
  background: var(--warn); color: #1a1205; font-size: 12px; font-weight: 700;
  min-width: 20px; text-align: center; padding: 1px 7px; border-radius: 20px;
}

/* Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  padding: 20px 24px 4px;
}
.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stat-label { color: var(--text-dim); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.stat-value { font-size: 24px; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.stat-value.pos { color: var(--success); }
.stat-value.neg { color: var(--danger); }
.stat-sub { font-size: 12px; color: var(--text-faint); margin-top: 2px; }

/* Alert banner */
.alert-banner {
  margin: 12px 24px 0;
  padding: 12px 16px;
  background: rgba(255, 180, 84, 0.12);
  border: 1px solid rgba(255, 180, 84, 0.4);
  border-radius: var(--radius-sm);
  color: var(--warn);
  font-size: 14px;
}

/* Controls */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 18px 24px;
  align-items: center;
}
.search-wrap { position: relative; flex: 1 1 240px; min-width: 200px; }
.search-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); opacity: .6; font-size: 14px; }
#search {
  width: 100%;
  padding: 10px 12px 10px 34px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
}
.filter {
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }

/* Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  padding: 0 24px 40px;
}
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: transform .12s, border-color .12s, box-shadow .12s;
  display: flex;
  flex-direction: column;
}
.card:hover { transform: translateY(-3px); border-color: var(--border); box-shadow: var(--shadow); }
.card-photo {
  aspect-ratio: 1 / 1;
  background: var(--bg-input);
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  color: var(--text-faint);
  position: relative;
}
.card-qty-badge {
  position: absolute; top: 8px; right: 8px;
  background: rgba(0,0,0,.6); backdrop-filter: blur(4px);
  color: #fff; font-size: 12px; font-weight: 700;
  padding: 3px 8px; border-radius: 20px;
}
.card-low { position: absolute; top: 8px; left: 8px; font-size: 16px; }
.card-body { padding: 12px 14px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card-name { font-weight: 600; line-height: 1.3; }
.card-meta { font-size: 12px; color: var(--text-dim); display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.card-foot { display: flex; align-items: center; justify-content: space-between; margin-top: auto; padding-top: 6px; }
.card-price { font-weight: 700; font-size: 15px; }

/* Status pill */
.pill {
  font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 20px;
  text-transform: uppercase; letter-spacing: .03em; white-space: nowrap;
}
.pill-collection { background: rgba(108,140,255,.18); color: #9fb2ff; }
.pill-sale { background: rgba(62,207,142,.18); color: var(--success); }
.pill-trade { background: rgba(255,180,84,.18); color: var(--warn); }
.pill-wanted { background: rgba(180,120,255,.18); color: #c39bff; }
.pill-ordered { background: rgba(80,190,220,.18); color: #74d0e6; }
.pill-sold { background: rgba(150,160,180,.18); color: var(--text-dim); }

.tag { font-size: 11px; background: var(--bg-input); color: var(--text-dim); padding: 2px 7px; border-radius: 5px; }

/* Empty state */
.empty { text-align: center; padding: 80px 24px; color: var(--text-dim); }
.empty-emoji { font-size: 54px; }
.empty h3 { margin: 14px 0 6px; color: var(--text); }

/* Modal */
.modal[hidden] { display: none; }
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(8, 11, 18, 0.7);
  backdrop-filter: blur(4px);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto;
}
.modal-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%; max-width: 620px;
  box-shadow: var(--shadow);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 22px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 18px; }
.form { padding: 22px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-dim); font-weight: 600; }
.form label.full { grid-column: 1 / -1; }
.form input, .form select, .form textarea {
  padding: 9px 11px; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text); font: inherit; font-weight: 400;
}
.form textarea { resize: vertical; }
.hint { color: var(--text-faint); font-weight: 400; }

/* Photo field */
.photo-field { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 18px; }
.photo-preview {
  width: 160px; height: 160px; border-radius: var(--radius);
  background: var(--bg-input); border: 2px dashed var(--border);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; background-size: cover; background-position: center;
  color: var(--text-faint); text-align: center; font-size: 13px;
}
.photo-preview:hover { border-color: var(--primary); }
.photo-placeholder { line-height: 1.6; }

.modal-foot { display: flex; align-items: center; gap: 10px; margin-top: 22px; }
.modal-foot .spacer { flex: 1; }

/* View mode (read-only) */
#viewMode { padding: 22px; }
.view-body { display: flex; gap: 20px; flex-wrap: wrap; }
.view-photo {
  flex: 0 0 220px; width: 220px; height: 220px; border-radius: var(--radius);
  background: var(--bg-input); background-size: cover; background-position: center;
  display: flex; align-items: center; justify-content: center;
  font-size: 56px; color: var(--text-faint);
}
.view-photo.clickable { cursor: zoom-in; }
.view-details { flex: 1 1 260px; min-width: 240px; }
.view-row { display: flex; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--border-soft); }
.view-row:last-child { border-bottom: none; }
.view-row .vlabel { flex: 0 0 130px; color: var(--text-dim); font-size: 13px; }
.view-row .vval { flex: 1; }
.view-row.notes .vval { white-space: pre-wrap; }
.view-details a { color: var(--primary); word-break: break-all; }
.view-name { font-size: 20px; font-weight: 700; margin: 0 0 10px; letter-spacing: -0.02em; }

/* Lightbox */
.lightbox[hidden] { display: none; }
.lightbox {
  position: fixed; inset: 0; z-index: 70;
  background: rgba(5, 7, 12, 0.92);
  display: flex; align-items: center; justify-content: center;
  padding: 24px; cursor: zoom-out;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; box-shadow: var(--shadow); }

/* Login */
body:not(.authed) .header-actions,
body:not(.authed) #stats,
body:not(.authed) .tabs,
body:not(.authed) .controls,
body:not(.authed) main { display: none; }

.login {
  position: fixed; inset: 0; z-index: 40;
  display: flex; align-items: center; justify-content: center;
  padding: 24px; background: var(--bg);
}
.login-card {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 32px; width: 100%; max-width: 360px;
  display: flex; flex-direction: column; gap: 14px; box-shadow: var(--shadow);
}
.login-brand { display: flex; align-items: center; gap: 10px; justify-content: center; }
.login-brand h1 { margin: 0; font-size: 22px; }
.login-sub { color: var(--text-dim); text-align: center; margin: 0; font-size: 14px; }
.login-card input {
  padding: 11px 13px; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text); font: inherit;
}
.login-card input:focus { outline: none; border-color: var(--primary); }
.login-error {
  color: var(--danger); font-size: 13px; text-align: center;
  background: rgba(255,92,108,.1); border-radius: var(--radius-sm); padding: 8px;
}

/* Toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-card); border: 1px solid var(--border);
  color: var(--text); padding: 12px 20px; border-radius: var(--radius-sm);
  box-shadow: var(--shadow); z-index: 60; font-weight: 500;
  animation: toastIn .2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } }

@media (max-width: 560px) {
  .form-grid { grid-template-columns: 1fr; }
  .app-header { flex-wrap: wrap; gap: 10px; }
  .header-actions { width: 100%; }
  .header-actions .btn { flex: 1; }
}
