:root {
  --bg: #f7f4ef;
  --surface: #ffffff;
  --line: #e7e1d6;
  --line-strong: #d8d0c0;
  --ink: #1d1b16;
  --ink-2: #555049;
  --ink-3: #8a8377;
  --accent: #d96a4a;       /* warm coral */
  --accent-soft: #fbe8df;
  --accent-ink: #8a3d24;
  --today: #1d1b16;
  --shadow-sm: 0 1px 0 rgba(20,16,10,0.04);
  --shadow-md: 0 8px 24px rgba(20,16,10,0.08);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 18px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.4;
  overscroll-behavior-y: none;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: 14px 14px 80px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 2px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.brand-mark {
  font-size: 18px;
  color: var(--accent);
  transform: translateY(-1px);
}
.brand-name {
  font-size: 17px;
  letter-spacing: -0.02em;
}
.hbtn {
  background: none;
  border: 1px solid var(--line);
  color: var(--ink-2);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 12px;
  cursor: pointer;
  font-family: var(--font);
}
.hbtn:hover { background: var(--surface); }

/* ---------- Stats bar ---------- */
.stats {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 12px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: var(--shadow-sm);
}
.stats-summary {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}
.stats-upcoming {
  margin-left: auto;
  font-size: 12px;
  color: var(--ink-3);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}
.stats-upcoming strong {
  color: var(--ink);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.upcoming-flag {
  display: inline-flex;
  align-items: center;
}
.upcoming-flag .flag {
  font-size: 15px;
  line-height: 1;
  vertical-align: -2px;
}
.year-select-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  margin-left: 4px;
  color: var(--ink);
  border-radius: 4px;
}
.year-select-wrap:hover { background: #f1ecdf; }
.year-select {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  border: none;
  background: transparent;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
  cursor: pointer;
  padding: 0 16px 0 4px;
  margin: 0;
  outline: none;
  line-height: 1.4;
}
.year-chevron {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--ink-3);
  pointer-events: none;
}
.stats-total {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}
.stats-total-label {
  color: var(--ink-3);
  font-size: 13px;
}
.stats-scroll {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 20px;
}
.stats-empty {
  color: var(--ink-3);
  font-size: 13px;
  padding: 2px 0;
}
.stat-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px 5px 8px;
  border-radius: 999px;
  background: #f1ecdf;
  border: 1px solid transparent;
  font-family: var(--font);
  cursor: default;
}
.stat-pill .flag { font-size: 16px; line-height: 1; }
.stat-pill .count {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}

/* ---------- Tooltip ---------- */
.flag-btn {
  position: relative;
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
}
.flag-btn .tip {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  font-size: 11px;
  padding: 5px 8px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 50;
  letter-spacing: 0.01em;
}
.flag-btn .tip::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 4px solid transparent;
  border-top-color: var(--ink);
}

/* ---------- Picker ---------- */
.picker { }
.picker-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.picker-pill {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  line-height: 1;
  transition: transform 60ms ease, border-color 120ms ease, box-shadow 120ms ease;
  font-family: var(--font);
  color: var(--ink-3);
}
.picker-pill:hover { border-color: var(--line-strong); }
.picker-pill:active { transform: scale(0.96); }
.picker-pill.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
.picker-pill.add {
  border-style: dashed;
  background: transparent;
}
.picker-pill.eraser {
  color: var(--ink-2);
}
.picker-pill.eraser.active {
  border-color: var(--ink);
  background: var(--ink);
  color: #fff;
  box-shadow: none;
}

.toolbar-active-pill.eraser {
  background: var(--ink);
  color: #fff;
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.toolbar-active {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}
.toolbar-label {
  font-size: 12px;
  color: var(--ink-3);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.toolbar-active-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px 5px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-size: 13px;
  font-weight: 500;
  min-width: 0;
}
.toolbar-active-pill .flag { font-size: 16px; line-height: 1; }
.toolbar-active-pill .name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}
.toolbar-active-pill.empty {
  background: #f1ecdf;
  color: var(--ink-3);
  font-style: italic;
}

