/* Euportia ログインゲート
   デザイン言語: 紙の面と墨の文字、陶色のアクセント。
   - 押した瞬間に反応する（:active scale）
   - 入場は scale(0.98)+opacity から、強めの ease-out、300ms 以下
   - 動きはすべて transform / opacity のみ */

:root {
  color-scheme: light;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --gate-bg: #F4F2EC;
  --card-bg: rgba(250, 249, 245, 0.82);
  --card-bg-opaque: #FAF9F5;
  --card-border: rgba(20, 20, 19, 0.08);
  --text: #141413;
  --muted: #6B6659;
  --accent: #C15F3C;
  --accent-text: #A94F30;
  --accent-press: #9F4E31;
  --on-accent: #0D0D0D;
  --field-bg: rgba(20, 20, 19, 0.07);
  --field-border: rgba(20, 20, 19, 0.14);
  --danger: #e5484d;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 12px 40px rgba(0, 0, 0, 0.08);
  --jt-secondary-button-bg: #FAF9F5;
  --jt-secondary-button-text: #141413;
  --jt-secondary-button-border: rgba(20, 20, 19, 0.14);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --gate-bg: #14130F;
    --card-bg: rgba(28, 26, 21, 0.82);
    --card-bg-opaque: #1C1A15;
    --card-border: rgba(237, 233, 223, 0.10);
    --text: #EDE9DF;
    --muted: rgba(237, 233, 223, 0.62);
    --accent: #C15F3C;
    --accent-text: #D97757;
    --accent-press: #9F4E31;
    --on-accent: #0D0D0D;
    --field-bg: rgba(237, 233, 223, 0.12);
    --field-border: rgba(237, 233, 223, 0.16);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 40px rgba(0, 0, 0, 0.5);
    --jt-secondary-button-bg: #1C1A15;
    --jt-secondary-button-text: #EDE9DF;
    --jt-secondary-button-border: rgba(237, 233, 223, 0.16);
  }
}

/* 設定画面で固定テーマを選んだ場合は端末設定より優先する。 */
:root[data-jt-theme="light"] {
  color-scheme: light;
  --gate-bg: #F4F2EC;
  --card-bg: rgba(250, 249, 245, 0.82);
  --card-bg-opaque: #FAF9F5;
  --card-border: rgba(20, 20, 19, 0.08);
  --text: #141413;
  --muted: #6B6659;
  --accent: #C15F3C;
  --accent-text: #A94F30;
  --accent-press: #9F4E31;
  --on-accent: #0D0D0D;
  --field-bg: rgba(20, 20, 19, 0.07);
  --field-border: rgba(20, 20, 19, 0.14);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 12px 40px rgba(0, 0, 0, 0.08);
  --jt-secondary-button-bg: #FAF9F5;
  --jt-secondary-button-text: #141413;
  --jt-secondary-button-border: rgba(20, 20, 19, 0.14);
}

:root[data-jt-theme="dark"] {
  color-scheme: dark;
  --gate-bg: #14130F;
  --card-bg: rgba(28, 26, 21, 0.82);
  --card-bg-opaque: #1C1A15;
  --card-border: rgba(237, 233, 223, 0.10);
  --text: #EDE9DF;
  --muted: rgba(237, 233, 223, 0.62);
  --accent: #C15F3C;
  --accent-text: #D97757;
  --accent-press: #9F4E31;
  --on-accent: #0D0D0D;
  --field-bg: rgba(237, 233, 223, 0.12);
  --field-border: rgba(237, 233, 223, 0.16);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 12px 40px rgba(0, 0, 0, 0.5);
  --jt-secondary-button-bg: #1C1A15;
  --jt-secondary-button-text: #EDE9DF;
  --jt-secondary-button-border: rgba(237, 233, 223, 0.16);
}

/* ── 紙の背景（ゲートとアプリで共有） ── */

body { background: var(--gate-bg); }

/* スクロール領域も OS の既定色へ戻さず、同じ中性色で描く。 */
* {
  scrollbar-color: color-mix(in srgb, var(--text) 30%, transparent) transparent;
  scrollbar-width: thin;
}

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  border: 3px solid transparent;
  border-radius: 9px;
  background: color-mix(in srgb, var(--text) 30%, transparent);
  background-clip: padding-box;
}

