:root {
  --surface: #ffffff;
  --bg: var(--surface);
  --text: #0f172a;
  --muted: #64748b;
  --secondary-label: rgba(60, 60, 67, 0.6);
  --line: #e5e7eb;
  --accent: #003a63;
  --carbs: rgb(96, 160, 148);
  --tab-inactive: var(--accent);
  --tab-active: #007aff;
  --control-bg: #eef2f6;
  --range-radius: 8px;
  --range-radius-inner: 6px;
  --max: 420px;
  --page-pad: 16px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Inter", sans-serif;
  background: var(--bg);
  color: var(--text);
}

html,
body {
  height: 100%;
  overscroll-behavior-y: none;
}

#homeView,
.settings-view,
.events-view,
.editor-view {
  padding-left: max(var(--page-pad), env(safe-area-inset-left));
  padding-right: max(var(--page-pad), env(safe-area-inset-right));
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
}

/* Navigation bar (iOS-style compact) */
.nav-bar {
  position: sticky;
  top: 0;
  z-index: 40;
  padding-top: env(safe-area-inset-top);
  margin-left: calc(-1 * max(var(--page-pad), env(safe-area-inset-left)));
  margin-right: calc(-1 * max(var(--page-pad), env(safe-area-inset-right)));
  padding-left: max(var(--page-pad), env(safe-area-inset-left));
  padding-right: max(var(--page-pad), env(safe-area-inset-right));
  background: rgba(247, 249, 251, 0.84);
  border-bottom: 1px solid var(--line);
  backdrop-filter: saturate(180%) blur(18px);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
}

@supports (background: color-mix(in srgb, white, transparent)) {
  .nav-bar {
    background: color-mix(in srgb, var(--bg) 84%, transparent);
  }
}

.nav-bar__inner {
  max-width: var(--max);
  margin: 0 auto;
  height: 44px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}

.nav-bar__left,
.nav-bar__right {
  display: flex;
  align-items: center;
  min-width: 0;
}

.nav-bar__left {
  justify-content: flex-start;
}

.nav-bar__right {
  justify-content: flex-end;
}

.nav-bar__title {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  color: var(--text);
}

.nav-bar--root {
  background: transparent;
  border-bottom: 1px solid transparent;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}

.nav-back-btn {
  border: none;
  background: transparent;
  color: var(--tab-active);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 6px;
  margin-left: -6px;
  font-size: 17px;
  cursor: pointer;
}

.nav-back-btn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2.2;
}

.nav-back-btn__label {
  line-height: 1;
}

/* Day switcher (content control, not navigation) */
.day-switcher {
  max-width: var(--max);
  margin: 10px auto 0;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  padding: 0 4px;
  gap: 4px;
  height: 32px;
}

.day-switcher__btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--tab-active);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  place-self: center;
  position: relative;
}

.day-switcher__btn::before {
  content: "";
  position: absolute;
  inset: -6px;
}

.day-switcher__btn svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2.2;
}

.day-switcher__btn:active {
  background: rgba(0, 122, 255, 0.12);
}

.day-switcher__btn:disabled {
  opacity: 0.3;
  cursor: default;
}

.day-switcher__label {
  font-size: 15px;
  font-weight: 500;
  text-align: center;
  color: var(--secondary-label);
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-status {
  max-width: var(--max);
  margin: 12px auto 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 8px;
  height: 20px;
}

.top-status .online-dot {
  margin-left: 0;
}

.online-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #22c55e;
  margin-left: 6px;
}

.online-dot.offline {
  background: #ef4444;
}

/* Graph */
.graph {
  max-width: var(--max);
  margin: 16px auto 0;
}

.chart-label {
  padding: 0 8px;
  margin: 0 0 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--secondary-label);
  letter-spacing: 0.01em;
}

.chart-label--spaced {
  margin-top: 12px;
}

.chart-container {
  position: relative;
  height: 175px;
}

.bolus-chart-container {
  position: relative;
  height: 80px;
  margin-top: 0;
}

.carbs-chart-container {
  position: relative;
  height: 80px;
  margin-top: 0;
}

#glucoseChart,
#bolusChart,
#carbsChart {
  width: 100% !important;
  height: 100% !important;
}

/* Events */
.events {
  max-width: var(--max);
  margin: 0 auto;
}

.events-view .events {
  margin: 8px auto 0;
}

.events-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0 4px;
}

