:root {
  --bg: #eef7fa;
  --card: #ffffff;
  --text: #102f3b;
  --muted: #5c7f8f;
  --border: #d8ecf1;
  --primary: #2b86ac;
  --primary-dark: #1c5c78;
  --primary-contrast: #ffffff;
  --predicted-bg: rgba(43, 134, 172, 0.14);
  --predicted-border: #2b86ac;
  --danger: #d64545;
  --danger-contrast: #ffffff;
  --shadow: 0 2px 10px rgba(16, 48, 61, 0.1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1c22;
    --card: #15272e;
    --text: #e8f4f8;
    --muted: #82aab9;
    --border: #234450;
    --primary: #3aa3c9;
    --primary-dark: #7fcbe6;
    --predicted-bg: rgba(58, 163, 201, 0.2);
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

h1, h2 { margin: 0; }

.topbar {
  padding: calc(14px + var(--safe-top)) 20px 12px;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.view {
  padding: 4px 16px calc(100px + var(--safe-bottom));
  max-width: 520px;
  margin: 0 auto;
}
.hidden { display: none !important; }

.card, .next-card, .calendar-card {
  background: var(--card);
  border-radius: 18px;
  padding: 18px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.next-card { text-align: left; }
.next-card .next-label {
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.next-card .next-dates {
  font-size: 24px;
  font-weight: 700;
  margin-top: 4px;
}
.next-card .next-sub {
  font-size: 14px;
  color: var(--muted);
  margin-top: 4px;
}
.next-card .next-badge {
  display: inline-block;
  margin-top: 10px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--predicted-bg);
  color: var(--primary-dark);
  font-size: 12px;
  font-weight: 600;
}

.cal-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.cal-title { font-weight: 700; font-size: 16px; }
.icon-btn {
  background: var(--bg);
  border: none;
  border-radius: 10px;
  width: 34px;
  height: 34px;
  font-size: 18px;
  color: var(--text);
}

.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 4px;
}

.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.cal-day {
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  background: transparent;
  border: none;
}
.cal-day.empty { visibility: hidden; cursor: default; }
.cal-day.period {
  background: var(--primary);
  color: var(--primary-contrast);
  font-weight: 700;
}
.cal-day.predicted {
  background: var(--predicted-bg);
  border: 1.5px dashed var(--predicted-border);
  color: var(--primary-dark);
  font-weight: 700;
}
.cal-day.today::after {
  content: "";
  position: absolute;
  inset: 2px;
  border-radius: 10px;
  border: 2px solid var(--text);
  pointer-events: none;
}

.legend {
  display: flex;
  gap: 14px;
  margin-top: 14px;
  flex-wrap: wrap;
}
.legend-item {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot-period { background: var(--primary); }
.dot-predicted { background: var(--predicted-bg); border: 1.5px dashed var(--predicted-border); }
.dot-today { border: 2px solid var(--text); }

.section-title { font-size: 17px; font-weight: 700; margin-bottom: 10px; }
.hint { font-size: 13px; color: var(--muted); line-height: 1.4; margin: 0 0 12px; }

.history-list { display: flex; flex-direction: column; gap: 10px; }
.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.history-item .h-dates { font-weight: 600; font-size: 15px; }
.history-item .h-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.history-item button {
  border: none;
  background: none;
  color: var(--primary-dark);
  font-size: 14px;
  font-weight: 600;
  padding: 6px 8px;
}
.empty-state { color: var(--muted); font-size: 14px; text-align: center; padding: 24px 0; }

.field {
  display: block;
  margin-bottom: 14px;
}
.field span {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 600;
}
.field input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
}
.field input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn {
  flex: 1;
  padding: 12px 16px;
  border-radius: 12px;
  border: none;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  min-width: 100px;
}
.btn-primary { background: var(--primary); color: var(--primary-contrast); }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); flex: 0 0 auto; }

.status-msg { font-size: 13px; color: var(--muted); margin: 10px 0 0; min-height: 16px; }
.status-msg.error { color: var(--danger); }
.status-msg.success { color: #2a9d5c; }

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 8px 0 calc(8px + var(--safe-bottom));
  z-index: 10;
}
.tab-btn {
  flex: 1;
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
  padding: 4px 0;
}
.tab-btn.active { color: var(--primary-dark); }
.tab-icon { font-size: 20px; }

.fab {
  position: fixed;
  right: 20px;
  bottom: calc(78px + var(--safe-bottom));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--primary-contrast);
  font-size: 28px;
  border: none;
  box-shadow: 0 6px 16px rgba(233, 74, 141, 0.4);
  z-index: 11;
  line-height: 1;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 10, 14, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 20;
}
.modal {
  background: var(--card);
  width: 100%;
  max-width: 520px;
  border-radius: 20px 20px 0 0;
  padding: 22px 20px calc(22px + var(--safe-bottom));
}
.modal h2 { font-size: 18px; margin-bottom: 14px; }
.modal-btn-row { margin-top: 4px; }

@media (min-width: 560px) {
  .modal-backdrop { align-items: center; }
  .modal { border-radius: 20px; }
}