/* Range toggle */
.toolbar-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  font-size: 12px;
  color: var(--ink-2);
}
.toolbar-toggle input { display: none; }
.toolbar-toggle .track {
  width: 32px;
  height: 18px;
  border-radius: 999px;
  background: var(--line-strong);
  position: relative;
  transition: background 150ms ease;
}
.toolbar-toggle .thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  transition: transform 150ms ease;
  box-shadow: 0 1px 2px rgba(0,0,0,0.18);
}
.toolbar-toggle.on .track { background: var(--accent); }
.toolbar-toggle.on .thumb { transform: translateX(14px); }
.toolbar-toggle.on .label { color: var(--accent-ink); font-weight: 500; }

/* ---------- Calendar ---------- */
.cal {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 8px 8px 10px;
  box-shadow: var(--shadow-sm);
}
.cal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 4px 8px;
}
.cal-title {
  background: none;
  border: none;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--ink);
  font-family: var(--font);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
}
.cal-title:hover { background: #f3eee2; }
.cal-year { color: var(--ink-3); font-weight: 500; }
.cal-nav {
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  border-radius: 8px;
  cursor: pointer;
  color: var(--ink-2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.cal-nav:hover { background: #f3eee2; }

.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  padding: 0 2px 4px;
}
.cal-weekday {
  text-align: center;
  font-size: 11px;
  color: var(--ink-3);
  font-weight: 500;
  letter-spacing: 0.04em;
  padding-bottom: 4px;
}

.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}
.cal-cell {
  aspect-ratio: 1 / 1;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fcfaf5;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: space-between;
  padding: 4px 4px 3px;
  cursor: pointer;
  font-family: var(--font);
  transition: border-color 100ms ease, background 100ms ease, transform 60ms ease;
  touch-action: none;
  user-select: none;
}
.cal-cell:hover { border-color: var(--line-strong); }
.cal-cell:active { transform: scale(0.97); }
.cal-cell.empty {
  visibility: hidden;
  pointer-events: none;
}
.cal-cell.has {
  background: #fff;
}
.cal-cell.today {
  border-color: var(--ink);
}
.cal-cell.today .cal-date {
  color: var(--ink);
  font-weight: 700;
}
.cal-cell.range-start {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
.cal-date {
  font-size: 11px;
  color: var(--ink-3);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  text-align: left;
  line-height: 1;
}
.cal-flags {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  align-items: flex-end;
  justify-content: flex-end;
  flex: 1;
  margin-top: 1px;
}
.cal-flag {
  font-size: 16px;
  line-height: 1;
}

/* ---------- Hint ---------- */
.hint {
  text-align: center;
  font-size: 12px;
  color: var(--ink-3);
  padding: 4px 12px;
  line-height: 1.5;
}

/* ---------- Country Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(20, 16, 10, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
  animation: fade 140ms ease;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  background: var(--surface);
  width: 100%;
  max-width: 420px;
  max-height: min(78vh, 640px);
  border-radius: 22px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: var(--shadow-md);
  animation: pop 180ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
@keyframes pop { from { transform: scale(0.96) translateY(8px); opacity: 0; } }
.modal-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 8px;
  border-bottom: 1px solid var(--line);
}
.modal-search {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 14px;
  font-family: var(--font);
  background: #fcfaf5;
  outline: none;
}
.modal-search:focus { border-color: var(--accent); background: #fff; }
.modal-close {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  font-size: 22px;
  color: var(--ink-3);
  cursor: pointer;
  border-radius: 8px;
  line-height: 1;
}
.modal-close:hover { background: #f3eee2; color: var(--ink); }
.modal-list {
  overflow-y: auto;
  padding: 6px 6px 14px;
}
.modal-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: none;
  background: none;
  padding: 10px 12px;
  border-radius: 10px;
  font-family: var(--font);
  cursor: pointer;
  text-align: left;
  font-size: 14px;
}
.modal-item:hover { background: #f6f1e5; }
.modal-item .flag { font-size: 20px; line-height: 1; }
.modal-item .name { flex: 1; color: var(--ink); }
.modal-item .code { color: var(--ink-3); font-size: 11px; letter-spacing: 0.06em; font-variant-numeric: tabular-nums; }
.modal-empty {
  padding: 24px;
  text-align: center;
  color: var(--ink-3);
}

/* ---------- Small screens ---------- */
@media (max-width: 360px) {
  .cal-flag { font-size: 14px; }
  .cal-date { font-size: 10px; }
}
