* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --blue:       #3B82F6;
  --green:      #22C55E;
  --orange:     #F97316;
  --red:        #EF4444;
  --bg:         #F1F5F9;
  --card:       #FFFFFF;
  --text:       #1E293B;
  --muted:      #94A3B8;
  --border:     #E2E8F0;
  --header-h:   60px;
}

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  /* 3行グリッド：ヘッダー / メイン(可変) / フッター */
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  overflow: hidden;
}

/* ============================================================
   ヘッダー（グリッド row 1）
   ============================================================ */
.app-header {
  background: var(--blue);
  padding-top: env(safe-area-inset-top);
  z-index: 200;
  flex-shrink: 0;
}

.header-inner {
  height: var(--header-h);
  max-width: 430px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-title {
  font-size: 21px;
  font-weight: 800;
  color: #fff;
  letter-spacing: -0.3px;
}
.app-date {
  font-size: 12px;
  color: rgba(255,255,255,0.72);
  margin-top: 2px;
}

.settings-btn {
  border: none;
  background: rgba(255,255,255,0.18);
  border-radius: 12px;
  width: 42px; height: 42px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  color: #fff;
  flex-shrink: 0;
  transition: background 0.18s;
  -webkit-tap-highlight-color: transparent;
}
.settings-btn:active,
.settings-btn.active { background: rgba(255,255,255,0.32); }

/* ============================================================
   設定オーバーレイ & パネル（固定）
   ============================================================ */
.settings-backdrop {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: rgba(0,0,0,0.25);
}
.settings-backdrop.hidden { display: none; }

.settings-panel {
  position: fixed;
  top: calc(var(--header-h) + env(safe-area-inset-top));
  left: 0; right: 0;
  z-index: 160;
  background: var(--card);
  box-shadow: 0 6px 28px rgba(0,0,0,0.14);
}
.settings-panel.hidden { display: none; }

.settings-inner {
  max-width: 430px;
  margin: 0 auto;
  padding: 14px 16px 18px;
  border-radius: 0 0 18px 18px;
}

.settings-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 10px;
}

.schedule-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.schedule-opt {
  border: 2px solid var(--border);
  background: var(--bg);
  border-radius: 12px;
  padding: 10px 4px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 800;
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: all 0.18s;
  -webkit-tap-highlight-color: transparent;
}
.schedule-opt span        { font-size: 11px; font-weight: 500; color: var(--muted); }
.schedule-opt.active      { border-color: var(--blue); background: #EFF6FF; color: var(--blue); }
.schedule-opt.active span { color: #93C5FD; }

/* ============================================================
   メインコンテンツ（グリッド row 2）
   ============================================================ */
#main {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* max-width センタリング＆flex 充填 */
.main-inner {
  max-width: 430px;
  width: 100%;
  margin: 0 auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1;
  min-height: 0;
}

/* ============================================================
   カレンダー（残り領域を全部使う）
   ============================================================ */
.calendar-section {
  background: var(--card);
  border-radius: 20px;
  padding: 16px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.06);
  /* カレンダーが残りスペースを全部使う */
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.month-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  flex-shrink: 0;
}
.month-nav h2 { font-size: 20px; font-weight: 800; }

.nav-btn {
  border: none;
  background: var(--bg);
  border-radius: 12px;
  width: 44px; height: 44px;
  font-size: 28px;
  cursor: pointer;
  color: var(--text);
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.12s;
  -webkit-tap-highlight-color: transparent;
}
.nav-btn:active { background: var(--border); }

.weekday-header {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  margin-bottom: 6px;
  flex-shrink: 0;
}
.weekday-header span {
  text-align: center;
  font-size: 14px;
  font-weight: 700;
  color: var(--muted);
  padding: 3px 0;
}
.weekday-header .col-sun { color: var(--red); }
.weekday-header .col-sat { color: var(--blue); }

/* グリッド：行が均等に残り高さを分割 */
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-auto-rows: 1fr;
  gap: 4px;
  flex: 1;
  min-height: 0;
}

/* 日付セル */
.day-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  font-size: 17px;
  position: relative;
  gap: 3px;
}
.day-cell.other-month { opacity: 0.22; }

.day-cell.today {
  border: 2.5px solid var(--blue);
  color: var(--blue);
  font-weight: 800;
}

/* 全服用：緑丸 */
.day-cell.all-taken {
  background: var(--green);
  color: white;
  font-weight: 700;
  border-radius: 50%;
  aspect-ratio: 1;
  align-self: center;
  width: 80%;
  max-width: 54px;
}
.day-cell.all-taken::after {
  content: '✓';
  position: absolute;
  bottom: 14%;
  font-size: 9px;
  font-weight: 900;
}

.day-num { line-height: 1.2; font-weight: 600; }

