:root {
  /* 눈이 편한 녹색 톤 */
  --bg: #e4f5ea;
  --card: #ffffff;
  --text: #123522;
  --sub: #4a7d5f;
  --line: #c6e7d2;
  --accent: #1a9a57;
  --on-accent: #ffffff;
  --danger: #b8443a;
  /* 보기 화면 (밝기와 상관없이 짙은 녹색) */
  --v-bg: #0d2e20;
  --v-page: #134131;
  --v-bar: rgba(14, 66, 44, 0.92);
  --v-panel: rgba(18, 80, 54, 0.97);
  --v-text: #eafaf0;
  --v-sub: #a3dcbb;
  --v-accent: #5fe8a0;
  --v-btn: #16603f;
  --v-btn-line: #23794f;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d2e20;
    --card: #134131;
    --text: #eafaf0;
    --sub: #93d1ad;
    --line: #1f5a40;
    --accent: #4fdc92;
    --on-accent: #0c1a12;
    --danger: #ff7b70;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- 일반 화면 ---------- */

.screen {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 24px) calc(var(--safe-l) + 16px);
}

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
}
.bar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.25rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  min-width: 44px;
  height: 44px;
  border: 0;
  background: none;
  font-size: 1.4rem;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active { background: rgba(127, 127, 127, 0.2); }

h2 { font-size: 0.95rem; color: var(--sub); margin: 24px 4px 8px; font-weight: 600; }

.actions { display: grid; gap: 12px; margin-top: 8px; }

.big-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  text-align: left;
}
.big-btn:active { transform: scale(0.98); }
.big-btn b { display: block; font-size: 1.05rem; }
.big-btn small { color: var(--sub); font-size: 0.85rem; }
.big-icon { font-size: 2rem; }

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--card);
  border-radius: 14px;
  border: 1px solid var(--line);
  overflow: hidden;
}
.list li { display: flex; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list li.empty { display: block; padding: 24px 16px; text-align: center; color: var(--sub); }
.list li.empty p { margin: 0 0 12px; }

/* PC·넓은 화면: 화면 전체를 쓰고, 목록은 여러 칸으로 */
@media (min-width: 900px) {
  #browse.screen { max-width: none; padding-left: calc(var(--safe-l) + 32px); padding-right: calc(var(--safe-r) + 32px); }
  #browse .list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 12px;
    background: none;
    border: 0;
    border-radius: 0;
    overflow: visible;
  }
  #browse .list li {
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--card);
    overflow: hidden;
  }
  #browse .list li.empty { grid-column: 1 / -1; }
  #browse .row { min-height: 64px; }
  #browse .row:hover { background: rgba(127, 127, 127, 0.1); }
  #browse .row.up { width: auto; }
  #browse h2 { margin-top: 32px; }
}

.row {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 10px 14px;
  border: 0;
  background: none;
  text-align: left;
  min-width: 0;
}
.row:active { background: rgba(127, 127, 127, 0.15); }
.row .icon { font-size: 1.3rem; flex: none; }
.row .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; word-break: break-all; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.row .meta { flex: none; color: var(--sub); font-size: 0.85rem; }
.row.up { width: 100%; margin-bottom: 8px; border-radius: 12px; background: var(--card); border: 1px solid var(--line); }

.remove {
  width: 48px;
  border: 0;
  background: none;
  color: var(--sub);
  font-size: 1.3rem;
}

.hint { color: var(--sub); font-size: 0.85rem; text-align: center; margin-top: 28px; }
.small { color: var(--sub); font-size: 0.85rem; margin: 0 0 12px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 4px 16px 16px;
  margin-top: 16px;
}
.card h2 { margin: 12px 0 8px; color: var(--text); font-size: 1rem; }

.field { display: block; margin: 12px 0; }
.field > span { display: block; font-size: 0.85rem; color: var(--sub); margin-bottom: 4px; }
.field input, .field select {
  width: 100%;
  font: inherit;
  font-size: 16px; /* 16px 미만이면 iOS가 자동 확대함 */
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}
.copy-row { display: flex; gap: 8px; align-items: center; }
.copy-row code {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg);
  font-size: 0.85rem;
  word-break: break-all;
  user-select: all;
  -webkit-user-select: all;
}
.account { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 12px; }
.account span { flex: 1; }

.btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  font-size: 0.95rem;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.danger { color: var(--danger); }

/* ---------- 불러오는 중 / 알림 ---------- */

#busy {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(8, 20, 13, 0.5);
}
.busy-box {
  min-width: 220px;
  max-width: 80vw;
  padding: 24px;
  border-radius: 16px;
  background: var(--card);
  text-align: center;
}
.busy-box p { margin: 12px 0; }
.busy-box progress { width: 100%; margin-bottom: 12px; }

.spin {
  width: 32px;
  height: 32px;
  margin: 0 auto;
  border: 3px solid rgba(127, 127, 127, 0.3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#toast, .v-toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + 90px);
  transform: translateX(-50%);
  z-index: 60;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(22, 44, 32, 0.94);
  color: #eef6f0;
  font-size: 0.9rem;
  text-align: center;
  pointer-events: none;
  white-space: pre-line;
}

/* ---------- 보기 화면 ---------- */

body.reading { overflow: hidden; background: var(--v-bg); }
body.reading .screen { display: none; }

#viewer {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: var(--v-bg);
  color: var(--v-text);
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.v-stage {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
}
.v-zoom {
  position: relative;
  width: 100%;
  height: 100%;
  transform-origin: 0 0;
}
.v-spread { will-change: transform; }
.v-spread-old { position: absolute; inset: 0; pointer-events: none; }
.v-spread {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}
#viewer.rtl .v-spread { flex-direction: row-reverse; }
.v-spread img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  min-width: 0;
  object-fit: contain;
  pointer-events: none;
}
.v-spread.two img { max-width: 50%; }
.v-crop { flex: none; overflow: hidden; }
.v-crop img { max-width: none; max-height: none; }
.v-error { padding: 24px; color: var(--v-sub); text-align: center; }

.v-scroller {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
}
.s-page {
  position: relative;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  background: var(--v-page);
}
.s-page img { display: block; width: 100%; height: 100%; }
.s-page img:not([src]) { visibility: hidden; }
.s-page.split { overflow: hidden; }
.s-end { display: grid; place-items: center; padding: 48px 16px calc(var(--safe-b) + 120px); }
.s-end .btn { background: var(--v-btn); border-color: var(--v-btn-line); color: var(--v-text); }
.s-page.split img { position: absolute; top: 0; left: 0; width: 200%; }
.s-page.split[data-side="right"] img { left: -100%; }
.s-page[data-error]::after {
  content: attr(data-error);
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--v-sub);
}

.v-bar {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 4px;
  padding-left: calc(var(--safe-l) + 8px);
  padding-right: calc(var(--safe-r) + 8px);
  background: var(--v-bar);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  transition: transform 0.2s ease, opacity 0.2s ease;
}
.v-top { top: 0; padding-top: var(--safe-t); min-height: calc(var(--safe-t) + 52px); }
.v-bottom { bottom: 0; padding-bottom: calc(var(--safe-b) + 6px); padding-top: 6px; flex-wrap: wrap; }
#viewer.ui-hidden .v-top { transform: translateY(-100%); opacity: 0; pointer-events: none; }
#viewer.ui-hidden .v-bottom { transform: translateY(100%); opacity: 0; pointer-events: none; }

.v-title {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.v-settings { font-size: 1rem; font-weight: 700; }
.v-slider { flex: 1; min-width: 0; height: 44px; accent-color: var(--v-accent); }
.v-label { width: 100%; text-align: center; font-size: 0.8rem; color: var(--v-sub); font-variant-numeric: tabular-nums; }

.v-panel {
  position: absolute;
  z-index: 3;
  top: calc(var(--safe-t) + 56px);
  right: calc(var(--safe-r) + 8px);
  width: min(300px, calc(100vw - 16px));
  padding: 8px 16px 12px;
  border-radius: 14px;
  background: var(--v-panel);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}
.v-panel fieldset { border: 0; padding: 0; margin: 8px 0; }
.v-panel legend { font-size: 0.8rem; color: var(--v-sub); margin-bottom: 4px; }
.v-panel label { display: flex; align-items: center; gap: 8px; min-height: 36px; }
.v-panel input { width: 18px; height: 18px; accent-color: var(--v-accent); }

.v-spinner {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}
.v-toast { bottom: calc(var(--safe-b) + 110px); }
