/* Wallpaper Roulette - style.css */
:root {
  --bg: #0b0b0d;
  --bg-elev: #141417;
  --bg-elev2: #1c1c21;
  --border: #2a2a31;
  --text: #f2f2f4;
  --muted: #9a9aa5;
  --accent: #ffffff;
  --accent-text: #0b0b0d;
  --danger: #ff6b6b;
  --fav: #ff5d7a;
  --shadow: 0 10px 40px rgba(0, 0, 0, .45);
  --radius: 16px;
  --radius-sm: 10px;
  --header-h: 60px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --ease: cubic-bezier(.2, .7, .2, 1);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Noto Sans JP", "Segoe UI", Roboto, sans-serif;
}
[data-theme="light"] {
  --bg: #f4f4f6;
  --bg-elev: #ffffff;
  --bg-elev2: #ececf0;
  --border: #dcdce2;
  --text: #16161a;
  --muted: #6b6b76;
  --accent: #16161a;
  --accent-text: #ffffff;
  --shadow: 0 10px 40px rgba(20, 20, 30, .14);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100dvh;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  transition: background .3s var(--ease), color .3s var(--ease);
}
h1, h2 { line-height: 1.3; margin: 0; }
a { color: inherit; }
img { max-width: 100%; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); font-size: .875rem; }
.center { display: flex; justify-content: center; margin: 16px 0 0; }
kbd { font: inherit; font-size: .75rem; padding: 1px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg-elev); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
svg { width: 22px; height: 22px; flex: none; }

/* ---------- Header ---------- */
.app-header {
  position: sticky; top: 0; z-index: 20;
  min-height: var(--header-h);
  padding: var(--safe-top) 20px 0;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.logo { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 700; letter-spacing: .01em; }
.logo svg { width: 28px; height: 28px; }
.main-nav { display: flex; gap: 4px; }
.main-nav a {
  position: relative; display: flex; align-items: center; gap: 8px;
  min-height: 44px; padding: 0 14px; border-radius: 999px;
  text-decoration: none; color: var(--muted); font-size: .9375rem; font-weight: 500;
  transition: background .2s, color .2s;
}
.main-nav a svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; width: 20px; height: 20px; }
.main-nav a:hover { color: var(--text); background: var(--bg-elev2); }
.main-nav a.active { color: var(--accent-text); background: var(--accent); }
.badge { font-size: .6875rem; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; display: inline-flex; align-items: center; justify-content: center; background: var(--fav); color: #fff; }

/* ---------- Layout ---------- */
main { width: 100%; max-width: 1280px; margin: 0 auto; padding: 20px 20px calc(40px + var(--safe-bottom)); }
.view { animation: fadeIn .35s var(--ease); }
.view.narrow { max-width: 720px; margin: 0 auto; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 8px 0 16px; }
.section-head h1, .section-head h2 { font-size: 1.25rem; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg-elev); color: var(--text);
  font: inherit; font-size: .9375rem; font-weight: 600; text-decoration: none; cursor: pointer;
  transition: transform .15s var(--ease), background .2s, border-color .2s, opacity .2s;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.btn svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; width: 20px; height: 20px; }
