:root {
  color-scheme: light;
  --bg: #f4f6f2;
  --surface: #ffffff;
  --surface-muted: #eef3ee;
  --ink: #1f2923;
  --muted: #657369;
  --line: #dce4dd;
  --primary: #256d5a;
  --primary-dark: #17483c;
  --accent: #c96f45;
  --accent-soft: #fae8dc;
  --blue-soft: #e7eef8;
  --blue: #476b9d;
  --shadow: 0 18px 45px rgba(36, 49, 40, 0.09);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.82), rgba(255, 255, 255, 0)),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui,
    sans-serif;
  letter-spacing: 0;
}

body[data-page="login"] {
  display: grid;
  place-items: center;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(100% - 40px, 1360px);
  margin: 0 auto;
  padding: 28px 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 24px;
}

.save-status {
  position: fixed;
  left: 18px;
  bottom: 18px;
  z-index: 30;
  max-width: calc(100vw - 36px);
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--primary-dark);
  box-shadow: var(--shadow);
  font-size: 0.82rem;
  font-weight: 900;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.save-status.visible {
  opacity: 1;
  transform: translateY(0);
}

.save-status.error {
  background: #fff1ed;
  color: #9b281c;
}

.main-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.main-nav a {
  min-height: 38px;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--muted);
  font-weight: 800;
  text-decoration: none;
}

.main-nav a.active {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--primary);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.15;
}

h1 {
  font-size: clamp(2rem, 4vw, 3.3rem);
}

h2 {
  font-size: 1.26rem;
}

h3 {
  font-size: 1rem;
}

.date-panel {
  min-width: 230px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
}

.date-panel label,
.field-pair span,
.body-grid span {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 700;
}

.layout-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  gap: 20px;
  align-items: start;
}

.daily-header {
  display: grid;
  grid-template-columns: minmax(220px, 280px);
  gap: 14px;
  margin-bottom: 20px;
}

.main-column,
.settings-column {
  display: grid;
  gap: 20px;
}

.section-block {
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.daily-card {
  margin-bottom: 24px;
}

.metric-grid .daily-card {
  margin-bottom: 0;
}

.section-heading,
.group-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.section-heading.compact {
  margin-bottom: 14px;
}

.status-pill,
.unit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 30px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  white-space: nowrap;
}

.time-checklist {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.time-block {
  min-height: 150px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.time-block h3 {
  margin-bottom: 12px;
}

.check-card {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  margin-top: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-weight: 700;
}

.check-card input {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  flex: 0 0 auto;
}

.empty-state {
  padding: 16px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: #fbfcfb;
  color: var(--muted);
  font-weight: 700;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px;
}

.metric-grid + .metric-grid {
  margin-top: 28px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
  color: var(--ink);
  outline: none;
}

input,
select {
  min-height: 43px;
  padding: 9px 11px;
}

input[type="date"] {
  min-width: 0;
  text-align: left;
  -webkit-appearance: none;
  appearance: none;
}

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

input[type="date"]::-webkit-calendar-picker-indicator {
  margin-left: auto;
}

textarea {
  min-height: 72px;
  padding: 11px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 109, 90, 0.14);
}

.measurement-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 118px;
  gap: 10px;
  margin-bottom: 10px;
}

.switch-line {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 43px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--accent-soft);
  color: #6f3d25;
  font-weight: 800;
}

