:root {
  color-scheme: light dark;
  --paper: #faf9f6;
  --ink: #24211d;
  --muted: #6b675f;
  --line: #e3e0d8;
  --field-line: #8f8b81;
  --hover: #f0eee7;
  --field: #ffffff;
  --accent: #2c5b4e;
  --on-accent: #ffffff;
  --danger: #b3261e;
  --on-danger: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #191816;
    --ink: #ece9e2;
    --muted: #a39e94;
    --line: #34322d;
    --field-line: #6e6a62;
    --hover: #25231f;
    --field: #211f1c;
    --accent: #84c4ae;
    --on-accent: #12211c;
    --danger: #f28b82;
    --on-danger: #2a0f0c;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; scroll-padding-top: 64px; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "Segoe UI", sans-serif;
  word-break: keep-all;
  -webkit-font-smoothing: antialiased;
}

h1, h2, p { margin: 0; }

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

:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }

button { cursor: pointer; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* 잠금 화면 */
.gate {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: 24px 16px;
}

.gate-box { width: 100%; max-width: 320px; }
.gate-box h1 { font-size: 20px; font-weight: 700; }
.gate-lead { margin-top: 4px; margin-bottom: 20px; color: var(--muted); }
.gate-box label { display: block; margin-bottom: 4px; font-size: 13px; color: var(--muted); }

.gate-box input {
  display: block;
  width: 100%;
  height: 44px;
  padding: 0 12px;
  background: var(--field);
  border: 1px solid var(--field-line);
  border-radius: 8px;
}

#pw2-wrap { margin-top: 12px; }
.gate-error { min-height: 22px; margin: 8px 0; font-size: 13px; color: var(--danger); }

.primary {
  width: 100%;
  height: 44px;
  background: var(--accent);
  color: var(--on-accent);
  border: 0;
  border-radius: 8px;
  font-weight: 600;
  transition: transform 120ms ease-out;
}

.primary:focus-visible { outline-color: var(--ink); outline-offset: 2px; }
.primary:active { transform: scale(0.98); }
.primary:disabled { opacity: 0.6; cursor: default; }

/* 본문 */
.wrap { max-width: 1040px; margin: 0 auto; padding: 0 24px 80px; }

/* 아래쪽 줄을 고칠 때도 저장 상태가 보이도록 위에 붙여 둔다 */
.top {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.brand { flex: none; font-weight: 600; }
.save-state { margin-left: auto; font-size: 13px; color: var(--danger); text-align: right; }
.save-state[data-state="saving"], .save-state[data-state="saved"] { color: var(--muted); }

.quiet {
  flex: none;
  padding: 6px 10px;
  background: none;
  border: 0;
  border-radius: 6px;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  transition: background-color 120ms ease-out, color 120ms ease-out;
}

.section-title { margin-top: 32px; font-size: 24px; font-weight: 700; letter-spacing: -0.01em; }

.load-state { display: flex; flex-wrap: wrap; align-items: center; gap: 0 8px; margin-top: 24px; color: var(--muted); }
.load-state:has(#load-retry:not([hidden])) #load-text { color: var(--danger); }
#load-retry { color: var(--accent); font-weight: 600; }

.list { margin-top: 32px; }

.list-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 2px 16px;
  margin-bottom: 8px;
}

.list-head h2 { font-size: 16px; font-weight: 600; }
.count { margin-left: 2px; font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }
.summary { display: flex; flex-wrap: wrap; gap: 0 12px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

table { width: 100%; border-collapse: collapse; table-layout: fixed; }

th {
  padding: 0 9px 6px;
  border-bottom: 1px solid var(--line);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  text-align: left;
}

td { padding: 3px 0; border-bottom: 1px solid var(--line); vertical-align: top; }

.c-name { width: 30%; }
.c-amount { width: 196px; }
.c-cycle { width: 92px; }
.c-del { width: 84px; }
th.c-amount { text-align: right; padding-right: 81px; }

td input, td select, td textarea {
  display: block;
  width: 100%;
  min-height: 36.5px;
  padding: 6px 8px;
  background-color: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  transition: background-color 120ms ease-out;
}

td input:focus, td select:focus, td textarea:focus { background-color: var(--field); }

td input { text-overflow: ellipsis; }
td textarea { resize: none; field-sizing: content; max-height: 160px; overflow-wrap: anywhere; }

/* 브라우저마다 높이가 달라지는 기본 선택칸 대신 같은 모양으로 그린다 */
td select {
  appearance: none;
  padding-right: 22px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: right 13px center, right 8px center;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}

::placeholder { color: var(--muted); opacity: 1; }

.amount { display: flex; gap: 2px; }
.amount input { flex: 1; min-width: 0; text-align: right; font-variant-numeric: tabular-nums; }
.amount select { flex: 0 0 70px; width: 70px; }
.amount input[aria-invalid="true"], .amount input[aria-invalid="true"]:focus-visible { border-color: var(--danger); outline-color: var(--danger); }

.del {
  float: right;
  min-height: 36.5px;
  padding: 0 10px;
  background: none;
  border: 0;
  border-radius: 6px;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  transition: background-color 120ms ease-out, color 120ms ease-out;
}

.del[data-armed] { background: var(--danger); color: var(--on-danger); }
.del:disabled { opacity: 0.6; cursor: default; }

.empty { padding: 14px 8px; border-bottom: 1px solid var(--line); color: var(--muted); }

.add {
  margin-top: 6px;
  padding: 8px;
  background: none;
  border: 0;
  border-radius: 6px;
  color: var(--accent);
  font-weight: 600;
  transition: background-color 120ms ease-out, transform 120ms ease-out;
}

.add:active { transform: scale(0.97); }
.add:disabled { opacity: 0.6; cursor: default; }

/* 손가락 화면에서는 누른 뒤 올림 효과가 남으므로 마우스일 때만 쓴다 */
@media (hover: hover) {
  .quiet:hover { background: var(--hover); color: var(--ink); }
  td input:hover:not(:focus), td select:hover:not(:focus), td textarea:hover:not(:focus) { background-color: var(--hover); }
  .del:hover:not([data-armed]) { color: var(--danger); background: var(--hover); }
  .add:hover { background: var(--hover); }
}

@media (max-width: 860px), (pointer: coarse) and (max-width: 932px) {
  .wrap { padding: 0 16px 64px; }
  .section-title { margin-top: 24px; font-size: 22px; }

  .list-head { flex-direction: column; align-items: flex-start; }

  thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  table, tbody { display: block; }

  tr {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
    grid-template-areas: "name name" "amount cycle" "note note" "del del";
    gap: 6px;
    padding: 12px 0;
    border-bottom: 1px solid var(--line);
  }

  td { display: block; padding: 0; border: 0; }
  td.c-name { grid-area: name; width: auto; }
  td.c-amount { grid-area: amount; width: auto; }
  td.c-cycle { grid-area: cycle; width: auto; }
  td.c-note { grid-area: note; width: auto; }
  td.c-del { grid-area: del; width: auto; }

  /* 손가락으로 누르기 쉽게 하고, 아이폰이 글자 입력 때 화면을 키우지 않게 16px */
  td input, td select, td textarea {
    min-height: 44px;
    padding-top: 9px;
    padding-bottom: 9px;
    padding-left: 10px;
    border-color: var(--field-line);
    background-color: var(--field);
    font-size: 16px;
  }

  td input { padding-right: 10px; }
  td textarea { padding-right: 10px; }

  .gate-box input { font-size: 16px; }
  .amount select { flex-basis: 76px; width: 76px; }
  .del, .quiet { min-height: 44px; }
  .add { min-height: 44px; padding: 8px 10px; }
}

@media (prefers-reduced-motion: reduce) {
  .primary, .add { transition: none; }
  .primary:active, .add:active { transform: none; }
}
