:root {
  --bg: #0a0a0f;
  --bg-elevated: #17171f;
  --card: #131319;
  --card-2: #17171f;
  --border: #232330;
  --text: #f5f5f7;
  --text-dim: #8b8b9c;
  --text-faint: #56566a;
  --accent: #8b5cf6;
  --accent-2: #ec4899;
  --accent-grad: linear-gradient(135deg, #8b5cf6, #ec4899);
  --accent-dim: #8b5cf626;
  --danger: #fb7185;
  --good: #34d399;
  --warn: #fbbf24;
  --radius: 16px;
  --radius-sm: 10px;
  color-scheme: dark;
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(600px 300px at 15% -5%, #8b5cf61a, transparent),
    radial-gradient(500px 260px at 100% 0%, #ec489914, transparent),
    var(--bg);
  background-attachment: fixed;
}

/* ---------- topbar ---------- */

.topbar {
  padding: 12px 20px 12px;
  padding-top: calc(12px + env(safe-area-inset-top));
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(20px) saturate(150%);
  z-index: 10;
  border-bottom: 1px solid var(--border);
}
.topbar-brand { display: flex; align-items: center; gap: 10px; }
.brand-badge {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 10px;
  background: var(--accent-grad);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  box-shadow: 0 4px 14px -4px #8b5cf680;
}
.topbar h1 { margin: 0; font-size: 18px; font-weight: 800; letter-spacing: -0.01em; }
.topbar-sub { font-size: 12.5px; color: var(--text-dim); margin-top: 1px; }

#app {
  padding: 14px 16px 24px;
  padding-top: calc(70px + env(safe-area-inset-top));
  padding-bottom: calc(84px + env(safe-area-inset-bottom));
}

.view { display: block; animation: fadeIn 0.18s ease; }
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- cards ---------- */

.card {
  background: linear-gradient(160deg, var(--card-2), var(--card));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 1px 0 0 #ffffff08 inset, 0 10px 24px -16px #00000080;
}

.field-label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 12px;
}

.row { display: flex; gap: 12px; }
.row.two > * { flex: 1; min-width: 0; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; color: var(--text-dim); font-weight: 600; }
.field em { font-style: normal; opacity: 0.7; font-size: 11px; font-weight: 400; }

input[type="text"],
input[type="datetime-local"],
select,
textarea {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 11px 12px;
  font-size: 16px;
  font-family: inherit;
  width: 100%;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input[type="text"]:focus,
input[type="datetime-local"]:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
textarea { resize: vertical; }

/* ---------- severity slider ---------- */

#severityValue { color: var(--accent); font-weight: 800; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 10px;
  border-radius: 999px;
  background: linear-gradient(90deg, #34d399, #fbbf24 50%, #fb7185);
  outline: none;
  margin: 6px 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--severity-color, var(--accent));
  box-shadow: 0 2px 8px #00000060, 0 0 0 5px color-mix(in srgb, var(--severity-color, var(--accent)) 25%, transparent);
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--severity-color, var(--accent));
  box-shadow: 0 2px 8px #00000060;
  cursor: pointer;
}

.scale-labels {
  display: flex;
  justify-content: space-between;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-faint);
  margin-top: 2px;
}

/* ---------- chips ---------- */

.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.chip {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 15px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.12s, background 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s;
}
.chip:active { transform: scale(0.94); }
.chip.selected {
  background: var(--accent-grad);
  border-color: transparent;
  color: #fff;
  font-weight: 700;
  box-shadow: 0 4px 14px -6px #8b5cf690;
  transform: scale(1.03);
}
.chip.removable::after { content: " ✕"; opacity: 0.7; font-size: 11px; }

#customTrigger { margin-top: 6px; }

/* ---------- buttons ---------- */

.btn-primary {
  width: 100%;
  background: var(--accent-grad);
  color: #fff;
  border: none;
  border-radius: 14px;
  padding: 15px;
  font-size: 16px;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  margin-top: 6px;
  box-shadow: 0 8px 20px -8px #8b5cf690;
  transition: transform 0.12s, box-shadow 0.12s;
}
.btn-primary:active { transform: scale(0.98); box-shadow: 0 4px 12px -6px #8b5cf690; }

.btn-secondary {
  width: 100%;
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  font-size: 14.5px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  margin-top: 8px;
  transition: transform 0.12s;
}
.btn-secondary:active { transform: scale(0.98); }

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

.tabbar {
  display: flex;
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(20px) saturate(150%);
  padding: 6px 10px calc(6px + env(safe-area-inset-bottom));
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  gap: 4px;
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-faint);
  font-family: inherit;
  font-size: 10.5px;
  font-weight: 700;
  padding: 8px 4px 7px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: pointer;
  border-radius: 14px;
  transition: background 0.15s, color 0.15s;
}
.tab-icon { line-height: 1; display: flex; }
.tab.active {
  color: var(--accent);
  background: var(--accent-dim);
}
.tab:active { transform: scale(0.95); }