/*
 * 背景の装飾は置かない。地は単色で、そこに文字と
 * 余白だけが乗る。ぼかした円を漂わせると、読むものより背景が目に入る。
 *
 * #jt-bg 自体は互換のため残すが、中身は描かない。
 */
#jt-bg {
  display: none;
}

/* アプリ本体は紙の地の上に載せる（背景は skin が透過化する） */
#root { position: relative; z-index: 1; }

:where(#jt-gate, #jt-pill-wrap) :where(button, input) {
  -webkit-appearance: none;
  appearance: none;
}

/* ── 未ログイン操作のEuportiaログインモーダル ── */
#jt-auth-modal[hidden] { display: none !important; }

#jt-auth-modal {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--gate-bg) 42%, transparent);
  backdrop-filter: blur(12px) saturate(1.2);
  -webkit-backdrop-filter: blur(12px) saturate(1.2);
}

.auth-modal-frame {
  position: relative;
  width: min(520px, 100%);
  height: min(760px, calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom)));
  overflow: visible;
  border: 0;
  background: transparent;
  transform: translateY(0) scale(1);
  animation: auth-modal-in 220ms var(--ease-out) both;
}

@keyframes auth-modal-in {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

#auth-modal-content {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: transparent;
}

@media (max-width: 560px) {
  #jt-auth-modal { padding-left: 8px; padding-right: 8px; }
  .auth-modal-frame {
    height: calc(100dvh - max(16px, env(safe-area-inset-top)) - max(16px, env(safe-area-inset-bottom)));
  }
}

@media (prefers-reduced-motion: reduce) {
  .auth-modal-frame { animation: none; }
}

/* ── ゲート全景 ─────────────────────────────── */

/* hidden 属性を author CSS の display:flex より優先させる
   （これがないと透明なゲートが残って全タップを吸ってしまう） */
#jt-gate[hidden] { display: none !important; }

#jt-gate {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  /* オーロラを透かす薄い被膜 */
  background: color-mix(in srgb, var(--gate-bg) 45%, transparent);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Segoe UI", "Yu Gothic UI", Meiryo, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-y: auto;
}

#jt-gate.gate-leave {
  opacity: 0;
  transition: opacity 200ms var(--ease-out);
}

.gate-card {
  width: 100%;
  max-width: 360px;
  padding: 40px 32px 24px;
  border-radius: 24px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px) saturate(1.5);
  -webkit-backdrop-filter: blur(20px) saturate(1.5);
  text-align: center;
  opacity: 1;
  transform: translateY(0) scale(1);
  transition: opacity 240ms var(--ease-out), transform 240ms var(--ease-out);
}

@starting-style {
  .gate-card {
    opacity: 0;
    transform: translateY(10px) scale(0.98);
  }
}

/* 閲覧モード用の閉じるボタン（カード右上） */
.gate-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--field-bg);
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  transition: transform 160ms var(--ease-out), background-color 150ms ease;
}

.gate-close:active { transform: scale(0.92); }

.gate-close[hidden] { display: none; }

.gate-card { position: relative; }

.gate-logo {
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  animation: field-in 300ms var(--ease-out) backwards 0ms;
}

.gate-title {
  margin: 14px 0 26px;
  font-size: 27px;
  font-weight: 700;
  letter-spacing: -0.03em;
  animation: field-in 300ms var(--ease-out) backwards 40ms;
}

/* ── セグメントコントロール ─────────────────── */

.seg {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  margin-bottom: 20px;
  border-radius: 12px;
  background: var(--field-bg);
}

.seg-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: calc(50% - 3px);
  height: calc(100% - 6px);
  border-radius: 9px;
  background: var(--card-bg);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
  transform: translateX(0);
  transition: transform 200ms var(--ease-out);
}

.seg[data-mode="signup"] .seg-thumb { transform: translateX(100%); }

.seg-btn {
  position: relative;
  z-index: 1;
  min-height: 44px;
  padding: 8px 0;
  border: 0;
  background: none;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: color 150ms ease;
}

