:root {
  --page: #f4fbff;
  --page-warm: #fff9ed;
  --paper: #ffffff;
  --paper-soft: #eff8ff;
  --sky: #8fd3ff;
  --sky-soft: #dff1ff;
  --blue: #2563eb;
  --ink: #17324d;
  --ink-soft: #5f748a;
  --line: #17324d;
  --green: #dff8d8;
  --green-line: #67aa61;
  --green-ink: #27632d;
  --red: #ffe1e1;
  --red-line: #dc2626;
  --red-ink: #991b1b;
  --orange: #ffe6c7;
  --orange-line: #dc7a22;
  --orange-ink: #9a3e0c;
  --yellow: #fff4bf;
  --yellow-line: #d8a600;
  --yellow-ink: #7a5600;
  --radius-xs: 9px;
  --radius-sm: 12px;
  --radius-md: 18px;
  --radius-lg: 24px;
  --shadow: 0 5px 0 var(--line), 0 16px 30px rgba(23, 50, 77, 0.12);
  --shadow-sm: 0 3px 0 var(--line), 0 8px 16px rgba(23, 50, 77, 0.1);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "Nunito", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  background:
    radial-gradient(circle at 0 0, rgba(143, 211, 255, 0.24), transparent 32rem),
    linear-gradient(180deg, #f4fbff 0%, #fff9ed 100%);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app {
  min-height: 100vh;
}

.screen {
  min-height: 100vh;
  padding: 24px;
}

.screen-centered {
  display: grid;
  place-items: center;
}

.shell {
  width: min(100%, 1220px);
  margin: 0 auto;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.brand-kicker {
  margin: 0 0 5px;
  color: var(--blue);
  font-size: 0.9rem;
  font-weight: 900;
}

.panel {
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 22px;
}

.panel-soft {
  background: var(--paper-soft);
}

.hero-panel {
  width: min(100%, 620px);
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 8px;
  font-size: clamp(2.25rem, 6vw, 4.4rem);
  line-height: 1;
  letter-spacing: -0.02em;
}

h2 {
  margin-bottom: 8px;
  font-size: clamp(1.8rem, 4vw, 2.5rem);
  line-height: 1.05;
}

h3 {
  margin-bottom: 6px;
  font-size: 1.2rem;
}

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

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  border: 2px solid var(--line);
  border-radius: 999px;
  padding: 9px 15px;
  background: var(--sky);
  color: var(--ink);
  font-weight: 900;
  box-shadow: var(--shadow-sm);
  text-decoration: none;
  transition:
    transform 140ms ease,
    box-shadow 140ms ease;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--line);
}

.btn-secondary {
  background: var(--paper);
}

.login-form {
  display: grid;
  gap: 14px;
  margin-top: 18px;
}

.login-credit {
  margin: 18px 0 0;
  color: var(--ink-soft);
  font-size: 0.82rem;
  font-weight: 800;
  text-align: center;
}

.login-credit a {
  color: var(--ink-soft);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.link-button {
  width: fit-content;
  border: 0;
  background: transparent;
  color: var(--blue);
  cursor: pointer;
  font: inherit;
  font-size: 0.92rem;
  font-weight: 900;
  text-align: left;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.field {
  width: 100%;
  min-height: 52px;
  border: 2px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  background: var(--paper);
  color: var(--ink);
  font-weight: 800;
  outline: none;
}

.field:focus {
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.16);
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: fit-content;
  border: 2px solid var(--line);
  border-radius: 999px;
  padding: 5px 10px;
  background: var(--paper);
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 900;
  white-space: nowrap;
}

.chip-target {
  border-color: var(--green-line);
  background: var(--green);
  color: var(--green-ink);
}

.chip-low {
  border-color: var(--red-line);
  background: var(--red);
  color: var(--red-ink);
}

.chip-high {
  border-color: var(--orange-line);
  background: var(--orange);
  color: var(--orange-ink);
}

.chip-attention {
  border-color: var(--yellow-line);
  background: var(--yellow);
  color: var(--yellow-ink);
}

.grid-card {
  overflow: auto;
  border: 2px solid #bddff8;
  border-radius: var(--radius-md);
  background: var(--paper);
  padding: 10px;
  overscroll-behavior-x: contain;
  scrollbar-color: var(--sky) var(--paper-soft);
}

.glucose-grid {
  display: grid;
  grid-template-columns: 74px repeat(8, minmax(104px, 1fr));
  gap: 7px;
  min-width: 980px;
}

.grid-corner {
  position: sticky;
  left: 0;
  z-index: 4;
  min-height: 54px;
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--paper) 82%, rgba(255, 255, 255, 0));
}