.events-title {
  font-size: 15px;
  font-weight: 600;
}

.queue-send-btn {
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
}

.history-list {
  margin-top: 4px;
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 0 8px;
}

.event-empty {
  padding: 14px 12px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.event-item {
  position: relative;
  overflow: hidden;
  background: var(--surface);
  border: none;
  border-top: 1px solid var(--line);
  border-radius: 0;
  margin: 0;
}

.event-item--pending .event-swipe {
  background: #f8fafc;
}

.event-item--failed .event-swipe {
  background: #fef2f2;
}

.event-item--pending .event-icon {
  background: #e2e8f0;
  color: var(--text);
}

.event-item--failed .event-icon {
  background: #fecaca;
  color: #b91c1c;
}

.event-swipe {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  background: var(--surface);
  transition: transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
  touch-action: pan-y;
  will-change: transform;
}

.event-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.event-icon svg {
  width: 18px;
  height: 18px;
  stroke: #fff;
  fill: none;
  stroke-width: 2;
}

.event-actions {
  display: flex;
  align-items: center;
}

.icon-btn-ghost {
  border: none;
  background: transparent;
  color: var(--muted);
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

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

.icon-btn-ghost svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
}

.icon-btn-ghost:disabled {
  opacity: 0.35;
  cursor: default;
}

.event-status {
  margin-left: 6px;
  font-size: 14px;
}

.event-menu {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 0;
  display: flex;
  align-items: stretch;
  justify-content: flex-end;
  border-radius: 0;
}

.event-action {
  min-width: 88px;
  padding: 0 12px;
  border: none;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 0;
  white-space: nowrap;
  cursor: pointer;
}

.event-action--delete {
  background: #ef4444;
}

.event-action--edit {
  background: var(--tab-active);
}

.event-action--retry {
  background: #16a34a;
}

.event-row {
  display: flex;
  align-items: center;
  padding: 14px 0;
}

.event-icon {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 44px;
  margin-right: 0;
}

.event-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.event-top {
  display: flex;
  align-items: center;
  gap: 12px;
}

.event-metrics {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  flex: 1;
  min-width: 0;
}

.metric-chip {
  display: inline-flex;
  align-items: center;
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1;
  border: 1px solid var(--line);
  background: var(--control-bg);
  background: color-mix(in srgb, var(--surface) 88%, var(--control-bg));
  color: var(--text);
}

.metric-chip--insulin {
  border-color: rgba(0, 122, 255, 0.22);
  background: rgba(0, 122, 255, 0.12);
  color: var(--tab-active);
  border-color: color-mix(in srgb, var(--tab-active) 30%, var(--line));
  background: color-mix(in srgb, var(--tab-active) 12%, var(--surface));
  color: color-mix(in srgb, var(--tab-active) 90%, #0f172a);
}

.metric-chip--carbs {
  border-color: rgba(96, 160, 148, 0.28);
  background: rgba(96, 160, 148, 0.12);
  color: var(--carbs);
  border-color: color-mix(in srgb, var(--carbs) 35%, var(--line));
  background: color-mix(in srgb, var(--carbs) 12%, var(--surface));
  color: color-mix(in srgb, var(--carbs) 92%, #0f172a);
}

.metric-chip--note {
  border-color: rgba(100, 116, 139, 0.25);
  background: rgba(100, 116, 139, 0.1);
  border-color: color-mix(in srgb, var(--muted) 25%, var(--line));
  background: color-mix(in srgb, var(--muted) 10%, var(--surface));
  color: var(--muted);
}

.event-notes {
  margin-top: 8px;
  font-size: 14px;
  line-height: 1.35;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
}

.event-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.event-title-row .event-title {
  flex: 1;
  min-width: 0;
}

.event-badge {
  flex: 0 0 auto;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 8px;
  border-radius: 999px;
  line-height: 1;
}

.event-badge--pending {
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--muted);
}

.event-badge--failed {
  background: #fee2e2;
  border: 1px solid #fecaca;
  color: #b91c1c;
}

.event-badge--sent {
  background: #ecfdf5;
  border: 1px solid #bbf7d0;
  color: #15803d;
}

.event-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.event-time {
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
}

.event-meta {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.event-status-icons {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.status-icon {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}

.status-icon svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
}

.status-icon--failed {
  color: #ef4444;
}

.event-edit {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
}

hr {
  border: none;
  border-bottom: 1px solid var(--line);
  margin: 0;
}

/* Bottom spacer */
.bottom-spacer {
  height: 110px;
}

/* Tabbar */
.tabbar {
  position: fixed;
  bottom: 0;
  width: 100%;
  left: 0;
  transform: none;
  background: var(--surface);
  border-top: 1px solid var(--line);
  box-shadow: none;
  padding-top: 6px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  z-index: 50;
}

.tabbar-inner {
  max-width: var(--max);
  margin: 0 auto;
  width: 100%;
  height: 58px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  align-items: center;
  justify-items: center;
  padding: 0 18px;
}

.tab-spacer {
  width: 100%;
  height: 1px;
  pointer-events: none;
}

.tab-item {
  background: none;
  border: none;
  color: var(--tab-inactive);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 8px;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
}

.tab-item.active {
  color: var(--tab-active);
  font-weight: 600;
}

.tab-icon {
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tab-icon svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
}

.tab-label {
  font-size: 11px;
}

.tab-center-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 0;
  box-shadow: none;
  cursor: pointer;
}

.tab-center-btn svg {
  width: 26px;
  height: 26px;
  stroke: currentColor;
}

/* Update banner */
.update-banner {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(78px + env(safe-area-inset-bottom));
  width: min(var(--max), calc(100% - 20px));
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 6px 16px rgba(15, 23, 42, 0.12);
  z-index: 60;
}

.update-banner__text {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
}

.update-banner__btn.primary-btn {
  width: auto;
  padding: 8px 12px;
  font-size: 14px;
  border-radius: 9px;
}

/* Settings */
.settings-view {
  padding-top: 0;
}

.settings-content {
  max-width: var(--max);
  margin: 0 auto;
  padding-top: 10px;
}

.editor-content {
  max-width: var(--max);
  margin: 0 auto;
  padding-top: 10px;
}

.editor-content .settings-card {
  border-top: none;
}

.editor-hint {
  font-size: 13px;
  line-height: 1.35;
  margin: 4px 0 12px;
}

.settings-card {
  background: transparent;
  border: none;
  border-top: 1px solid var(--line);
  border-radius: 0;
  padding: 16px 0;
  margin: 0;
}

.settings-content .settings-card:first-child {
  border-top: none;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}

.field-label {
  font-size: 12px;
  color: var(--muted);
}

input {
  width: 100%;
  min-width: 0;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  padding: 12px;
  font-size: 15px;
  line-height: 1.2;
}

textarea {
  width: 100%;
  min-width: 0;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  padding: 12px;
  font-size: 15px;
  line-height: 1.3;
  font-family: inherit;
  resize: vertical;
}

input[type="datetime-local"] {
  display: block;
  max-width: 100%;
  min-height: 46px;
  -webkit-appearance: none;
  appearance: none;
  text-align: left;
}

input[type="datetime-local"]::-webkit-date-and-time-value {
  text-align: left;
}

input[type="datetime-local"]::-webkit-datetime-edit,
input[type="datetime-local"]::-webkit-datetime-edit-fields-wrapper {
  padding: 0;
}

.input--invalid::-webkit-date-and-time-value {
  text-align: left;
}

.input--invalid {
  border-color: #fca5a5;
  box-shadow: 0 0 0 4px rgba(239, 68, 68, 0.12);
}

@supports (-webkit-touch-callout: none) {
  input {
    font-size: 16px;
  }

  textarea {
    font-size: 16px;
  }
}

.primary-btn,
.secondary-btn,
.danger-btn {
  border: none;
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn:disabled,
.secondary-btn:disabled,
.danger-btn:disabled {
  background: #e2e8f0;
  color: var(--muted);
  cursor: default;
  opacity: 1;
}

.primary-btn {
  background: var(--accent);
  color: #fff;
  width: 100%;
}

.secondary-btn {
  background: var(--control-bg);
  color: var(--text);
}

.danger-btn {
  background: #fef2f2;
  color: #b91c1c;
  border: 1px solid #fecaca;
  width: 100%;
  margin-top: 10px;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  pointer-events: none;
}

.modal-card {
  position: relative;
  z-index: 1;
  background: var(--surface);
  border-radius: 14px;
  padding: 18px 16px;
  width: min(420px, 92%);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.modal-title {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 10px;
}

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

.modal-actions .secondary-btn,
.modal-actions .primary-btn {
  flex: 1;
}