.seg-btn.is-active { color: var(--text); }

/* ── フォーム ───────────────────────────────── */

.field {
  display: block;
  margin-bottom: 12px;
  text-align: left;
}

.field[hidden], .gate-guest[hidden] { display: none; }

.field-label {
  display: block;
  margin-bottom: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.field input {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  min-height: 44px;
  box-sizing: border-box;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--field-border);
  background: transparent;
  font: inherit;
  font-size: 16px; /* 16px 未満だと iOS Safari がフォーカス時に自動ズームする */
  color: var(--text);
  outline: none;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}

.field input:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}

.field-hint {
  display: block;
  margin-top: 5px;
  font-size: 11px;
  line-height: 1.6;
  color: var(--muted);
}

.field-hint[hidden] { display: none; }

/* フィールドの入場スタガー（初回のみ・装飾） */
#view-auth .field:nth-child(1), #view-lock .field:nth-child(1), #view-password .field:nth-child(1) { animation: field-in 260ms var(--ease-out) backwards 40ms; }
#view-auth .field:nth-child(2) { animation: field-in 260ms var(--ease-out) backwards 90ms; }

@keyframes field-in {
  from { opacity: 0; transform: translateY(6px); }
}

/* ── ボタン ─────────────────────────────────── */

.btn {
  position: relative;
  width: 100%;
  min-height: 44px;
  padding: 12px 16px;
  margin-top: 6px;
  border: 0;
  border-radius: 12px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 160ms var(--ease-out), background-color 150ms ease;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  color: var(--on-accent);
}

.btn-primary:active { background: var(--accent-press); }

.btn[disabled] { cursor: default; opacity: 0.8; }

.btn .btn-spinner {
  display: none;
  position: absolute;
  right: 14px;
  top: 50%;
  width: 16px;
  height: 16px;
  margin-top: -8px;
  border: 2px solid color-mix(in srgb, var(--on-accent) 35%, transparent);
  border-top-color: var(--on-accent);
  border-radius: 50%;
  animation: spin 480ms linear infinite;
}

.btn.is-busy .btn-spinner { display: block; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ── テキスト類 ─────────────────────────────── */

.gate-error {
  margin: 2px 0 8px;
  font-size: 13px;
  color: var(--danger);
  text-align: left;
  opacity: 1;
  transition: opacity 150ms ease;
}

.gate-error[hidden] { display: none; }

.gate-note {
  margin: 14px 0 0;
  font-size: 12px;
  line-height: 1.7;
  color: var(--muted);
  text-align: left;
}

.gate-consents {
  display: grid;
  gap: 9px;
  margin: 4px 0 12px;
  padding: 12px;
  border: 1px solid var(--card-border);
  border-radius: 13px;
  background: color-mix(in srgb, var(--field-bg) 70%, transparent);
  text-align: left;
}

.gate-consents[hidden], #recovery-confirm[hidden] { display: none; }

.gate-check {
  position: relative;
  display: flex;
  min-height: 44px;
  align-items: flex-start;
  gap: 9px;
  font-size: 11px;
  line-height: 1.55;
  color: var(--muted);
  text-align: left;
}

.gate-check input {
  -webkit-appearance: none;
  appearance: none;
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
}

.gate-check-box {
  position: relative;
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  border: 1px solid var(--field-border);
  border-radius: 5px;
  background: var(--field-bg);
  transition: border-color 150ms ease, background-color 150ms ease;
}

.gate-check input:checked + .gate-check-box {
  border-color: var(--accent);
  background: var(--accent);
}

.gate-check input:checked + .gate-check-box::after {
  position: absolute;
  top: 2px;
  left: 5px;
  width: 5px;
  height: 9px;
  border: solid var(--on-accent);
  border-width: 0 2px 2px 0;
  content: "";
  transform: rotate(45deg);
}

.gate-check input:focus-visible + .gate-check-box {
  outline: 3px solid color-mix(in srgb, var(--accent) 72%, var(--on-accent) 10%);
  outline-offset: 2px;
}
.gate-check a { color: var(--accent-text); font-weight: 600; }
.gate-check-danger { margin: 4px 0 10px; color: var(--danger); }
.recovery-warning { margin-bottom: 16px; }

.gate-guest {
  display: inline-block;
  min-height: 44px;
  margin-top: 18px;
  padding: 6px 10px;
  border: 0;
  border-radius: 8px;
  background: none;
  font: inherit;
  font-size: 13px;
  font-weight: 400;
  color: var(--accent-text);
  cursor: pointer;
  transition: transform 160ms var(--ease-out), background-color 150ms ease;
}

.gate-guest:active { transform: scale(0.97); }

@media (hover: hover) and (pointer: fine) {
  .gate-guest:hover { background: var(--field-bg); }
}

.gate-guest-sub { color: var(--muted); font-weight: 400; }

.lock-hello {
  margin: 4px 0 2px;
  font-size: 17px;
  font-weight: 600;
}

.lock-email {
  margin: 0 0 18px;
  font-size: 13px;
  color: var(--muted);
  word-break: break-all;
}

.gate-foot {
  margin: 22px 0 0;
  font-size: 11px;
  color: var(--muted);
}

.gate-foot a { color: inherit; }

/* ── 同期ステータスピル ─────────────────────── */

#jt-pill-wrap {
  position: fixed;
  /* 右上配置: アプリ下部のタブバーと重ならない。ノッチも回避 */
  top: calc(10px + env(safe-area-inset-top, 0px));
  right: calc(12px + env(safe-area-inset-right, 0px));
  z-index: 900;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Segoe UI", "Yu Gothic UI", Meiryo, sans-serif;
}

