:root {
  --bg: #0b0d12;
  --surface: #12151c;
  --surface-2: #181c25;
  --border: #232833;
  --text: #e8eaed;
  --text-dim: #838a99;
  --accent-ui: #6366f1;
  --cigarette: #f2f2f0;
  --alcohol: #ef4444;
  --marijuana: #22c55e;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  font-family: "Segoe UI", -apple-system, Arial, sans-serif;
  color: var(--text);
}

#app {
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* header */
#header {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 16px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

#legend {
  display: flex;
  flex-wrap: wrap;
  gap: clamp(8px, 3vw, 16px);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: clamp(11px, 3vw, 12px);
  color: var(--text-dim);
  white-space: nowrap;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex: none;
}

.dot-cigarette { background: var(--cigarette); }
.dot-alcohol { background: var(--alcohol); }
.dot-marijuana { background: var(--marijuana); }

#controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

#range-switch {
  display: flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

.range-btn {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: clamp(11px, 3vw, 12px);
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 999px;
  transition: background 0.15s ease, color 0.15s ease;
}

.range-btn.active {
  background: var(--accent-ui);
  color: #fff;
}

#undo-btn {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.15s ease, transform 0.08s ease-out;
}

#undo-btn svg {
  width: 16px;
  height: 16px;
}

#undo-btn:active:not(:disabled) {
  transform: scale(0.9);
}

#undo-btn:disabled {
  opacity: 0.35;
}

/* chart */
#chart-wrap {
  flex: 1 1 auto;
  min-height: 0;
  background: var(--bg);
  position: relative;
}

#chart {
  width: 100%;
  height: 100%;
  display: block;
}

/* buttons */
#buttons {
  flex: 0 0 auto;
  display: flex;
  flex-direction: row;
  gap: 10px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--bg);
}

.btn {
  flex: 1;
  min-height: 100px;
  height: clamp(100px, 18vh, 150px);
  border: 1px solid var(--border);
  border-top: 3px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: transform 0.08s ease-out, box-shadow 0.08s ease-out, border-color 0.08s ease-out;
}

.btn .icon {
  width: clamp(28px, 8vw, 34px);
  height: clamp(28px, 8vw, 34px);
}

.btn .label {
  font-size: clamp(12px, 3.4vw, 14px);
  font-weight: 600;
  color: var(--text-dim);
  text-align: center;
}

.btn .count {
  font-size: clamp(20px, 6vw, 26px);
  font-weight: 800;
  color: var(--text);
  display: flex;
  align-items: baseline;
  gap: 3px;
}

.btn .count .unit {
  font-size: 0.4em;
  font-weight: 600;
  color: var(--text-dim);
}

.btn:active,
.btn.pressed {
  transform: scale(0.96);
}

.btn-cigarette { color: var(--cigarette); border-top-color: var(--cigarette); }
.btn-alcohol { color: var(--alcohol); border-top-color: var(--alcohol); }
.btn-marijuana { color: var(--marijuana); border-top-color: var(--marijuana); }

.btn-cigarette.pressed { box-shadow: 0 0 0 2px rgba(242,242,240,0.35), 0 0 24px rgba(242,242,240,0.25); }
.btn-alcohol.pressed { box-shadow: 0 0 0 2px rgba(239,68,68,0.35), 0 0 24px rgba(239,68,68,0.35); }
.btn-marijuana.pressed { box-shadow: 0 0 0 2px rgba(34,197,94,0.35), 0 0 24px rgba(34,197,94,0.35); }

.btn-cigarette .count { color: var(--cigarette); }
.btn-alcohol .count { color: var(--alcohol); }
.btn-marijuana .count { color: var(--marijuana); }

/* narrow / portrait phones: keep header compact on one or two tidy rows */
@media (max-width: 480px) {
  #header {
    padding-left: 12px;
    padding-right: 12px;
  }
  .legend-item .legend-text {
    display: none;
  }
  .btn .label {
    font-size: clamp(11px, 3.2vw, 13px);
  }
}

/* quantity picker overlay */
#qty-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
  z-index: 150;
  padding: 24px;
}

#qty-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

#qty-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 20px;
  width: min(480px, 92vw);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

#qty-title {
  text-align: center;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 14px;
}

#qty-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(76px, 1fr));
  gap: 10px;
}

.qty-chip {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 12px;
  padding: 14px 8px;
  font-size: 16px;
  font-weight: 700;
  transition: transform 0.08s ease-out, background 0.1s ease;
}

.qty-chip:active {
  transform: scale(0.94);
  background: var(--accent-ui);
}

/* gif overlay */
#gif-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 100;
}

#gif-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

#gif-box {
  max-width: min(420px, 88vw);
  max-height: min(380px, 60vh);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

#gif-img {
  display: block;
  max-width: min(420px, 88vw);
  max-height: min(380px, 60vh);
  opacity: 0;
  transition: opacity 0.15s ease;
}

#gif-img.loaded {
  opacity: 1;
}

/* toast */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 200;
  white-space: nowrap;
}

#toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}