/* 一部服用：ドット（●●○） */
.dose-dots {
  display: flex;
  gap: 4px;
  justify-content: center;
}
.dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot.taken  { background: var(--green); }
.dot.missed { background: #CBD5E1; }

.day-cell.col-sun:not(.all-taken) { color: var(--red); }
.day-cell.col-sat:not(.all-taken) { color: var(--blue); }

/* ============================================================
   統計
   ============================================================ */
.stats-section {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  flex-shrink: 0;
}
.stat-card {
  background: var(--card);
  border-radius: 14px;
  padding: 10px 6px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.stat-value { font-size: 18px; font-weight: 800; margin-bottom: 2px; }
.stat-value.green  { color: var(--green); }
.stat-value.blue   { color: var(--blue); }
.stat-value.orange { color: var(--orange); }
.stat-label { font-size: 11px; color: var(--muted); font-weight: 500; }

/* ============================================================
   フッター（グリッド row 3）
   ============================================================ */
.app-footer {
  background: var(--card);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.09);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 200;
  flex-shrink: 0;
}

.footer-inner {
  max-width: 430px;
  margin: 0 auto;
  padding: 12px 14px;
}

/* ============================================================
   服薬ボタン
   ============================================================ */
.dose-grid { display: grid; gap: 10px; }
.doses-1   { grid-template-columns: 1fr; }
.doses-2   { grid-template-columns: repeat(2, 1fr); }
.doses-3   { grid-template-columns: repeat(3, 1fr); }

.dose-card {
  border: none;
  border-radius: 18px;
  padding: 14px 8px;
  cursor: pointer;
  background: var(--blue);
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  box-shadow: 0 4px 16px rgba(59,130,246,0.30);
  transition: background 0.22s, box-shadow 0.22s, transform 0.14s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  min-height: 80px;
}
.dose-card.taken {
  background: var(--green);
  box-shadow: 0 4px 16px rgba(34,197,94,0.30);
}
.dose-card:active { transform: scale(0.94); }

.dose-icon   { font-size: 28px; line-height: 1; }
.dose-name   { font-size: 18px; font-weight: 800; }
.dose-status { font-size: 11px; opacity: 0.82; font-weight: 500; }

.doses-1 .dose-card { flex-direction: row; justify-content: center; gap: 12px; }
.doses-1 .dose-icon { font-size: 38px; }
.doses-1 .dose-name { font-size: 22px; }

/* ============================================================
   日付詳細モーダル（ボトムシート）
   ============================================================ */
.day-backdrop {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0,0,0,0.35);
}
.day-backdrop.hidden { display: none; }

.day-modal {
  position: fixed;
  bottom: 0;
  left: 0; right: 0;
  z-index: 310;
  background: var(--card);
  border-radius: 24px 24px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  max-width: 430px;
  margin: 0 auto;
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -8px 40px rgba(0,0,0,0.18);
  transform: translateY(0);
  transition: transform 0.28s cubic-bezier(0.32,0.72,0,1);
}
.day-modal.hidden {
  display: none;
}

.day-modal-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 12px auto 0;
  flex-shrink: 0;
}

.day-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px 10px;
  flex-shrink: 0;
}
.day-modal-header h3 {
  font-size: 17px;
  font-weight: 800;
  color: var(--text);
}
.day-modal-close {
  border: none;
  background: var(--bg);
  border-radius: 50%;
  width: 30px; height: 30px;
  font-size: 14px;
  cursor: pointer;
  color: var(--muted);
  display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.day-modal-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* モーダル内：服薬セクション */
.modal-section-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 8px;
}

.dose-grid-sm {
  display: grid;
  gap: 8px;
}
.doses-sm-1 { grid-template-columns: 1fr; }
.doses-sm-2 { grid-template-columns: repeat(2, 1fr); }
.doses-sm-3 { grid-template-columns: repeat(3, 1fr); }

.dose-card-sm {
  border: 2px solid var(--border);
  background: var(--bg);
  border-radius: 14px;
  padding: 10px 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: all 0.18s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.dose-card-sm.taken {
  border-color: var(--green);
  background: #F0FDF4;
}
.dose-card-sm .dose-icon  { font-size: 22px; }
.dose-card-sm .dose-name  { font-size: 14px; font-weight: 700; }
.dose-card-sm .dose-status {
  font-size: 10px;
  color: var(--muted);
  font-weight: 500;
}
.dose-card-sm.taken .dose-status { color: var(--green); }
.dose-card-sm.future-locked {
  opacity: 0.4;
  cursor: default;
}

/* モーダル内：イベントセクション */
.event-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.event-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  border-radius: 12px;
  padding: 10px 12px;
}
.event-item-icon { font-size: 20px; line-height: 1; flex-shrink: 0; }
.event-item-label { flex: 1; font-size: 14px; font-weight: 600; }
.event-item-del {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  padding: 0 2px;
  -webkit-tap-highlight-color: transparent;
  line-height: 1;
}

.event-add-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.event-type-btn {
  border: 2px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--text);
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.event-type-btn:active { background: var(--border); }

/* カレンダーのイベント表示 */
.day-cell.has-event {
  background: #FFF3E0;
}
.day-cell.all-taken.has-event {
  background: var(--green); /* 服薬完了の緑を優先 */
}

.day-cell .cell-event-icons {
  display: flex;
  gap: 2px;
  justify-content: center;
  flex-wrap: nowrap;
  line-height: 1;
  flex-shrink: 0;
}
.day-cell .cell-evt-icon {
  font-size: 12px;
}
/* 全服薬の緑丸セルでは白くする */
.day-cell.all-taken .cell-event-icons .cell-evt-icon {
  filter: brightness(0) invert(1);
  opacity: 0.85;
}