.grid-header {
  display: grid;
  place-items: center;
  min-height: 54px;
  border: 2px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--sky-soft);
  text-align: center;
  font-size: 0.78rem;
  font-weight: 900;
  line-height: 1.05;
}

.grid-header span {
  color: var(--ink-soft);
  font-weight: 800;
}

.day-label {
  position: sticky;
  left: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  min-height: 48px;
  border: 2px solid #bddff8;
  border-radius: var(--radius-sm);
  background: var(--paper-soft);
  padding: 0 8px;
  font-size: 0.9rem;
  font-weight: 900;
  box-shadow: 8px 0 14px rgba(255, 255, 255, 0.86);
}

.glucose-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  border: 2px dashed var(--sky);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--blue);
  font-size: 1.15rem;
  font-weight: 900;
  line-height: 1;
  padding: 4px;
}

button.glucose-cell {
  width: 100%;
}

.glucose-cell input {
  width: 100%;
  height: 100%;
  border: 0;
  background: transparent;
  color: var(--ink);
  text-align: center;
  font-size: 1.15rem;
  font-weight: 900;
  outline: none;
}

.cell-editor {
  display: grid;
  width: 100%;
  gap: 4px;
  padding: 4px;
}

.cell-editing {
  min-height: 124px;
}

.glucose-cell .meal-note-input {
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--ink);
  font-size: 0.76rem;
  font-weight: 800;
  padding: 0 6px;
}

.meal-note-badge {
  display: inline-flex;
  max-width: 100%;
  border: 1px solid #b7d4e8;
  border-radius: 999px;
  background: #eef8ff;
  color: var(--blue);
  padding: 2px 5px;
  font-size: 0.62rem;
  font-weight: 900;
  line-height: 1;
}

.save-cell-btn,
.remove-reading-btn {
  border: 1px solid var(--sky);
  border-radius: 999px;
  background: var(--blue);
  color: white;
  padding: 2px 6px;
  font-size: 0.7rem;
  font-weight: 900;
}

.remove-reading-btn {
  border-color: var(--red-line);
  background: var(--red);
  color: var(--red-ink);
}

.cell-target {
  border-style: solid;
  border-color: var(--green-line);
  background: var(--green);
  color: var(--green-ink);
}

.cell-low {
  border: 3px solid var(--red-line);
  background: var(--red);
  color: var(--red-ink);
}

.cell-high {
  border-style: solid;
  border-color: var(--orange-line);
  background: var(--orange);
  color: var(--orange-ink);
}

.cell-attention {
  border-style: solid;
  border-color: var(--yellow-line);
  background: var(--yellow);
  color: var(--yellow-ink);
}

.cell-editing {
  border: 3px solid var(--blue);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.16);
}

.alert {
  margin-top: 14px;
  border: 2px solid var(--red-line);
  border-radius: var(--radius-md);
  background: var(--red);
  color: var(--red-ink);
  padding: 13px;
}

.alert-prominent {
  margin: 0 0 12px;
}

.notice {
  margin: 0;
  border: 2px solid var(--green-line);
  border-radius: var(--radius-md);
  background: var(--green);
  color: var(--green-ink);
  padding: 12px 14px;
  font-weight: 900;
}

.mobile-scroll-hint {
  display: none;
  margin: -2px 0 10px;
  color: var(--ink-soft);
  font-size: 0.9rem;
  font-weight: 800;
}