#jt-pill-wrap[hidden] { display: none !important; }

/*
 * PCでは出さない。
 *
 * アプリ側（_layout.js が注入するCSS）でも 1024px 以上では隠しているが、
 * あれはExpoが起動したあとに効く。gate.js は起動前にこのピルを表示するので、
 * 再読み込みのたびに右上で**同期マークとクレジット残高が一瞬光っていた**。
 * 起動前に効くこのファイルで止める。
 *
 * PCでは左のサイドバーのアカウント行から、契約・アカウント設定・ログアウトへ
 * 行けるので、ここを消しても行き止まりにならない。
 * 狭い画面ではサイドバーが出ないため、ピルを残す。
 */
@media (min-width: 1024px) {
  #jt-pill-wrap { display: none !important; }
}

#jt-account-menu { position: relative; }

#jt-pill {
  display: flex;
  width: 87px;
  min-height: 44px;
  box-sizing: border-box;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid var(--card-border);
  border-radius: 999px;
  background: var(--card-bg);
  backdrop-filter: blur(16px) saturate(1.5);
  -webkit-backdrop-filter: blur(16px) saturate(1.5);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  font-size: 12px;
  font-weight: 400;
  color: var(--muted);
  cursor: pointer;
  transition: transform 160ms var(--ease-out);
}

#pill-text {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

#jt-pill:active { transform: scale(0.96); }

/* モバイル(ログイン後): 状態ドットだけの最小表示。タップで従来のメニュー */
#jt-pill.pill-mini {
  width: 44px;
  height: 44px;
  padding: 0;
  justify-content: center;
  border-radius: 50%;
}

#jt-pill.pill-mini #pill-text { display: none; }

/* ダークでの視認性: ピルの背景を持ち上げ、ドットに発光リング */
@media (prefers-color-scheme: dark) {
  #jt-pill {
    background: var(--card-bg-opaque);
    border-color: rgba(237, 233, 223, 0.18);
  }
  .pill-dot {
    box-shadow: 0 0 0 2px rgba(237, 233, 223, 0.22), 0 0 8px currentColor;
  }
  .pill-dot { color: #30d158; }
  .pill-dot[data-state="syncing"] { color: #ff9f0a; }
  .pill-dot[data-state="error"] { color: var(--danger); }
  .pill-dot[data-state="guest"] { color: var(--muted); }
}

/* 未ログイン（閲覧モード）: はっきりしたログインボタンにする */
#jt-pill.pill-cta {
  background: var(--accent);
  border-color: transparent;
  color: var(--on-accent);
  font-weight: 600;
}