.switch-line input {
  width: 18px;
  min-height: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.stack-list,
.editable-list {
  display: grid;
  gap: 10px;
}

.record-card,
.editable-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.record-card {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 12px;
}

.record-card strong {
  color: var(--primary-dark);
}

.record-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

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

.body-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.settings-panel {
  position: sticky;
  top: 20px;
}

.settings-group {
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.settings-group + .settings-group {
  margin-top: 18px;
}

.settings-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(340px, 0.8fr);
  gap: 20px;
  align-items: start;
}

.settings-dashboard {
  align-items: stretch;
}

.settings-side-stack {
  display: grid;
  gap: 16px;
  align-content: start;
}

.settings-medicine-panel .editable-list {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.wide-panel {
  grid-column: 1 / -1;
}

.subtle-text {
  margin: -6px 0 14px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.field-control {
  display: block;
  margin-bottom: 10px;
}

.field-control span,
.daily-header label,
.course-card span {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.ghost-button,
.icon-button,
.remove-button {
  border: 0;
  border-radius: 8px;
  font-weight: 800;
}

.ghost-button {
  min-height: 36px;
  padding: 8px 12px;
  background: var(--blue-soft);
  color: var(--blue);
}

.icon-button {
  width: 36px;
  height: 36px;
  background: var(--primary);
  color: #fff;
  font-size: 1.25rem;
  line-height: 1;
}

.remove-button {
  min-height: 36px;
  padding: 8px 10px;
  background: #f6e8e2;
  color: #8b3f25;
}

.editable-card {
  display: grid;
  gap: 9px;
  padding: 10px;
}

.medicine-row {
  grid-template-columns: minmax(0, 1fr);
}

.time-toggle-row,
.toggle-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.time-toggle-row label,
.toggle-grid label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-weight: 800;
}

.time-toggle-row input,
.toggle-grid input {
  width: 16px;
  min-height: 16px;
  height: 16px;
  accent-color: var(--primary);
}

.field-pair {
  margin-bottom: 10px;
}

.insulin-slot-row {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
  align-items: center;
}

.report-field-row {
  grid-template-columns: minmax(180px, 1.3fr) minmax(90px, 0.5fr) minmax(110px, 0.65fr) repeat(2, minmax(90px, 0.5fr)) auto;
  align-items: center;
}

.report-settings-list {
  gap: 12px;
}

.course-list {
  display: grid;
  gap: 18px;
}

.course-card {
  display: grid;
  gap: 16px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.course-editor {
  display: grid;
  grid-template-columns: minmax(220px, 1.2fr) minmax(170px, 0.75fr) minmax(170px, 0.75fr) 180px auto;
  gap: 12px;
  align-items: end;
}

.course-summary {
  min-height: 43px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--blue-soft);
  color: var(--blue);
  font-weight: 800;
}

.course-record-list {
  display: grid;
  gap: 8px;
}

.course-record-row {
  display: grid;
  grid-template-columns: 180px 150px 150px 150px 150px minmax(310px, 1fr);
  gap: 12px;
  align-items: stretch;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 750;
}

.course-record-row > * {
  display: flex;
  align-items: center;
  min-height: 34px;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.course-record-row strong {
  color: var(--primary-dark);
}

.login-shell {
  width: min(100% - 32px, 430px);
}

.login-panel h1 {
  margin-bottom: 18px;
}

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

.login-form span {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.primary-button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-weight: 900;
}

.login-error {
  margin: 0 0 14px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #fff1ed;
  color: #9b281c;
  font-weight: 800;
}

.course-summary span {
  margin: 3px 0 0;
  color: inherit;
  font-size: 0.82rem;
}

.report-table {
  display: grid;
  gap: 14px;
}

.report-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: start;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.report-card-main {
  display: grid;
  grid-template-columns: 140px minmax(0, 1fr);
  gap: 12px;
  align-items: start;
  width: 100%;
  min-height: 58px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ink);
  text-align: left;
}

.report-card-date {
  color: var(--primary-dark);
  font-weight: 900;
}

.report-status {
  justify-self: start;
  min-width: 86px;
  padding: 6px 9px;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--primary);
  font-size: 0.82rem;
  font-weight: 900;
  text-align: center;
}

.report-status.has-alert {
  background: #fff1ed;
  color: #9b281c;
}

.report-values-grid {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 8px;
}

.report-value-cell {
  display: grid;
  gap: 4px;
  min-height: 58px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.report-value-cell small {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
}

.report-value-cell strong {
  color: var(--ink);
  font-size: 1rem;
}

.report-value-cell.abnormal {
  border-color: #c3412f;
  background: #fff1ed;
  color: #8d2418;
}

.report-value-cell.abnormal strong,
.report-value-cell.abnormal small {
  color: #8d2418;
  font-weight: 900;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(20, 30, 25, 0.42);
}

.modal-backdrop[hidden] {
  display: none;
}

.modal-panel {
  width: min(100%, 620px);
  max-height: min(760px, calc(100vh - 44px));
  overflow: auto;
  padding: 20px;
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 24px 70px rgba(18, 31, 24, 0.26);
}

.quiet-button {
  background: var(--surface-muted);
  color: var(--muted);
}

.modal-field-list {
  display: grid;
  gap: 12px;
  margin-top: 14px;
}

.modal-field-list small {
  display: inline-block;
  margin-left: 8px;
  color: var(--primary);
  font-size: 0.75rem;
}

.modal-report-value.abnormal {
  border-color: #c3412f;
  background: #fff1ed;
  color: #8d2418;
  font-weight: 900;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
}

@media (max-width: 1180px) {
  .app-shell {
    width: min(100% - 28px, 1080px);
  }

  .layout-grid {
    grid-template-columns: minmax(0, 1fr) 340px;
    gap: 16px;
  }

  .metric-grid {
    gap: 16px;
  }

  .section-block {
    padding: 16px;
  }

}

@media (max-width: 900px) {
  .topbar,
  .layout-grid,
  .metric-grid,
  .settings-layout,
  .daily-header {
    grid-template-columns: 1fr;
  }

  .topbar {
    display: grid;
  }

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

  .date-panel {
    min-width: 0;
  }

  .settings-panel {
    position: static;
  }

  .wide-panel {
    grid-column: auto;
  }

  .course-editor,
  .report-field-row {
    grid-template-columns: 1fr 1fr;
  }

  .settings-medicine-panel .editable-list {
    grid-template-columns: 1fr;
  }

  .course-record-row {
    grid-template-columns: 160px repeat(4, minmax(110px, 1fr)) minmax(280px, 1.2fr);
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100% - 22px, 100%);
    padding: 18px 0;
  }

  .time-checklist,
  .body-grid,
  .record-fields,
  .record-fields.three,
  .measurement-row {
    grid-template-columns: 1fr;
  }

  .record-card {
    grid-template-columns: 1fr;
  }

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

  .course-editor,
  .course-record-row,
  .report-card,
  .report-card-main,
  .report-field-row {
    grid-template-columns: 1fr;
  }
}