.btn:hover { background: var(--bg-elev2); }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { opacity: .88; background: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.big { min-height: 52px; padding: 0 24px; font-size: 1rem; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.btn[aria-pressed="true"] { border-color: var(--fav); color: var(--fav); }
.btn[aria-pressed="true"] .heart { fill: var(--fav); stroke: var(--fav); }
.heart { fill: none; stroke: currentColor; transition: transform .25s var(--ease); }
.btn[aria-pressed="true"] .heart { animation: pop .35s var(--ease); }
@keyframes pop { 40% { transform: scale(1.35); } }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 48px; height: 48px; border-radius: 50%; border: 0; cursor: pointer;
  background: rgba(20, 20, 24, .6); color: #fff; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  transition: background .2s, transform .15s;
}
.icon-btn svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:hover { background: rgba(60, 60, 70, .8); }
.icon-btn:active { transform: scale(.94); }

/* ---------- Viewer ---------- */
.viewer-wrap { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.stage {
  --ar: 0.75;
  --avail-h: calc(100dvh - var(--header-h) - var(--safe-top) - 190px);
  position: relative; margin: 0; overflow: hidden;
  border-radius: calc(var(--radius) + 4px);
  background: var(--bg-elev); border: 1px solid var(--border); box-shadow: var(--shadow);
  cursor: zoom-in; touch-action: pan-y;
  width: min(100%, calc(var(--avail-h) * var(--ar)));
  aspect-ratio: var(--ar);
  max-height: var(--avail-h);
  transition: width .4s var(--ease), aspect-ratio .4s var(--ease);
}
.stage[data-fit="fill"] { width: 100%; aspect-ratio: auto; height: var(--avail-h); max-height: none; min-height: 320px; }
.stage[data-fit="fill"] img { object-fit: cover; }
.stage img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; opacity: 0; transition: opacity .5s var(--ease), transform .6s var(--ease); transform: scale(1.015); user-select: none; -webkit-user-select: none; -webkit-touch-callout: default; }
.stage img.ready { opacity: 1; transform: none; }
.skeleton { position: absolute; inset: 0; background: linear-gradient(110deg, var(--bg-elev) 30%, var(--bg-elev2) 50%, var(--bg-elev) 70%); background-size: 200% 100%; animation: shimmer 1.4s linear infinite; }
.stage.loaded .skeleton { display: none; }
@keyframes shimmer { to { background-position: -200% 0; } }
.loader { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--muted); font-size: .875rem; pointer-events: none; transition: opacity .3s; }
.stage.loaded .loader, .stage.failed .loader { opacity: 0; visibility: hidden; }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--text); animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { position: absolute; inset: 0; z-index: 2; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 24px; text-align: center; background: var(--bg-elev); cursor: default; }
.error-box p { margin: 0; color: var(--muted); max-width: 28ch; }
.res-badge { position: absolute; left: 12px; bottom: 12px; padding: 4px 10px; border-radius: 999px; font-size: .75rem; font-weight: 600; letter-spacing: .02em; color: #fff; background: rgba(0, 0, 0, .55); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); font-variant-numeric: tabular-nums; }
.credit { margin: 0; min-height: 1.6em; color: var(--muted); font-size: .8125rem; text-align: center; }
.credit a { text-decoration: underline; text-underline-offset: 3px; }
.actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.hint { margin: 0; color: var(--muted); font-size: .75rem; text-align: center; }
@media (pointer: coarse) { .hint { display: none; } }

/* ---------- Grid ---------- */
.history { margin-top: 44px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.thumb {
  position: relative; padding: 0; border: 1px solid var(--border); border-radius: var(--radius-sm);
  overflow: hidden; cursor: pointer; background: var(--bg-elev) linear-gradient(110deg, var(--bg-elev) 30%, var(--bg-elev2) 50%, var(--bg-elev) 70%);
  background-size: 200% 100%; animation: shimmer 1.6s linear infinite;
  aspect-ratio: var(--thumb-ar, 0.75); display: block; width: 100%;
  transition: transform .25s var(--ease), border-color .2s; touch-action: manipulation;
}
.thumb:hover { transform: translateY(-2px); border-color: var(--muted); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; opacity: 0; transition: opacity .4s; }
.thumb img.ready { opacity: 1; }
.thumb.done { animation: none; }
.thumb .fav-dot { position: absolute; top: 8px; right: 8px; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; background: rgba(0, 0, 0, .55); color: var(--fav); }
.thumb .fav-dot svg { width: 14px; height: 14px; fill: currentColor; }
.thumb.bad { animation: none; display: grid; place-items: center; color: var(--muted); font-size: .75rem; }
.empty { text-align: center; color: var(--muted); padding: 48px 12px; margin: 0; }
.sentinel { height: 1px; }

/* ---------- Cards / Settings ---------- */
.card { background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 6px 18px; margin-top: 16px; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 0; flex-wrap: wrap; }
.setting + .setting { border-top: 1px solid var(--border); }
.setting-label { display: flex; flex-direction: column; min-width: 0; flex: 1 1 200px; }
.seg { display: inline-flex; padding: 4px; gap: 2px; border-radius: 999px; background: var(--bg-elev2); border: 1px solid var(--border); }
.seg button { min-height: 40px; padding: 0 16px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); font: inherit; font-size: .875rem; font-weight: 600; cursor: pointer; transition: background .2s, color .2s; touch-action: manipulation; }
.seg button[aria-checked="true"] { background: var(--accent); color: var(--accent-text); }
.size-inputs { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); }
.size-inputs input { width: 96px; min-height: 44px; padding: 0 10px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg-elev2); color: var(--text); font: inherit; font-variant-numeric: tabular-nums; }
select { max-width: 100%; min-height: 44px; padding: 0 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--bg-elev2); color: var(--text); font: inherit; }
.about { padding: 18px; }
.about h2 { font-size: 1rem; margin-bottom: 8px; }
.about p { color: var(--muted); font-size: .875rem; margin: 0 0 10px; }
.about a { color: var(--text); text-underline-offset: 3px; }

