:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #16211d;
  --muted: #5f6f69;
  --line: #e3e8e6;
  --accent: #0d7a5f;
  --accent-ink: #ffffff;
  --accent-soft: #d6efe6;
  --accent-soft-ink: #0a5c48;
  --projected: #eef7f3;
  --projected-ink: #3c7a68;
  --danger: #c93b3b;
  --danger-soft: #fbe3e3;
  --warn: #b7791f;
  --warn-soft: #fbefd7;
  --shadow: 0 1px 2px rgba(20, 30, 26, .06), 0 8px 24px -12px rgba(20, 30, 26, .18);
  --radius: 18px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1512;
    --card: #14201c;
    --text: #e8efec;
    --muted: #93a49e;
    --line: #23322d;
    --accent: #2fb58f;
    --accent-ink: #06231b;
    --accent-soft: #163d33;
    --accent-soft-ink: #9fe2cc;
    --projected: #12261f;
    --projected-ink: #6fbfa5;
    --danger: #ff6b6b;
    --danger-soft: #3a1a1a;
    --warn: #e0a94a;
    --warn-soft: #3a2c14;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px -12px rgba(0,0,0,.6);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, h4 { margin: 0; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin-bottom: .6rem; }
h4 { font-size: .9rem; margin: 1rem 0 .4rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

.app { max-width: 520px; margin: 0 auto; padding: 0 16px 40px; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 2px 10px;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand h1 { font-size: 1.25rem; }
.brand-flag { font-size: 1.3rem; }
.icon-btn {
  background: transparent; border: 0; color: var(--muted);
  width: 40px; height: 40px; border-radius: 12px; display: inline-flex; align-items: center; justify-content: center;
  font-size: 1.6rem; line-height: 1;
}
.icon-btn:hover { background: var(--line); color: var(--text); }

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

/* Hero */
.hero { position: relative; overflow: hidden; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px; font-size: .8rem; font-weight: 600;
  background: var(--accent-soft); color: var(--accent-soft-ink);
}
.pill.out { background: var(--line); color: var(--muted); }
.hero-num { display: flex; align-items: baseline; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.hero-num .n { font-size: 4rem; font-weight: 800; line-height: 1; letter-spacing: -.04em; color: var(--accent); font-variant-numeric: tabular-nums; }
.hero-num .l { font-size: 1.05rem; color: var(--muted); font-weight: 600; }
.hero.warn .hero-num .n { color: var(--warn); }
.hero.danger .hero-num .n { color: var(--danger); }
.hero-sub { margin: 8px 0 0; font-size: .98rem; }
.hero-sub strong { font-weight: 700; }
.hero-sub.alert { color: var(--danger); font-weight: 600; }
.bar { height: 10px; border-radius: 999px; background: var(--line); margin-top: 14px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width .35s ease; }
.hero.warn .bar > i { background: var(--warn); }
.hero.danger .bar > i { background: var(--danger); }
.bar-label { display: flex; justify-content: space-between; font-size: .82rem; color: var(--muted); margin-top: 6px; font-variant-numeric: tabular-nums; }
.error-box { background: var(--danger-soft); color: var(--danger); padding: 10px 12px; border-radius: 12px; font-size: .9rem; margin-top: 10px; }

/* Calendar */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.cal-title { background: transparent; border: 0; font-weight: 700; font-size: 1.05rem; padding: 6px 10px; border-radius: 10px; }
.cal-title:hover { background: var(--line); }
.cal-weekdays, .cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-weekdays span { text-align: center; font-size: .72rem; color: var(--muted); font-weight: 600; padding: 4px 0; text-transform: uppercase; letter-spacing: .04em; }
.cal-weekdays span:first-child { color: var(--danger); }
.day {
  position: relative; aspect-ratio: 1 / 1; border-radius: 10px; border: 0; background: transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  font-size: .95rem; font-variant-numeric: tabular-nums; padding: 0; color: var(--text);
  transition: transform .08s ease;
}
.day:active { transform: scale(.94); }
.day.blank { pointer-events: none; }
.day.sun { color: var(--danger); }
.day.other { color: var(--muted); opacity: .55; }
.day.present { background: var(--accent-soft); color: var(--accent-soft-ink); font-weight: 600; }
.day.projected { background: var(--projected); color: var(--projected-ink); border: 1px dashed var(--projected-ink); }
.day.over { background: repeating-linear-gradient(135deg, var(--danger-soft) 0 4px, transparent 4px 8px); color: var(--danger); }
.day.today { box-shadow: inset 0 0 0 2px var(--accent); }
.day.last { box-shadow: inset 0 0 0 2px var(--danger); color: var(--danger); font-weight: 800; }
.day.today.last { box-shadow: inset 0 0 0 2px var(--danger), 0 0 0 2px var(--accent); }
.day .tag {
  font-size: .56rem; font-weight: 800; letter-spacing: .06em; line-height: 1; padding: 2px 4px; border-radius: 4px;
  background: var(--accent); color: var(--accent-ink);
}
.day .tag.out { background: var(--text); color: var(--card); }
.day .tag.last { background: var(--danger); color: #fff; }
.day.event { font-weight: 800; }
.legend { display: flex; flex-wrap: wrap; gap: 10px 14px; font-size: .78rem; color: var(--muted); margin-top: 12px; }
.legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin-right: 5px; }
.sw-present { background: var(--accent-soft); }
.sw-projected { background: var(--projected); border: 1px dashed var(--projected-ink); }
.sw-over { background: repeating-linear-gradient(135deg, var(--danger-soft) 0 3px, transparent 3px 6px); border: 1px solid var(--danger-soft); }
.sw-last { box-shadow: inset 0 0 0 2px var(--danger); }
.hint { font-size: .82rem; color: var(--muted); margin: 10px 0 0; }

/* Trips */
.trips { list-style: none; margin: 0; padding: 0; }
.trips li { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); }
.trips li:first-child { border-top: 0; }
.trip-idx { width: 28px; height: 28px; border-radius: 8px; background: var(--accent-soft); color: var(--accent-soft-ink); display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: .85rem; flex: none; }
.trip-main { flex: 1; min-width: 0; }
.trip-dates { font-weight: 600; }
.trip-meta { font-size: .82rem; color: var(--muted); }
.trip-del { border: 0; background: transparent; color: var(--muted); width: 34px; height: 34px; border-radius: 10px; font-size: 1.2rem; }
.trip-del:hover { background: var(--danger-soft); color: var(--danger); }
.trips .empty { color: var(--muted); font-size: .92rem; border: 0; }

/* Planner */
.plan-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; }
.plan-form label, .settings-grid label { display: flex; flex-direction: column; gap: 4px; font-size: .8rem; color: var(--muted); font-weight: 600; }
input[type="date"], input[type="number"], textarea {
  width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 1rem;
  -webkit-appearance: none; appearance: none; min-height: 44px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.plan-result { margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: var(--projected); color: var(--projected-ink); font-size: .95rem; }
.plan-result.bad { background: var(--danger-soft); color: var(--danger); }
.plan-result.neutral { background: var(--bg); color: var(--muted); }
.plan-result b { font-size: 1.25rem; }
#plan-exit-wrap[hidden] { display: none; }
.plan-form:has(#plan-exit-wrap[hidden]) { grid-template-columns: 1fr; }

.footnote { font-size: .78rem; color: var(--muted); text-align: center; margin: 6px 8px 0; }

/* Sheets */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 20; backdrop-filter: blur(2px); }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  background: var(--card); border-radius: 22px 22px 0 0; padding: 10px 18px calc(18px + env(safe-area-inset-bottom));
  max-width: 560px; margin: 0 auto; box-shadow: 0 -8px 40px rgba(0,0,0,.25);
  animation: up .22s ease; max-height: 88vh; overflow: auto;
}
@keyframes up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 2px auto 14px; }
.sheet h3 { font-size: 1.1rem; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.sheet-actions.row { flex-direction: row; }
.sheet-actions.row .btn { flex: 1; }
.btn {
  border: 1px solid var(--line); background: var(--bg); border-radius: 14px; padding: 13px 14px;
  font-weight: 600; text-align: left; display: flex; align-items: center; gap: 10px; min-height: 48px;
}
.btn .em { font-size: 1.2rem; width: 26px; text-align: center; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; justify-content: center; }
.btn.danger { color: var(--danger); }
.btn.subtle { color: var(--muted); justify-content: center; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.settings-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
.settings-grid .check { grid-column: 1 / -1; flex-direction: row; align-items: center; gap: 10px; font-size: .95rem; color: var(--text); font-weight: 500; }
.settings-grid .check input { width: 20px; height: 20px; accent-color: var(--accent); }
#backup-text { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .78rem; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(22px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--card); padding: 12px 16px; border-radius: 14px; z-index: 30;
  display: flex; gap: 14px; align-items: center; font-size: .92rem; box-shadow: 0 10px 30px rgba(0,0,0,.3);
  max-width: calc(100% - 32px); white-space: nowrap;
}
.toast .link { background: transparent; border: 0; color: var(--accent); font-weight: 700; padding: 0; }
@media (prefers-color-scheme: dark) { .toast .link { color: #7fe0c2; } }

@media (max-width: 380px) {
  .hero-num .n { font-size: 3.4rem; }
  .card { padding: 14px; }
}

/* 기준일 · 재입국 사다리 */
.asof { font-size: .78rem; color: var(--muted); margin: 10px 0 0; }
.ladder-title { font-size: .95rem; margin: 16px 0 4px; }
.ladder { width: 100%; border-collapse: collapse; font-size: .92rem; margin-top: 6px; }
.ladder th { text-align: left; font-size: .75rem; color: var(--muted); font-weight: 600; text-transform: none; padding: 4px 0; border-bottom: 1px solid var(--line); }
.ladder td { padding: 8px 0; border-bottom: 1px solid var(--line); }
.ladder tr:last-child td { border-bottom: 0; }
.ladder td:last-child, .ladder th:last-child { text-align: right; }