.patient-feedback {
  min-height: 24px;
  margin: -4px 0 10px;
  color: var(--green-ink);
  font-size: 0.95rem;
  font-weight: 900;
}

.patient-feedback-error {
  color: var(--red-ink);
}

.doctor-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.new-patient-form {
  display: grid;
  gap: 12px;
  border: 2px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--page-warm);
  padding: 16px;
}

.new-patient-form h2 {
  margin-bottom: 4px;
  font-size: 1.45rem;
}

.new-patient-form p {
  margin-bottom: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  gap: 12px;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.patient-table {
  display: grid;
  gap: 8px;
}

.patient-table-header,
.patient-row {
  display: grid;
  grid-template-columns: 1.4fr 0.8fr 0.9fr 0.9fr 0.7fr;
  gap: 10px;
  align-items: center;
}

.patient-table-header {
  padding: 0 12px;
  color: var(--ink-soft);
  font-size: 0.8rem;
  font-weight: 900;
}

.patient-row {
  border: 2px solid #bddff8;
  border-radius: var(--radius-md);
  background: var(--paper);
  padding: 12px;
  text-align: left;
}

.patient-row button {
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.patient-row strong {
  display: block;
}

.progress-track {
  height: 8px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #dbeafe;
}

.progress-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--sky);
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.summary-card {
  border: 2px solid #bddff8;
  border-radius: var(--radius-md);
  background: var(--paper);
  padding: 14px;
}

.summary-card strong {
  display: block;
  font-size: 1.6rem;
}

.stats-section {
  margin-bottom: 16px;
}

.stats-section h2 {
  margin-bottom: 10px;
  font-size: 1.35rem;
  line-height: 1.1;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 10px;
}

.stat-card {
  display: flex;
  min-height: 112px;
  flex-direction: column;
  justify-content: space-between;
  border: 2px solid #bddff8;
  border-radius: var(--radius-md);
  background: linear-gradient(180deg, var(--paper) 0%, var(--paper-soft) 100%);
  padding: 14px;
}

.stat-card strong {
  display: block;
  margin: 8px 0 4px;
  font-size: 1.65rem;
  line-height: 1;
}

.stat-card small {
  color: var(--ink-soft);
  font-size: 0.82rem;
  font-weight: 800;
  line-height: 1.2;
}

.stat-card-low {
  border-color: var(--red-line);
  background: var(--red);
}

.stat-card-low strong,
.stat-card-low small {
  color: var(--red-ink);
}

.stat-card-high {
  border-color: var(--orange-line);
  background: var(--orange);
}

.stat-card-high strong,
.stat-card-high small {
  color: var(--orange-ink);
}

.target-panel {
  display: grid;
  gap: 12px;
  margin-bottom: 16px;
  border: 2px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--paper);
  padding: 16px;
}

.patient-profile-panel {
  display: grid;
  gap: 12px;
  margin-bottom: 16px;
  border: 2px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--paper);
  padding: 16px;
}

.patient-profile-panel h2 {
  margin-bottom: 4px;
  font-size: 1.35rem;
}

.patient-profile-panel p {
  margin-bottom: 0;
}

.patient-profile-form {
  display: grid;
  gap: 12px;
}

.target-panel h2 {
  margin-bottom: 4px;
  font-size: 1.35rem;
}

.target-panel p {
  margin-bottom: 0;
}

.target-form {
  display: grid;
  gap: 12px;
}

.target-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
}

.target-grid .field {
  min-height: 46px;
}

.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-top: 16px;
}

.chart-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.period-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 12px;
}

.segment {
  min-height: 38px;
  border: 2px solid var(--line);
  border-radius: 999px;
  padding: 7px 12px;
  background: var(--paper);
  color: var(--ink);
  font-weight: 900;
}

.segment-active {
  background: var(--sky);
  box-shadow: 0 2px 0 var(--line);
}

.chart {
  border: 2px solid #bddff8;
  border-radius: var(--radius-md);
  background: var(--paper);
  padding: 14px;
}

.chart svg {
  display: block;
  width: 100%;
  min-height: 170px;
}