/* ---------- Lightbox ---------- */
.lightbox { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .94); display: flex; flex-direction: column; animation: fadeInPlain .25s; touch-action: pan-y; }
@keyframes fadeInPlain { from { opacity: 0; } to { opacity: 1; } }
.lb-body { flex: 1; min-height: 0; display: grid; place-items: center; padding: calc(var(--safe-top) + 12px) 12px 8px; position: relative; }
.lb-body img { max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; border-radius: 10px; opacity: 0; transition: opacity .3s; user-select: none; -webkit-user-select: none; }
.lb-body img.ready { opacity: 1; }
.lb-spinner { position: absolute; border-color: rgba(255, 255, 255, .2); border-top-color: #fff; }
.lb-spinner.off { display: none; }
.lb-close { position: absolute; top: calc(var(--safe-top) + 12px); right: calc(var(--safe-right) + 12px); z-index: 2; }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); z-index: 2; }
.lb-nav:active { transform: translateY(-50%) scale(.94); }
.lb-nav.prev { left: calc(var(--safe-left) + 12px); }
.lb-nav.next { right: calc(var(--safe-right) + 12px); }
.lb-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 10px 16px calc(14px + var(--safe-bottom)); color: #d8d8de; }
.lb-meta { font-size: .8125rem; font-variant-numeric: tabular-nums; }
.lb-actions { display: flex; gap: 8px; }
.lb-bar .btn { background: rgba(255, 255, 255, .1); border-color: rgba(255, 255, 255, .18); color: #fff; }
.lb-bar .btn[aria-pressed="true"] { color: var(--fav); border-color: var(--fav); }

/* ---------- Save sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .6); display: flex; align-items: flex-end; justify-content: center; animation: fadeInPlain .2s; }
.sheet-card { width: min(520px, 100%); max-height: 92dvh; overflow: auto; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 22px 22px 0 0; padding: 20px 20px calc(20px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 12px; animation: slideUp .3s var(--ease); }
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-card h2 { font-size: 1.125rem; }
.sheet-card img { align-self: center; max-height: 38dvh; width: auto; border-radius: 12px; -webkit-touch-callout: default; user-select: auto; -webkit-user-select: auto; }
.sheet-card p { margin: 0; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.sheet-actions .btn { flex: 1 1 140px; }
@media (min-width: 700px) { .sheet { align-items: center; } .sheet-card { border-radius: 22px; } }

/* ---------- Toast ---------- */
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); z-index: 80; transform: translate(-50%, 20px); max-width: min(92vw, 520px); padding: 12px 18px; border-radius: 14px; background: var(--accent); color: var(--accent-text); font-size: .875rem; font-weight: 600; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s var(--ease); text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  .app-header { padding-left: 14px; padding-right: 10px; }
  .logo span { display: none; }
  .main-nav a { padding: 0 12px; }
  .main-nav a span:not(.badge) { display: none; }
  .main-nav a.active span:not(.badge) { display: inline; }
  main { padding: 14px 14px calc(32px + var(--safe-bottom)); }
  .grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
  .stage { --avail-h: calc(100dvh - var(--header-h) - var(--safe-top) - 160px); }
  .viewer-wrap { gap: 10px; }
  .actions { flex-wrap: nowrap; width: 100%; gap: 8px; }
  .actions .btn.big { padding: 0 14px; }
  .actions #btn-next { flex: 1 1 auto; }
  .lb-nav { display: none; }
  .seg button { padding: 0 12px; }
}
@media (max-width: 480px) {
  .actions #btn-fav .lbl, .actions #btn-save .lbl { display: none; }
  .actions #btn-fav, .actions #btn-save { flex: 0 0 56px; padding: 0; }
}
@media (min-width: 641px) and (max-width: 1100px) { /* タブレット (iPad) */
  .grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
  .stage { --avail-h: calc(100dvh - var(--header-h) - var(--safe-top) - 200px); }
}
@media (orientation: landscape) and (max-height: 520px) { /* 横向きスマホ */
  .stage { --avail-h: calc(100dvh - var(--header-h) - 130px); }
  .lb-bar { padding-top: 4px; }
}
@media (min-width: 1100px) { .grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
