:root {
  color-scheme: light;
  --bg: #f4f7f7;
  --surface: #ffffff;
  --surface-soft: #f8faf9;
  --text: #172422;
  --muted: #667571;
  --line: #dce5e2;
  --green: #0f766e;
  --green-soft: #def3ee;
  --red: #b42318;
  --red-soft: #fee4e2;
  --amber: #9a6700;
  --amber-soft: #fff3d6;
  --shadow: 0 1px 2px rgba(15, 35, 31, 0.04), 0 12px 30px rgba(15, 35, 31, 0.06);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
button, code, input, select { font: inherit; }
button, input, select { -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid rgba(15, 118, 110, 0.18); outline-offset: 2px; }

.shell { width: min(1180px, calc(100% - 32px)); margin: 0 auto; padding: 34px 0 26px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 24px; }
.eyebrow, .card-label { margin: 0 0 7px; color: var(--muted); font-size: 0.72rem; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; }
.title-meta { display: flex; align-items: center; gap: 10px; margin-bottom: 7px; }
.title-meta .eyebrow { margin: 0; }
.title-meta .badge { padding: 5px 8px; font-size: 0.7rem; }
.title-meta .status-dot { width: 6px; height: 6px; }
h1 { margin: 0; font-size: clamp(1.8rem, 4vw, 2.45rem); letter-spacing: -0.045em; }
h2 { margin: 0; font-size: 1.05rem; letter-spacing: -0.015em; }
.header-actions { display: flex; align-items: center; gap: 10px; }
.button { display: inline-flex; align-items: center; justify-content: center; min-height: 40px; border: 1px solid var(--green); border-radius: 9px; background: var(--green); color: white; padding: 10px 14px; font-weight: 750; cursor: pointer; }
.button-ghost { background: #e7ecea; border-color: #d4dfdc; color: #344541; }
.button-ghost:hover { border-color: var(--red, #ef4444); color: var(--red, #ef4444); background: rgba(239,68,68,0.06); }
.button:hover { filter: brightness(0.95); }
.button:disabled { cursor: wait; opacity: 0.62; }
.badge { display: inline-flex; align-items: center; gap: 7px; border-radius: 999px; padding: 8px 11px; font-size: 0.82rem; font-weight: 750; }
.badge-neutral { color: #52605d; background: #e7ecea; }
.badge-ok { color: #096c58; background: var(--green-soft); }
.badge-error { color: var(--red); background: var(--red-soft); }
.badge-warn { color: var(--amber); background: var(--amber-soft); }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

.notice { margin-bottom: 18px; border: 1px solid #eccf8d; border-radius: 12px; background: #fff8e7; padding: 14px 16px; color: #73510a; }
.notice strong { margin-right: 4px; }
.hidden { display: none !important; }

.summary-grid { display: grid; grid-template-columns: 1.65fr repeat(2, 1fr); gap: 14px; }
.card { border: 1px solid var(--line); border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); }
.current-card, .metric-card { min-height: 175px; padding: 20px; }
.metric-card { display: flex; flex-direction: column; }
.metric-card .muted { margin-top: auto; }
.polling-interval-control { display: grid; gap: 6px; margin-top: 12px; }
.polling-interval-control > span { color: var(--muted); font-size: 0.7rem; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; }
.polling-interval-control select { width: 100%; min-height: 36px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); padding: 7px 10px; font-size: 0.82rem; font-weight: 700; cursor: pointer; }
.polling-interval-control select:focus { border-color: var(--green); }
.polling-interval-control select:disabled { cursor: wait; opacity: 0.62; }
.current-row { display: flex; align-items: baseline; gap: 8px; margin-top: 8px; }
.current-value { font-size: clamp(3.2rem, 7vw, 4.8rem); line-height: 0.92; letter-spacing: -0.075em; }
.unit { color: var(--muted); font-weight: 700; }
.trend { margin-left: auto; color: var(--green); font-size: 2.25rem; font-weight: 800; }
.metric { display: block; margin-top: 17px; font-size: 2rem; letter-spacing: -0.045em; }
.metric-small { font-size: 1.35rem; line-height: 1.25; }
.muted { color: var(--muted); font-size: 0.86rem; }
.small { font-size: 0.76rem; }
.freshness { height: 6px; margin-top: 7px; overflow: hidden; border-radius: 99px; background: #e7ecea; }
.freshness span { display: block; width: 0%; height: 100%; border-radius: inherit; background: var(--green); transition: width 300ms ease, background 300ms ease; }
.freshness-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-top: 15px; color: var(--muted); font-size: 0.72rem; }
.freshness-heading > span { font-weight: 800; letter-spacing: 0.05em; text-transform: uppercase; }
.freshness-heading strong { color: var(--text); font-size: 0.76rem; }
.freshness-scale { display: flex; justify-content: space-between; margin-top: 5px; color: var(--muted); font-size: 0.65rem; }
.current-card p { margin: 10px 0 0; }

.chart-card, .table-card { margin-top: 14px; padding: 20px; }
.section-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 18px; }
.chart-header { display: flex; align-items: center; justify-content: space-between; gap: 20px; margin-bottom: 18px; }
.chart-day-controls { display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.day-btn { min-height: 36px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); padding: 7px 12px; font-size: 0.82rem; font-weight: 750; cursor: pointer; transition: border-color 150ms ease, color 150ms ease, background 150ms ease; }
.day-btn-icon { width: 36px; padding-inline: 0; font-size: 1rem; }
.day-btn:hover:not(:disabled) { border-color: var(--green); color: var(--green); background: var(--surface-soft); }
.day-btn:disabled { opacity: 0.38; cursor: not-allowed; }
.day-btn-today { background: var(--green-soft); border-color: #b9e3d9; color: var(--green); }
.day-btn-today:hover:not(:disabled) { background: #ccefe6; }
.select-wrapper { position: relative; display: inline-flex; align-items: center; min-width: 240px; max-width: 360px; }
.select-wrapper::after {
  position: absolute;
  top: 50%;
  right: 13px;
  width: 7px;
  height: 7px;
  transform: translateY(-65%) rotate(45deg);
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  pointer-events: none;
  content: "";
}
.day-select {
  width: 100%;
  height: 36px;
  min-height: 36px;
  -webkit-appearance: none;
  appearance: none;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background-color: var(--surface);
  background-image: none;
  padding: 7px 36px 7px 12px;
  color: var(--text);
  font-size: 0.84rem;
  font-weight: 700;
  line-height: normal;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  outline: none;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}
.day-select::-ms-expand { display: none; }
.day-select:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12); }

.day-stats-bar { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); overflow: hidden; margin-bottom: 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-soft); }
.chart-stat { display: grid; gap: 5px; min-width: 0; padding: 12px 14px; border-right: 1px solid var(--line); }
.chart-stat:last-child { border-right: 0; }
.chart-stat .stat-label { overflow: hidden; color: var(--muted); font-size: 0.67rem; font-weight: 800; letter-spacing: 0.065em; text-overflow: ellipsis; text-transform: uppercase; white-space: nowrap; }
.chart-stat strong { overflow: hidden; font-size: 0.9rem; letter-spacing: -0.015em; text-overflow: ellipsis; white-space: nowrap; }
.chart-range-text { margin-left: auto; }

.chart-controls-toolbar { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px; margin-bottom: 12px; }
.zoom-group { display: inline-flex; align-items: center; gap: 3px; }
.toolbar-label { margin-right: 4px; color: var(--muted); font-size: 0.68rem; font-weight: 800; letter-spacing: 0.065em; text-transform: uppercase; white-space: nowrap; }
.zoom-step-btn { min-width: 36px; border: 1px solid transparent; border-radius: 7px; background: transparent; color: var(--muted); padding: 6px 8px; font-size: 0.78rem; font-weight: 750; cursor: pointer; transition: color 130ms ease, background 130ms ease, border-color 130ms ease; }
.zoom-step-btn:hover { color: var(--text); background: rgba(15, 118, 110, 0.07); }
.zoom-step-btn.active { border-color: #b9e3d9; background: var(--green-soft); color: var(--green); }
.scroll-hint { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: 0.74rem; }
.scroll-hint::before { width: 14px; height: 1px; background: var(--muted); box-shadow: -3px -2px 0 -1px var(--muted), -3px 2px 0 -1px var(--muted), 3px -2px 0 -1px var(--muted), 3px 2px 0 -1px var(--muted); content: ""; }

.chart-scroll-container { position: relative; width: 100%; height: 350px; overflow-x: auto; overflow-y: hidden; border: 1px solid var(--line); border-radius: 12px; background: linear-gradient(180deg, #ffffff 0%, #fbfcfc 100%); cursor: grab; user-select: none; }
.chart-scroll-container.is-dragging { cursor: grabbing; }
.chart-scroll-container::-webkit-scrollbar { height: 8px; }
.chart-scroll-container::-webkit-scrollbar-track { background: var(--surface-soft); border-radius: 8px; }
.chart-scroll-container::-webkit-scrollbar-thumb { background: #c6d5d1; border-radius: 8px; }
.chart-scroll-container::-webkit-scrollbar-thumb:hover { background: #96aca7; }

.chart-canvas-wrapper { position: relative; min-width: 100%; height: 100%; }
#glucoseChart { display: block; height: 100%; }
.chart-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 0.9rem; pointer-events: none; }
.chart-tooltip { position: absolute; z-index: 5; display: grid; gap: 2px; min-width: 116px; padding: 9px 11px; transform: translate(-50%, calc(-100% - 12px)); border: 1px solid #263835; border-radius: 9px; background: #172422; color: white; box-shadow: 0 8px 24px rgba(15, 35, 31, 0.2); pointer-events: none; }
.chart-tooltip::after { position: absolute; top: 100%; left: 50%; width: 9px; height: 9px; transform: translate(-50%, -5px) rotate(45deg); background: #172422; content: ""; }
.chart-tooltip.tooltip-below { transform: translate(-50%, 12px); }
.chart-tooltip.tooltip-below::after { top: 0; transform: translate(-50%, -5px) rotate(45deg); }
.chart-tooltip strong { font-size: 0.94rem; }
.chart-tooltip span { color: #c6d2cf; font-size: 0.72rem; white-space: nowrap; }
.chart-legend { display: flex; align-items: center; flex-wrap: wrap; gap: 18px; margin-top: 12px; color: var(--muted); font-size: 0.76rem; }
.chart-legend span { display: inline-flex; align-items: center; gap: 7px; }
.legend-line { width: 18px; height: 3px; border-radius: 4px; background: var(--green); }
.legend-band { width: 18px; height: 10px; border: 1px solid #b9d9cf; background: #eaf5f1; }

.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { border-bottom: 1px solid var(--line); padding: 12px 10px; text-align: left; white-space: nowrap; }
th { color: var(--muted); font-size: 0.72rem; letter-spacing: 0.07em; text-transform: uppercase; }
td { font-size: 0.88rem; }
tbody tr:last-child td { border-bottom: 0; }
.reading-value { font-weight: 800; }
.sync-result { display: inline-flex; align-items: center; gap: 6px; border-radius: 999px; padding: 5px 8px; font-size: 0.75rem; font-weight: 800; }
.sync-result::before { width: 6px; height: 6px; border-radius: 50%; background: currentColor; content: ""; }
.sync-result-ok { color: #096c58; background: var(--green-soft); }
.sync-result-repeated { color: var(--amber); background: var(--amber-soft); }
.sync-result-error { color: var(--red); background: var(--red-soft); }
.sync-detail { max-width: 430px; white-space: normal; color: var(--muted); }
.annotation-cell { min-width: 145px; white-space: normal; }
.annotation-open-button { display: grid; gap: 3px; width: 100%; min-height: 38px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); padding: 7px 10px; font-size: 0.76rem; font-weight: 750; text-align: left; cursor: pointer; }
.annotation-open-button:hover { border-color: var(--green); color: var(--green); }
.annotation-open-button.has-annotation { border-color: #b9e3d9; background: var(--green-soft); color: var(--green); }
.annotation-open-button span { font-size: 0.65rem; font-weight: 650; opacity: 0.8; }
.annotation-file-button, .annotation-remove-button { min-height: 34px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); color: var(--text); padding: 7px 10px; font-size: 0.76rem; font-weight: 750; cursor: pointer; }
.annotation-file-button:hover, .annotation-remove-button:hover { border-color: var(--green); color: var(--green); }
.annotation-file-button input { position: absolute; width: 1px; height: 1px; overflow: hidden; opacity: 0; pointer-events: none; }
.annotation-remove-button { color: var(--red); }
.sync-row-error td { background: #fff9f8; }
.empty-cell { padding: 30px; color: var(--muted); text-align: center; }
.pagination { display: flex; align-items: center; justify-content: flex-end; gap: 12px; border-top: 1px solid var(--line); padding-top: 16px; }
.pagination-button { border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); padding: 8px 12px; font-weight: 750; cursor: pointer; }
.pagination-button:hover:not(:disabled) { border-color: var(--green); color: var(--green); }
.pagination-button:disabled { cursor: not-allowed; opacity: 0.42; }
.pagination-info { min-width: 104px; color: var(--muted); font-size: 0.82rem; text-align: center; }

.error-panel { display: flex; margin-top: 14px; border: 1px solid #f2b8b5; border-radius: 12px; background: var(--red-soft); padding: 15px 17px; color: var(--red); }
.error-panel p { margin: 5px 0 0; font-size: 0.86rem; }
footer { display: flex; justify-content: space-between; gap: 20px; padding: 18px 2px 0; color: var(--muted); font-size: 0.72rem; }

.annotation-dialog { width: min(560px, calc(100% - 28px)); max-height: calc(100dvh - 32px); overflow: auto; border: 0; border-radius: 16px; background: var(--surface); color: var(--text); padding: 0; box-shadow: 0 24px 80px rgba(15, 35, 31, 0.3); }
.annotation-dialog::backdrop { background: rgba(15, 35, 31, 0.52); backdrop-filter: blur(3px); }
.annotation-dialog-form { display: grid; gap: 20px; padding: 24px; }
.annotation-dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; }
.annotation-dialog-heading .muted { margin: 7px 0 0; }
.dialog-close-button { display: grid; flex: 0 0 auto; place-items: center; width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-soft); color: var(--muted); font-size: 1.35rem; line-height: 1; cursor: pointer; }
.dialog-close-button:hover { border-color: var(--green); color: var(--green); }
.annotation-dialog-field { display: grid; gap: 8px; }
.annotation-dialog-field > span { color: var(--text); font-size: 0.78rem; font-weight: 800; }
.annotation-dialog-field textarea { width: 100%; resize: vertical; border: 1px solid var(--line); border-radius: 9px; background: var(--surface); color: var(--text); padding: 10px 12px; line-height: 1.4; }
.annotation-dialog-field textarea:focus { border-color: var(--green); outline: 3px solid rgba(15, 118, 110, 0.12); }
.annotation-dialog-photo { display: grid; min-height: 170px; overflow: hidden; place-items: center; border: 1px dashed #bfd0cc; border-radius: 10px; background: var(--surface-soft); }
.annotation-dialog-preview { display: block; width: 100%; max-height: 300px; object-fit: contain; }
.annotation-dialog-no-photo { color: var(--muted); font-size: 0.8rem; }
.annotation-dialog-photo-actions { display: flex; flex-wrap: wrap; gap: 7px; }
.annotation-compression-hint { color: var(--muted); font-size: 0.7rem; }
.annotation-dialog-status { min-height: 1.2em; margin: -8px 0 0; color: var(--muted); font-size: 0.76rem; }
.annotation-dialog-footer { display: flex; justify-content: flex-end; gap: 9px; padding-top: 2px; }

@media (max-width: 900px) {
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .current-card { grid-column: span 2; }
  .day-stats-bar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .chart-stat:nth-child(2) { border-right: 0; }
  .chart-stat:nth-child(-n + 2) { border-bottom: 1px solid var(--line); }
}

@media (max-width: 600px) {
  .shell { width: min(100% - 20px, 1180px); padding-top: 20px; }
  .topbar, .section-heading, .chart-header, footer { align-items: flex-start; flex-direction: column; }
  .header-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); width: 100%; }
  .header-actions .button { width: 100%; }
  .summary-grid { grid-template-columns: 1fr; }
  .current-card { grid-column: auto; }
  .metric-card { min-height: 145px; }
  .chart-card { padding: 16px; }
  .chart-day-controls { width: 100%; flex-wrap: nowrap; }
  .select-wrapper { flex: 1; min-width: 0; max-width: none; }
  .day-select { width: 100%; }
  .chart-controls-toolbar { align-items: flex-start; }
  .zoom-group { max-width: 100%; overflow-x: auto; padding-bottom: 2px; }
  .chart-scroll-container { height: 310px; }
  .chart-legend { align-items: flex-start; flex-direction: column; gap: 7px; }
  .chart-range-text { margin-left: 0; }
  .pagination { justify-content: space-between; }
}

/* Login */
.login-page {
  min-height: 100dvh;
  background:
    radial-gradient(circle at 10% 5%, rgba(15, 118, 110, 0.08), transparent 30rem),
    var(--bg);
}

.login-page::after {
  position: fixed;
  inset: 0;
  background-image: linear-gradient(rgba(15, 118, 110, 0.035) 1px, transparent 1px), linear-gradient(90deg, rgba(15, 118, 110, 0.035) 1px, transparent 1px);
  background-size: 32px 32px;
  mask-image: linear-gradient(to right, #000, transparent 72%);
  pointer-events: none;
  content: "";
}

.login-shell {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(340px, 430px);
  gap: clamp(48px, 9vw, 120px);
  align-items: center;
  width: min(1040px, calc(100% - 48px));
  min-height: 100dvh;
  margin: 0 auto;
  padding: 48px 0;
}

.login-intro {
  padding: 12px 0;
}

.education-notice {
  max-width: 540px;
}

.notice-label {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 14px;
  color: var(--green);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.notice-label::before { width: 28px; height: 2px; border-radius: 2px; background: currentColor; content: ""; }

.education-notice h1 {
  max-width: 480px;
  font-size: clamp(1.8rem, 3.5vw, 2.5rem);
  line-height: 1.08;
  letter-spacing: -0.045em;
}

.education-notice > p:last-child {
  max-width: 490px;
  margin: 18px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.7;
}

.login-panel {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  padding: clamp(28px, 5vw, 38px);
  box-shadow: var(--shadow);
}

.login-panel-heading { margin-bottom: 26px; }
.login-panel-heading h2 { font-size: 1.5rem; letter-spacing: -0.035em; }
.login-panel-heading p { margin: 7px 0 0; color: var(--muted); font-size: 0.86rem; }

.login-field { margin-bottom: 17px; }
.login-field label { display: block; margin-bottom: 7px; color: var(--text); font-size: 0.78rem; font-weight: 750; }

.login-field input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-soft);
  padding: 12px 13px;
  color: var(--text);
  font-size: 0.92rem;
  outline: none;
  transition: border-color 150ms ease, box-shadow 150ms ease, background 150ms ease;
}

.login-field input::placeholder { color: #9aa7a3; }
.login-field input:focus { border-color: var(--green); background: var(--surface); box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.1); }

.login-button {
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 44px;
  margin-top: 8px;
  border: 1px solid var(--green);
  border-radius: 9px;
  background: var(--green);
  padding: 11px 14px;
  color: #fff;
  font-size: 0.9rem;
  font-weight: 750;
  cursor: pointer;
  transition: background 150ms ease, transform 150ms ease, opacity 150ms ease;
}

.login-button:hover:not(:disabled) { background: #0c6962; transform: translateY(-1px); }
.login-button:active:not(:disabled) { transform: translateY(0); }
.login-button:disabled { cursor: wait; opacity: 0.68; }
.login-button .btn-spinner { display: none; width: 17px; height: 17px; border: 2px solid rgba(255,255,255,0.35); border-top-color: #fff; border-radius: 50%; animation: login-spin 700ms linear infinite; }
.login-button.loading .btn-text { display: none; }
.login-button.loading .btn-spinner { display: block; }

@keyframes login-spin { to { transform: rotate(360deg); } }

.login-error {
  display: none;
  align-items: center;
  gap: 9px;
  margin: -7px 0 18px;
  border: 1px solid #f2b8b5;
  border-radius: 9px;
  background: var(--red-soft);
  padding: 10px 12px;
  color: var(--red);
  font-size: 0.8rem;
  font-weight: 650;
}

.login-error.visible { display: flex; }
.error-mark { display: grid; flex: 0 0 auto; place-items: center; width: 18px; height: 18px; border: 1px solid currentColor; border-radius: 50%; font-size: 0.68rem; font-weight: 800; }

.login-disclaimer {
  margin: 24px 0 0;
  border-top: 1px solid var(--line);
  padding-top: 18px;
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.55;
  text-align: center;
}

@media (max-width: 760px) {
  .login-shell { grid-template-columns: 1fr; gap: 32px; width: min(100% - 28px, 460px); padding: 36px 0; }
  .login-intro { padding: 0; }
  .education-notice h1 { font-size: clamp(1.55rem, 8vw, 2rem); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}