#jt-pill.pill-cta .pill-dot { display: none; }

.pill-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  color: #30d158;
  background: currentColor;
  transition: color 200ms ease;
}

.pill-dot[data-state="syncing"] { color: #ff9f0a; }
.pill-dot[data-state="error"]   { color: var(--danger); }
.pill-dot[data-state="guest"]   { color: var(--muted); }

#jt-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  min-width: 200px;
  padding: 6px;
  border: 1px solid var(--card-border);
  border-radius: 14px;
  background: var(--card-bg);
  backdrop-filter: blur(20px) saturate(1.5);
  -webkit-backdrop-filter: blur(20px) saturate(1.5);
  box-shadow: var(--shadow);
  transform-origin: top right; /* トリガー（右上ボタン）起点で開く */
  opacity: 1;
  transform: scale(1);
  transition: opacity 180ms var(--ease-out), transform 180ms var(--ease-out);
}

#jt-panel[hidden] { display: none; }

@starting-style {
  #jt-panel { opacity: 0; transform: scale(0.95); }
}

.panel-title {
  margin: 6px 10px 2px;
  font-size: 11px;
  font-weight: 700;
  color: var(--text);
}

.panel-entitlements {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 8px 8px;
}

.panel-entitlements span {
  padding: 5px 8px;
  border-radius: 999px;
  background: var(--field-bg);
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
}

.panel-btn {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 8px 10px;
  border: 0;
  border-radius: 9px;
  background: none;
  font: inherit;
  font-size: 13px;
  font-weight: 400;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  transition: transform 160ms var(--ease-out), background-color 120ms ease;
}

.panel-link { text-decoration: none; }

.panel-btn:active { transform: scale(0.98); }

@media (hover: hover) and (pointer: fine) {
  .panel-btn:hover { background: var(--field-bg); }
}

.panel-danger { color: var(--danger); }

/* ゲート内の全ボタン: ダブルタップズーム由来のタップ遅延を無効化 */
#jt-gate button,
#jt-pill-wrap button {
  touch-action: manipulation;
}

:where(#jt-gate, #jt-pill-wrap) :where(button, a, input):focus { outline: none; }
:where(#jt-gate, #jt-pill-wrap) :where(button, a, input):focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 72%, var(--on-accent) 10%);
  outline-offset: 2px;
}

@media (prefers-contrast: more) {
  /* currentColor は端末側のシステム色に解決される。iOS の
     「コントラストを上げる」で青くなるので、テーマの値を使う。 */
  .gate-card, .field input, .seg, #jt-panel { border-color: var(--text); }
}

@media (forced-colors: active) {
  :where(#jt-gate, #jt-pill-wrap) :where(button, input, a):focus-visible { outline: 3px solid CanvasText; }
  .gate-check-box { forced-color-adjust: auto; }
  .gate-check input:checked + .gate-check-box::after { border-color: HighlightText; }
}

/* ── アクセシビリティ ───────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .gate-card, #jt-panel { transition: opacity 200ms ease; transform: none; }
  @starting-style {
    .gate-card, #jt-panel { transform: none; }
  }
  #view-auth .field, #view-lock .field { animation: none; }
  .gate-logo, .gate-title { animation: none; }
  #jt-bg i { animation: none; }
  .btn:active, .gate-guest:active, #jt-pill:active, .panel-btn:active { transform: none; }
  .seg-thumb { transition: none; }
}

/* 透明度を下げる設定では、背景の内容をぼかして見せることができない。
   半透明面をテーマごとの不透明な面へ置き換え、前景の境界を保つ。 */
@media (prefers-reduced-transparency: reduce) {
  #jt-auth-modal,
  .gate-card,
  #jt-pill,
  #jt-panel {
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }

  #jt-auth-modal { background: var(--gate-bg) !important; }
  .gate-card,
  #jt-pill,
  #jt-panel { background: var(--card-bg-opaque) !important; }

  /* The guest pill is a primary login action, not a translucent status pill.
     Keep its opaque accent treatment so semantic action text remains readable. */
  #jt-pill.pill-cta { background: var(--accent) !important; }
}
