:root {
  --bg: #0b0d10;
  --bg-raised: #15181d;
  --bg-card: #1a1e24;
  --border: #262b33;
  --text: #f2f4f7;
  --text-dim: #9aa3af;
  --text-faint: #626b78;
  --accent: #ff8a3d;
  --accent-dim: #4a3420;
  --green: #33d17a;
  --green-dim: #163a26;
  --red: #ff5d5d;
  --red-dim: #3a1818;
  --yellow: #f2c94c;
  --yellow-dim: #3a3316;
  --radius: 14px;
  --tab-h: 72px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-size: 16px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

.hidden { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  height: 100%;
  overscroll-behavior-y: none;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

h1 { font-size: 1.5rem; margin: 0 0 4px; font-weight: 800; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 0 0 4px; font-weight: 700; }
p { margin: 0; }

#app {
  flex: 1 1 auto;
  min-height: 0;
  padding-top: var(--safe-t);
}

.view { display: flex; flex-direction: column; height: 100%; }
.view.hidden { display: none; }

.view-header {
  padding: 18px 18px 10px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.refresh-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 36px;
  height: 36px;
  font-size: 1.2rem;
  line-height: 1;
}

.refresh-btn:active { color: var(--text); }
.refresh-btn:disabled { opacity: 0.5; }

.view-subheader {
  color: var(--text-dim);
  font-size: 0.92rem;
  margin-top: 2px;
}

.view-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 16px calc(24px + var(--tab-h) + var(--safe-b));
  -webkit-overflow-scrolling: touch;
}

/* ---------- form controls ---------- */

.field-label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-dim);
  margin-bottom: 6px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.text-input {
  width: 100%;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 1rem;
  padding: 14px 14px;
  border-radius: 12px;
  min-height: 48px;
  margin-bottom: 16px;
  font-family: inherit;
}

.text-input:focus {
  outline: none;
  border-color: var(--accent);
}

.primary-btn {
  width: 100%;
  min-height: 52px;
  background: var(--accent);
  color: #1a0e02;
  font-size: 1.05rem;
  font-weight: 800;
  border: none;
  border-radius: 12px;
  margin-bottom: 10px;
}

.primary-btn:active { filter: brightness(0.92); }

.primary-btn:disabled {
  background: var(--bg-raised);
  color: var(--text-faint);
}

.secondary-btn {
  width: 100%;
  min-height: 48px;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.98rem;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.hint-text {
  color: var(--text-dim);
  font-size: 0.88rem;
  line-height: 1.4;
  margin-bottom: 14px;
}

.error-text {
  color: var(--red);
  font-size: 0.88rem;
  margin-top: 10px;
}
.error-text.hidden { display: none; }

/* ---------- scan ---------- */

.thumb-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.thumb-grid:empty { margin-bottom: 0; }

.thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg-raised);
  border: 1px solid var(--border);
}

.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.thumb .thumb-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(0,0,0,0.65);
  color: #fff;
  border: none;
  font-size: 15px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.thumb .thumb-queued {
  position: absolute;
  bottom: 4px;
  left: 4px;
  right: 4px;
  background: rgba(0,0,0,0.7);
  color: var(--yellow);
  font-size: 0.65rem;
  text-align: center;
  border-radius: 6px;
  padding: 2px 0;
}

.big-photo-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 64px;
  background: var(--bg-raised);
  border: 1.5px dashed var(--border);
  border-radius: 14px;
  color: var(--text);
  font-weight: 700;
  font-size: 1.02rem;
  margin-bottom: 16px;
}

.big-photo-btn-icon {
  font-size: 1.4rem;
  color: var(--accent);
}

/* ---------- cards (Results) ---------- */

.card-list { display: flex; flex-direction: column; gap: 12px; }

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.card.card-buy { border-color: var(--green-dim); box-shadow: inset 0 0 0 1px var(--green-dim); }
.card.card-ambiguous { border-color: var(--yellow-dim); }
.card.card-missing, .card.card-error { border-color: var(--red-dim); }

.card-top {
  display: flex;
  gap: 12px;
}

.card-thumb {
  flex: 0 0 auto;
  width: 64px;
  height: 64px;
  border-radius: 10px;
  background: var(--bg-raised);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  overflow: hidden;
  border: 1px solid var(--border);
}
.card-thumb img { width: 100%; height: 100%; object-fit: cover; }

.card-heading { flex: 1 1 auto; min-width: 0; }

.card-name {
  font-weight: 700;
  font-size: 1rem;
  line-height: 1.25;
  overflow-wrap: anywhere;
}

.card-verdict-pill {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 2px 8px;
  border-radius: 20px;
  margin-top: 4px;
}

.pill-buy { background: var(--green-dim); color: var(--green); }
.pill-ambiguous { background: var(--yellow-dim); color: var(--yellow); }
.pill-missing, .pill-error { background: var(--red-dim); color: var(--red); }

.card-body { margin-top: 12px; }

