:root {
  color-scheme: dark;
  --bg: #03101f;
  --panel: #092442;
  --panel-soft: #0d3157;
  --line: #214b73;
  --text: #f4f8ff;
  --muted: #9bb2ca;
  --blue: #1a9cff;
  --cyan: #4edee5;
  --mint: #4ee1c1;
  --danger: #ff7185;
  --shadow: 0 14px 32px rgba(0, 0, 0, .24);
}

* { box-sizing: border-box; }
html { background: var(--bg); }
body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at 90% -10%, #124b78 0, transparent 34%), var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
a { color: var(--cyan); }

main {
  width: min(720px, 100%);
  margin: 0 auto;
  padding: 18px 16px 112px;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 22px;
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 16px;
  background: linear-gradient(145deg, var(--blue), #0757b5);
  box-shadow: var(--shadow);
  font-size: 23px;
}
.app-header h1 { margin: 0; font-size: 23px; }
.app-header p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.free-badge {
  margin-left: auto;
  border: 1px solid #28896f;
  border-radius: 999px;
  padding: 5px 9px;
  color: var(--mint);
  background: rgba(28, 132, 103, .18);
  font-size: 11px;
  font-weight: 800;
}

.screen { display: none; animation: reveal .2s ease; }
.screen.active { display: block; }
@keyframes reveal { from { opacity: 0; transform: translateY(4px); } }

h2 { margin: 5px 0 16px; font-size: 27px; line-height: 1.15; }
h3 { margin: 20px 0 10px; }
p { margin: 8px 0; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

.hero {
  border: 1px solid #197dd0;
  border-radius: 24px;
  padding: 22px;
  background: linear-gradient(145deg, #0a3f6d, #092a4a);
  box-shadow: var(--shadow);
}
.hero span, .section-heading span {
  color: var(--cyan);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.hero h2 { margin-bottom: 8px; }
.hero p { color: #c9d8e8; }

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}
.summary-card {
  min-height: 94px;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px;
  background: rgba(9, 36, 66, .92);
}
.summary-card span { display: block; min-height: 35px; color: var(--muted); font-size: 12px; }
.summary-card b { display: block; font-size: 20px; color: var(--text); }

.quick-actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 9px;
}
.quick-actions button {
  min-height: 92px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--panel);
  color: var(--text);
  font-size: 24px;
}
.quick-actions b { display: block; margin-top: 7px; font-size: 13px; }

button {
  border: 0;
  border-radius: 14px;
  padding: 13px 16px;
  background: linear-gradient(145deg, #1598ff, #0876d8);
  color: white;
  font-weight: 800;
}
button:active { transform: translateY(1px); }
button.secondary { border: 1px solid var(--line); background: var(--panel-soft); }
button.danger { background: #8e2740; }
button.small { padding: 9px 12px; font-size: 12px; }
button.wide { width: 100%; margin-top: 12px; }

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.section-heading h2 { margin-bottom: 10px; }

.card, .lesson, .notice {
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 15px;
  background: rgba(9, 36, 66, .94);
  box-shadow: 0 8px 18px rgba(0, 0, 0, .12);
}
.notice {
  border-color: #2b638e;
  background: #0c3152;
  color: #dceeff;
  font-size: 13px;
}
.lesson { margin-bottom: 10px; }
.lesson b { color: var(--cyan); font-size: 17px; }
.lesson p { color: #c9d8e8; }
.legal .card { margin-bottom: 12px; }

.library-hero {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 16px;
  overflow: hidden;
  border: 1px solid #167fc7;
  border-radius: 24px;
  padding: 21px;
  background:
    radial-gradient(circle at 92% 5%, rgba(78, 222, 229, .23), transparent 36%),
    linear-gradient(145deg, #0a416f, #092544 70%);
  box-shadow: var(--shadow);
}
.library-hero::after {
  content: '';
  position: absolute;
  right: -46px;
  bottom: -65px;
  width: 160px;
  height: 160px;
  border: 1px solid rgba(78, 222, 229, .23);
  border-radius: 50%;
}
.library-hero h3 { margin: 7px 0; max-width: 430px; font-size: 24px; line-height: 1.15; }
.library-hero p { max-width: 520px; color: #c9d8e8; }
.eyebrow { color: var(--cyan); font-size: 11px; font-weight: 900; letter-spacing: .1em; text-transform: uppercase; }
.library-symbol {
  z-index: 1;
  display: grid;
  place-items: center;
  align-self: center;
  width: 76px;
  height: 76px;
  border: 1px solid rgba(255, 255, 255, .25);
  border-radius: 24px;
  background: rgba(3, 16, 31, .4);
  font-size: 36px;
  transform: rotate(3deg);
}
.library-stats { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 13px; }
.library-stats b {
  border: 1px solid rgba(78, 222, 229, .25);
  border-radius: 999px;
  padding: 5px 9px;
  background: rgba(4, 25, 45, .45);
  color: #dffcff;
  font-size: 11px;
}

.urgent-guide {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  margin: 12px 0 16px;
  border: 1px solid rgba(255, 113, 133, .62);
  border-radius: 20px;
  padding: 14px;
  background: linear-gradient(135deg, rgba(120, 30, 52, .72), rgba(49, 30, 48, .9));
  box-shadow: 0 10px 24px rgba(0, 0, 0, .16);
}
.urgent-icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 15px;
  background: var(--danger);
  color: #270613;
  font-size: 25px;
  font-weight: 1000;
}
.urgent-guide span { display: block; color: #ffb8c3; font-size: 11px; font-weight: 900; letter-spacing: .06em; text-transform: uppercase; }
.urgent-guide b { display: block; margin-top: 2px; color: #fff; }
.urgent-guide p { margin: 4px 0 0; color: #efcbd2; font-size: 12px; }
.urgent-guide button { padding: 10px 12px; background: #f7e5e8; color: #68243a; font-size: 12px; }

.handbook-tools { margin-bottom: 12px; }
.handbook-search {
  position: relative;
  display: block;
  margin: 0 0 9px;
}
.handbook-search span {
  position: absolute;
  z-index: 1;
  left: 14px;
  top: 50%;
  color: var(--cyan);
  font-size: 24px;
  font-weight: 400;
  transform: translateY(-54%);
}
.handbook-search input { padding-left: 44px; border-radius: 16px; background: #061b32; }
.handbook-filters { display: flex; gap: 7px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: none; }
.handbook-filters::-webkit-scrollbar { display: none; }
.handbook-filters .chip { flex: 0 0 auto; }

.handbook-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 11px; }
.handbook-card {
  display: flex;
  min-width: 0;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: rgba(9, 36, 66, .96);
  box-shadow: 0 10px 24px rgba(0, 0, 0, .16);
}
.handbook-card.featured { border-color: rgba(255, 113, 133, .62); }
.handbook-cover {
  position: relative;
  overflow: hidden;
  aspect-ratio: 16 / 9;
  border-bottom: 1px solid var(--line);
  background: #eaf7ff;
}
.handbook-cover::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, transparent 45%, rgba(3, 16, 31, .34));
  pointer-events: none;
}
.handbook-cover img { display: block; width: 100%; height: 100%; object-fit: cover; object-position: top center; }
.handbook-icon {
  position: absolute;
  z-index: 1;
  right: 10px;
  bottom: 9px;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid rgba(255, 255, 255, .42);
  border-radius: 13px;
  background: rgba(4, 24, 43, .86);
  box-shadow: 0 7px 15px rgba(0, 0, 0, .25);
  font-size: 19px;
}
.handbook-body { display: flex; flex: 1; flex-direction: column; padding: 13px; }
.handbook-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.handbook-meta span {
  border-radius: 999px;
  padding: 4px 7px;
  background: rgba(78, 222, 229, .13);
  color: var(--cyan);
  font-size: 10px;
  font-weight: 900;
  text-transform: uppercase;
}
.featured .handbook-meta span { background: rgba(255, 113, 133, .14); color: #ff9dac; }
.handbook-meta small { color: var(--muted); font-size: 10px; white-space: nowrap; }
.handbook-body h3 { margin: 10px 0 5px; font-size: 17px; line-height: 1.2; }
.handbook-body p { flex: 1; margin: 0 0 12px; color: #b9ccde; font-size: 12px; }
.handbook-actions { display: grid; grid-template-columns: minmax(0, 1fr) 42px; gap: 7px; }
.handbook-actions button { padding: 10px; font-size: 12px; }
.handbook-actions a {
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-soft);
  color: var(--text);
  font-size: 20px;
  font-weight: 900;
  text-decoration: none;
}
.handbook-empty { grid-column: 1 / -1; }
.library-notice { margin-top: 12px; }
.school-summary { margin-top: 12px; }
.school-summary summary {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px 15px;
  background: var(--panel-soft);
  color: var(--cyan);
  cursor: pointer;
  font-weight: 800;
}
.school-summary[open] summary { margin-bottom: 10px; }

label {
  display: grid;
  gap: 7px;
  margin: 13px 0;
  color: #cbd9e9;
  font-size: 13px;
  font-weight: 700;
}
input, select, textarea {
  width: 100%;
  border: 1px solid #2b5e89;
  border-radius: 14px;
  padding: 12px 13px;
  outline: none;
  background: #041a31;
  color: var(--text);
}
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(26, 156, 255, .14); }
.form-card > button { width: 100%; margin-top: 5px; }
.two-columns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.form-card hr { border: 0; border-top: 1px solid var(--line); margin: 22px 0 10px; }
.warning-notice { border-color: #8b702f; background: #392f17; color: #fff1bf; }
.check-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 11px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #061b32;
}
.check-row input { width: 20px; height: 20px; margin: 0; flex: 0 0 auto; accent-color: var(--blue); }
.medical-settings { margin: 8px 0 16px; padding: 2px 12px 10px; border-left: 2px solid var(--blue); }
.medical-settings small { display: block; margin: -8px 0 11px; color: var(--muted); }
.bolus-card { margin-top: 12px; border-color: #216da3; }
.calculator-hero {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 14px;
  border: 1px solid #167fc7;
  border-radius: 22px;
  padding: 17px;
  background: linear-gradient(145deg, #0a416f, #092544);
  box-shadow: var(--shadow);
}
.calculator-hero b { color: #fff; font-size: 20px; }
.calculator-hero p { color: #c9d8e8; }
.calculator-symbol { display: grid; place-items: center; width: 58px; height: 58px; border-radius: 18px; background: #061b32; font-size: 29px; }
.bolus-setup { margin-top: 12px; }
.bolus-setup button { width: 100%; margin-top: 8px; }
.calculation-modes { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.calculation-modes div { border: 1px solid var(--line); border-radius: 14px; padding: 11px; background: #061b32; }
.calculation-modes span, .calculation-modes b { display: block; }
.calculation-modes span { color: var(--cyan); font-size: 12px; font-weight: 900; }
.calculation-modes b { margin-top: 5px; color: #dceeff; font-size: 12px; }
.field-help { display: block; margin-top: -8px; color: var(--muted); }
.medical-parameters {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
  margin: 12px 0;
}
.medical-parameters span { padding: 9px; border-radius: 12px; background: #061b32; color: var(--muted); font-size: 11px; }
.medical-parameters b { display: block; margin-top: 3px; color: var(--text); overflow-wrap: anywhere; }
.bolus-result { margin-top: 12px; padding: 15px; border: 1px solid #2a8e7b; border-radius: 16px; background: #072f35; }
.bolus-result > span { color: var(--mint); font-size: 12px; font-weight: 800; text-transform: uppercase; }
.bolus-result > b { display: block; margin: 4px 0 12px; color: white; font-size: 30px; }
.formula-breakdown { display: grid; gap: 6px; margin-bottom: 12px; color: #d9ecf7; font-size: 13px; }
.formula-breakdown div { padding: 8px 10px; border-radius: 10px; background: rgba(2, 20, 37, .65); }
.bolus-result button { width: 100%; margin-top: 10px; }
.dose-result-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; margin-bottom: 12px; }
.dose-result-grid div { border-radius: 12px; padding: 10px 7px; background: rgba(2, 20, 37, .65); text-align: center; }
.dose-result-grid span { display: block; color: var(--muted); font-size: 10px; }
.dose-result-grid b { display: block; margin-top: 3px; color: white; font-size: 15px; }
.high-glucose-note { margin-top: 12px; border-color: #9a5e39; background: #3a251d; color: #ffe6d3; }

.entry-tabs, .filter-row {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  padding: 3px 0 10px;
}
.entry-tabs button, .chip {
  flex: 1 0 auto;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  padding: 10px 12px;
}
.entry-tabs button.active, .chip.active { border-color: var(--blue); background: #0e4d7b; color: white; }
.entry-panel { display: none; }
.entry-panel.active { display: block; }

.calculation-result {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
  margin: 14px 0 8px;
}
.calculation-result div {
  border-radius: 15px;
  padding: 13px;
  background: #061b32;
}
.calculation-result span { display: block; color: var(--muted); font-size: 11px; }
.calculation-result b { display: block; margin-top: 4px; color: var(--mint); font-size: 20px; }

.toolbar { margin-bottom: 10px; }
.compact { margin: 0 0 12px; }
.compact select { padding: 9px 11px; }
#diarySummary { display: flex; flex-wrap: wrap; gap: 7px 16px; margin-bottom: 10px; }
#diarySummary b { width: 100%; color: var(--cyan); }
#diarySummary span { font-size: 12px; }
.chart-card { margin-bottom: 10px; }
.chart-title { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.chart-title span { color: var(--muted); font-size: 12px; }
#glucoseChart { display: block; width: 100%; height: 180px; }
.diary-entry {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 9px;
}
.diary-entry p { overflow-wrap: anywhere; }
.entry-actions { display: flex; gap: 5px; }
.entry-actions button {
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--line);
  background: var(--panel-soft);
  color: var(--muted);
}
.entry-actions button:last-child { color: var(--danger); }
.empty { text-align: center; color: var(--muted); }

.button-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.text-links { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 12px; }
.text-links button { padding: 2px; background: none; color: var(--cyan); font-weight: 600; }
.data-actions { margin-top: 12px; }

nav {
  position: fixed;
  z-index: 20;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  width: min(720px, 100%);
  padding: 9px 8px calc(9px + env(safe-area-inset-bottom));
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 24px 24px 0 0;
  background: rgba(5, 25, 46, .97);
  box-shadow: 0 -12px 30px rgba(0, 0, 0, .28);
}
nav button {
  display: grid;
  place-items: center;
  gap: 2px;
  min-width: 0;
  padding: 6px 3px;
  background: transparent;
  color: var(--muted);
  font-size: 19px;
}
nav button span { font-size: 10px; font-weight: 700; }
nav button.active { color: var(--cyan); }
nav .plus {
  width: 54px;
  height: 54px;
  margin: -18px auto 0;
  border-radius: 50%;
  background: linear-gradient(145deg, #1ba9ff, #0874df);
  color: white;
  font-size: 31px;
  box-shadow: 0 8px 22px rgba(26, 156, 255, .38);
}

#toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: 92px;
  width: min(360px, calc(100% - 32px));
  padding: 12px 16px;
  border: 1px solid #2d6b9e;
  border-radius: 15px;
  background: #eaf7ff;
  color: #08223c;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 10px);
  transition: .2s ease;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.modal {
  position: fixed;
  z-index: 100;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 7, 15, .82);
  backdrop-filter: blur(5px);
}
.modal-card {
  width: min(520px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  border: 1px solid #3978a8;
  border-radius: 22px;
  padding: 20px;
  background: #092442;
  box-shadow: var(--shadow);
}
.modal-card h2 { font-size: 23px; }
.modal-card ul { padding-left: 21px; color: #c9d8e8; }

@media (max-width: 560px) {
  .library-hero { grid-template-columns: 1fr; }
  .library-symbol { display: none; }
  .urgent-guide { grid-template-columns: auto minmax(0, 1fr); }
  .urgent-guide button { grid-column: 1 / -1; width: 100%; }
  .handbook-grid { grid-template-columns: 1fr; }
}

@media (max-width: 390px) {
  main { padding-inline: 12px; }
  .app-header { gap: 9px; }
  .free-badge { display: none; }
  .quick-actions { gap: 6px; }
  .quick-actions button { min-height: 82px; padding-inline: 5px; }
  .two-columns { grid-template-columns: 1fr; gap: 0; }
  .medical-parameters { grid-template-columns: 1fr; }
  .button-row { grid-template-columns: 1fr; }
  h2 { font-size: 24px; }
}