/* ---------- history ---------- */

.history-header {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-dim);
  margin-bottom: 10px;
  padding: 0 2px;
}

.history-list { display: flex; flex-direction: column; gap: 10px; }

.entry-card {
  background: linear-gradient(160deg, var(--card-2), var(--card));
  border: 1px solid var(--border);
  border-left: 3px solid var(--sev-color, var(--accent));
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: 0 1px 0 0 #ffffff08 inset, 0 10px 24px -16px #00000080;
}
.entry-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}
.entry-date { font-weight: 800; font-size: 15px; letter-spacing: -0.01em; }
.entry-meta { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

.severity-badge {
  font-size: 13px;
  font-weight: 800;
  padding: 5px 12px;
  border-radius: 999px;
  color: #0a0a0f;
  flex-shrink: 0;
  margin-left: 10px;
}

.entry-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.tag {
  font-size: 12px;
  font-weight: 600;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
  color: var(--text-dim);
}

.entry-details {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 14px;
  display: none;
}
.entry-details.open { display: block; animation: fadeIn 0.15s ease; }
.entry-details .detail-row { margin-bottom: 8px; }
.entry-details .detail-label { color: var(--text-faint); font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 2px; }

.entry-actions { display: flex; gap: 8px; margin-top: 10px; }
.entry-actions button {
  flex: 1;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 9px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.12s;
}
.entry-actions button:active { transform: scale(0.96); }
.entry-actions button.delete { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); background: transparent; }

/* ---------- empty states ---------- */

.empty-state {
  text-align: center;
  padding: 64px 20px;
  color: var(--text-dim);
}
.empty-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 16px;
  border-radius: 18px;
  background: var(--accent-dim);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
}
.empty-state p { margin: 4px 0; font-weight: 600; }
.empty-state p.muted { font-weight: 400; }
.muted { color: var(--text-dim); }
.small { font-size: 13px; }

/* ---------- insight stats ---------- */

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}
.stat-card {
  position: relative;
  background: linear-gradient(160deg, var(--card-2), var(--card));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 1px 0 0 #ffffff08 inset, 0 10px 24px -16px #00000080;
}
.stat-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 9px;
  margin-bottom: 10px;
}
.stat-icon.icon-total { background: #8b5cf626; color: #a78bfa; }
.stat-icon.icon-month { background: #ec489926; color: #f472b6; }
.stat-icon.icon-severity { background: #fb718526; color: #fb7185; }
.stat-icon.icon-duration { background: #34d39926; color: #34d399; }
.stat-value { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; }
.stat-label { font-size: 12px; font-weight: 600; color: var(--text-dim); margin-top: 2px; }

/* ---------- rank lists ---------- */

.rank-list { display: flex; flex-direction: column; gap: 12px; }
.rank-row { display: flex; align-items: center; gap: 10px; }
.rank-badge {
  flex: 0 0 20px;
  height: 20px;
  border-radius: 6px;
  background: var(--bg-elevated);
  color: var(--text-faint);
  font-size: 10.5px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}
.rank-row:nth-child(1) .rank-badge { background: #8b5cf633; color: #c4b5fd; }
.rank-row:nth-child(2) .rank-badge { background: #8b5cf622; color: #a78bfa; }
.rank-name { flex: 0 0 108px; font-size: 13px; font-weight: 600; }
.rank-bar-track { flex: 1; height: 8px; background: var(--bg-elevated); border-radius: 999px; overflow: hidden; }
.rank-bar-fill { height: 100%; background: var(--accent-grad); border-radius: 999px; }
.rank-count { flex: 0 0 22px; font-size: 12px; font-weight: 700; color: var(--text-dim); text-align: right; }

canvas { width: 100%; display: block; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  bottom: calc(90px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: color-mix(in srgb, var(--bg-elevated) 92%, transparent);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 11px 20px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 10px 30px -10px #000000a0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