.price-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.shelf-price {
  font-size: 1.05rem;
  font-weight: 700;
}

.price-badge {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 20px;
  background: var(--bg-raised);
  color: var(--text-dim);
  border: 1px solid var(--border);
  white-space: nowrap;
}

.price-badge.tappable {
  color: var(--accent);
  border-color: var(--accent-dim);
  background: var(--accent-dim);
}

.profit-figure {
  font-size: 1.7rem;
  font-weight: 900;
  color: var(--green);
  line-height: 1.1;
}

.profit-figure.negative { color: var(--red); }

.profit-sub {
  color: var(--text-dim);
  font-size: 0.82rem;
  margin-top: 2px;
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
  margin-top: 12px;
  font-size: 0.85rem;
}

.detail-grid .k { color: var(--text-faint); }
.detail-grid .v { color: var(--text); font-weight: 600; }

.comps-line {
  margin-top: 10px;
  font-size: 0.82rem;
  color: var(--text-dim);
}

.thin-badge {
  display: inline-block;
  margin-top: 8px;
  font-size: 0.7rem;
  color: var(--yellow);
  background: var(--yellow-dim);
  padding: 2px 8px;
  border-radius: 20px;
  font-weight: 700;
}

.card-actions {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.card-actions button {
  flex: 1 1 0;
  min-height: 44px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  background: var(--bg-raised);
  color: var(--text);
}

.card-actions .bought-btn.done {
  background: var(--green-dim);
  color: var(--green);
  border-color: var(--green-dim);
}

.card-actions .deepdive-btn {
  color: var(--accent);
}

.retake-msg {
  margin-top: 10px;
  font-size: 0.88rem;
  color: var(--yellow);
}

.missing-msg {
  margin-top: 10px;
  font-size: 0.88rem;
  color: var(--red);
}

.empty-state {
  text-align: center;
  color: var(--text-dim);
  padding: 60px 20px;
  font-size: 0.95rem;
  line-height: 1.5;
}

/* ---------- rows (History / Bought) ---------- */

.row-list { display: flex; flex-direction: column; gap: 8px; }

.row-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  min-height: 44px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.row-main { min-width: 0; }
.row-title { font-weight: 700; overflow-wrap: anywhere; }
.row-sub { color: var(--text-dim); font-size: 0.82rem; margin-top: 2px; }

.row-status-pill {
  flex: 0 0 auto;
  font-size: 0.68rem;
  font-weight: 800;
  text-transform: uppercase;
  padding: 3px 9px;
  border-radius: 20px;
  background: var(--bg-raised);
  color: var(--text-dim);
  white-space: nowrap;
}

.totals-bar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 10px;
  margin-bottom: 16px;
  text-align: center;
}

.totals-bar .total-item .total-val {
  font-size: 1.15rem;
  font-weight: 800;
}
.totals-bar .total-item .total-label {
  font-size: 0.68rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-top: 2px;
}

/* ---------- tab bar ---------- */

.tab-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--tab-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  display: flex;
  background: rgba(21, 24, 29, 0.96);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  z-index: 50;
}

.tab-btn {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 0.7rem;
  font-weight: 700;
  min-height: 44px;
}

.tab-icon { font-size: 1.3rem; }

.tab-btn.active { color: var(--accent); }

/* ---------- status strip / offline banner ---------- */

.status-strip {
  flex: 0 0 auto;
  padding: 8px 16px;
  padding-top: calc(8px + var(--safe-t));
  background: var(--red-dim);
  color: var(--red);
  font-size: 0.82rem;
  font-weight: 700;
  text-align: center;
  z-index: 60;
}
.status-strip.warn { background: var(--yellow-dim); color: var(--yellow); }
.status-strip.hidden { display: none; }

.offline-banner {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(var(--tab-h) + var(--safe-b) + 10px);
  background: var(--yellow-dim);
  color: var(--yellow);
  font-size: 0.82rem;
  font-weight: 700;
  text-align: center;
  padding: 10px 12px;
  border-radius: 10px;
  z-index: 55;
}
.offline-banner.hidden { display: none; }

/* ---------- overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
}
.overlay.hidden { display: none; }

.overlay-card {
  width: 100%;
  max-width: 480px;
  background: var(--bg-raised);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  padding: 22px 20px calc(28px + var(--safe-b));
  border-top: 1px solid var(--border);
}

#login-overlay .overlay-card {
  border-radius: 20px;
  margin-bottom: auto;
  margin-top: auto;
}

.yen-input-row {
  display: flex;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 16px;
  padding: 0 4px 0 14px;
}
.yen-sign { color: var(--text-dim); font-weight: 700; font-size: 1.1rem; }
.yen-input {
  background: none;
  border: none;
  margin-bottom: 0;
  font-size: 1.2rem;
  font-weight: 700;
}

@media (min-width: 560px) {
  #app, .tab-bar { max-width: 480px; margin: 0 auto; }
  body { align-items: center; }
}