.bar-row {
  display: grid;
  grid-template-columns: 110px 1fr 42px;
  gap: 8px;
  align-items: center;
  margin-top: 8px;
}

.bar-track {
  height: 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--paper-soft);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--sky);
}

.moment-averages {
  margin-bottom: 14px;
}

.moment-averages-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.moment-averages-header h3 {
  margin-bottom: 0;
}

.moment-average-scroll {
  overflow: auto;
  border: 2px solid #bddff8;
  border-radius: var(--radius-md);
  background: var(--paper);
  padding: 10px;
  overscroll-behavior-x: contain;
  scrollbar-color: var(--sky) var(--paper-soft);
}

.moment-average-grid {
  display: grid;
  grid-template-columns: 74px repeat(8, minmax(104px, 1fr));
  gap: 7px;
  min-width: 980px;
}

.moment-average-corner,
.moment-average-card {
  min-height: 72px;
  border: 2px solid var(--line);
  border-radius: var(--radius-sm);
}

.moment-average-corner {
  position: sticky;
  left: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  background: var(--sky-soft);
  font-weight: 900;
  box-shadow: 8px 0 14px rgba(255, 255, 255, 0.86);
}

.moment-average-card {
  display: grid;
  gap: 3px;
  align-content: center;
  background: var(--paper-soft);
  padding: 8px;
  text-align: center;
}

.moment-average-card span {
  font-size: 0.78rem;
  font-weight: 900;
  line-height: 1.05;
}

.moment-average-card strong {
  font-size: 1.05rem;
  line-height: 1;
}

.moment-average-card small {
  color: var(--ink-soft);
  font-size: 0.74rem;
  font-weight: 800;
}

.moment-summary {
  margin-top: 16px;
}

.moment-summary h3 {
  margin-bottom: 10px;
}

.moment-summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.moment-summary-card {
  display: grid;
  gap: 6px;
  border: 2px solid #bddff8;
  border-radius: var(--radius-md);
  background: var(--paper-soft);
  padding: 12px;
}

.moment-summary-card strong {
  line-height: 1.1;
}

@media (max-width: 1020px) {
  .shell {
    width: min(100%, 920px);
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .target-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .chart-grid {
    grid-template-columns: 1fr;
  }

  .moment-summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .patient-table-header,
  .patient-row {
    grid-template-columns: 1.2fr 0.7fr 0.8fr 0.8fr 0.6fr;
  }
}

@media (max-width: 760px) {
  .screen {
    padding: 14px;
  }

  h1 {
    font-size: clamp(2rem, 11vw, 3rem);
  }

  .topbar {
    flex-direction: column;
  }

  .panel {
    border-radius: var(--radius-md);
    padding: 14px;
  }

  .button-row {
    width: 100%;
  }

  .topbar .btn {
    flex: 1;
  }

  .chip-row {
    gap: 6px;
  }

  .chip {
    font-size: 0.78rem;
    padding: 5px 8px;
  }

  .mobile-scroll-hint {
    display: block;
  }

  .grid-card {
    margin: 0 -4px;
    padding: 8px;
  }

  .moment-average-scroll {
    margin: 0 -4px;
    padding: 8px;
  }

  .glucose-grid {
    grid-template-columns: 76px repeat(8, minmax(96px, 1fr));
    gap: 6px;
    min-width: 862px;
  }

  .moment-average-grid {
    grid-template-columns: 76px repeat(8, minmax(96px, 1fr));
    gap: 6px;
    min-width: 862px;
  }

  .grid-header {
    min-height: 58px;
  }

  .day-label,
  .glucose-cell {
    min-height: 56px;
  }

  .glucose-cell {
    font-size: 1.2rem;
  }

  .patient-table-header {
    display: none;
  }

  .patient-row {
    grid-template-columns: 1fr;
  }

  .form-grid,
  .summary-grid,
  .stats-grid,
  .target-grid,
  .moment-summary-grid,
  .chart-grid {
    grid-template-columns: 1fr;
  }
}
