/**
 * AI Study Scan LIFF - Styles
 * スタイルシート
 */

/* ========================================
 * Reset & Base
 * ======================================== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* iOS/Android WebView スクロール/バウンス完全抑制 */
html {
  position: fixed;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: auto;
  touch-action: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  background: #000;
  position: fixed;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overscroll-behavior: none;
  touch-action: none;
  /* テキスト選択・長押しメニュー無効 */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

/* ========================================
 * Screen Base
 * ======================================== */
.screen {
  display: none;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* ========================================
 * Camera Screen
 * カメラ画面
 * ======================================== */
#camera-screen {
  background: #000;
  position: relative;
  overflow: hidden;
}

.header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: transparent;
  padding: 15px;
  color: #fff;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  text-shadow: 0 2px 8px rgba(0,0,0,0.8);
}

/* カメラビューポート（ズーム用コンテナ） */
#camera-viewport {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
  touch-action: none;
  /* GPU合成レイヤー化でorientation変更を安定化 */
  transform: translateZ(0);
  will-change: contents;
}

#camera-preview {
  /* insetで四辺を親に固定 */
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform-origin: center center;
  /* GPU合成レイヤーとして独立 */
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

/* Orientation変更中の縮小を隠す */
#camera-preview.orientation-changing {
  opacity: 0 !important;
}

/* ========================================
 * Camera Grid
 * ======================================== */
.camera-grid {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 5;
}

.grid-line {
  position: absolute;
  background: rgba(255, 255, 255, 0.3);
}

.grid-vertical-1,
.grid-vertical-2 {
  width: 1px;
  height: 100%;
  top: 0;
}

.grid-vertical-1 { left: 33.33%; }
.grid-vertical-2 { left: 66.66%; }

.grid-horizontal-1,
.grid-horizontal-2 {
  width: 100%;
  height: 1px;
  left: 0;
}

.grid-horizontal-1 { top: 33.33%; }
.grid-horizontal-2 { top: 66.66%; }

/* ========================================
 * Camera Controls
 * ======================================== */
.camera-controls {
  position: absolute;
  bottom: 30px;
  left: 0;
  right: 0;
  height: auto;
  background: transparent;
  padding: 20px 20px 10px 20px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  text-align: center;
  z-index: 100;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  gap: 20px;
}

.hint {
  color: #fff;
  font-size: 12px;
  margin-bottom: 0;
  text-shadow: 0 2px 4px rgba(0,0,0,0.8);
  background: rgba(0,0,0,0.6);
  padding: 8px 12px;
  border-radius: 4px;
}

.camera-buttons {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30px;
}

.camera-sub-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  border: 2px solid rgba(255,255,255,0.5);
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

.camera-sub-btn:active {
  background: rgba(255,255,255,0.3);
}

.camera-sub-btn svg {
  width: 24px;
  height: 24px;
  stroke: #fff;
  stroke-width: 2;
  fill: none;
}

.camera-sub-btn.text-icon {
  color: #fff;
  font-size: 28px;
  font-weight: bold;
}

#shutter-btn {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: #fff;
  border: 4px solid #000;
  cursor: pointer;
  transition: transform 0.1s;
  box-shadow: none;
}

#shutter-btn:active {
  transform: scale(0.95);
  background: #f0f0f0;
}

/* ========================================
 * Edit Screen
 * 編集画面
 * ======================================== */
#edit-screen {
  background: #333;
  position: relative;
}

.edit-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

.edit-message {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.6);
  padding: 10px 16px;
  border-radius: 20px;
  color: #fff;
  font-size: 13px;
  z-index: 5;
  pointer-events: none;
  white-space: nowrap;
}

#edit-canvas {
  /* Canvasを画面全体に拡張 */
  width: 100% !important;
  height: 100% !important;
  touch-action: none;
}

.mode-buttons {
  position: absolute;
  bottom: calc(100px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 10;
}

.mode-btn {
  padding: 8px 16px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  color: rgba(255,255,255,0.6);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 60px;
}

.mode-btn.active {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: rgba(255,255,255,0.95);
  font-weight: 500;
}

.action-buttons {
  position: absolute;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  left: 20px;
  right: 20px;
  display: flex;
  gap: 12px;
  z-index: 10;
  padding-bottom: 10px;
}

.btn-secondary {
  flex: 1;
  padding: 14px;
  background: rgba(0,0,0,0.75);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 12px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  line-height: 1.2;
}

.btn-primary {
  flex: 2;
  padding: 14px;
  background: #06c755;
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  line-height: 1.2;
}

/* ========================================
 * Processing Screen
 * 処理中画面
 * ======================================== */
#processing-screen {
  background: #fff;
  align-items: center;
  justify-content: center;
}

.spinner {
  width: 60px;
  height: 60px;
  border: 4px solid #f0f0f0;
  border-top-color: #06c755;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 20px;
}

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

.processing-text {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
}

.processing-hint {
  font-size: 12px;
  color: #999;
}

/* ========================================
 * Complete Screen
 * 完了画面
 * ======================================== */
#complete-screen {
  background: #fff;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}

.complete-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin-bottom: 10px;
}

.complete-message {
  font-size: 14px;
  color: #666;
  text-align: center;
  margin-bottom: 30px;
  line-height: 1.6;
}

.complete-buttons {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.action-btn {
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.2s;
}

.action-btn-primary {
  background: #06c755;
  color: #fff;
  font-weight: bold;
  box-shadow: 0 2px 8px rgba(6, 199, 85, 0.3);
}

.action-btn-primary:active {
  background: #05b349;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(6, 199, 85, 0.4);
}

.action-btn-secondary {
  background: #f5f5f5;
  color: #333;
  border: 1px solid #ddd;
  font-weight: 500;
  font-size: 15px;
}

.action-btn-secondary:active {
  background: #e8e8e8;
  border-color: #ccc;
}

/* ========================================
 * Error Screen
 * エラー画面
 * ======================================== */
#error-screen {
  background: #fff;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}

.error-title {
  font-size: 24px;
  font-weight: bold;
  color: #e74c3c;
  margin-bottom: 10px;
}

.error-detail {
  font-size: 14px;
  color: #666;
  text-align: center;
  margin-bottom: 30px;
  padding: 0 20px;
  white-space: pre-wrap;
}

/* ========================================
 * Landscape Mode
 * 横画面（ランドスケープ）レイアウト
 * ======================================== */
@media screen and (orientation: landscape) {
  .camera-controls {
    /* 横画面では透明背景のまま全画面に配置 */
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 100%;
    padding: 20px;
    padding-left: calc(20px + env(safe-area-inset-left, 0px));
    padding-right: calc(20px + env(safe-area-inset-right, 0px));
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    flex-direction: row;
    justify-content: space-between;
    align-items: flex-end;
    /* タッチイベントを透過（ズーム機能を有効化）*/
    pointer-events: none;
  }

  /* ボタンとヒントはタッチ可能に */
  .camera-controls .hint,
  .camera-controls .camera-sub-btn,
  .camera-controls #shutter-btn {
    pointer-events: auto;
  }

  /* ヘッダー: 上部中央に表示 */
  #camera-screen .header {
    display: flex;
    top: 0;
    left: 0;
    right: 0;
    height: auto;
    padding: 10px 20px;
    font-size: 14px;
  }

  .hint {
    /* 横画面では中央下に配置 */
    display: block;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    font-size: 11px;
    padding: 6px 10px;
  }

  .camera-buttons {
    /* 横画面では非表示（個別に配置するため）*/
    display: contents;
  }

  /* 撮影ボタン: 右側中央（右手親指位置）*/
  #shutter-btn {
    position: absolute;
    right: calc(30px + env(safe-area-inset-right, 0px));
    top: 50%;
    transform: translateY(-50%);
  }

  /* アルバムボタン: 左下（左手親指位置）*/
  #album-btn {
    position: absolute;
    left: calc(20px + env(safe-area-inset-left, 0px));
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  }

  /* ヘルプボタン: アルバムの右（左下エリア）*/
  #help-btn {
    position: absolute;
    left: calc(88px + env(safe-area-inset-left, 0px));
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  }

  /* ========================================
   * Edit Screen Landscape
   * 編集画面の横画面レイアウト
   * ======================================== */

  /* メッセージを小さく */
  .edit-message {
    top: 10px;
    padding: 6px 12px;
    font-size: 11px;
  }

  /* モード切替ボタンを小さく・右寄せ */
  .mode-buttons {
    bottom: calc(60px + env(safe-area-inset-bottom, 0px));
    right: calc(20px + env(safe-area-inset-right, 0px));
    left: auto;
    transform: none;
    gap: 6px;
  }

  .mode-btn {
    padding: 5px 10px;
    font-size: 10px;
    min-width: 45px;
    border-radius: 6px;
  }

  /* アクションボタンを小さく・右寄せ */
  .action-buttons {
    bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    left: auto;
    right: calc(20px + env(safe-area-inset-right, 0px));
    width: auto;
    gap: 8px;
    padding-bottom: 5px;
  }

  .btn-secondary {
    flex: none;
    padding: 8px 16px;
    font-size: 12px;
    border-radius: 8px;
  }

  .btn-primary {
    flex: none;
    padding: 8px 20px;
    font-size: 13px;
    border-radius: 8px;
  }
}

/* ========================================
 * Image Loading Overlay
 * 画像読み込みオーバーレイ（iCloud対応）
 * ======================================== */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 40px 20px;
}

.loading-overlay .spinner {
  width: 50px;
  height: 50px;
  margin-bottom: 20px;
}

.loading-text {
  font-size: 16px;
  color: #fff;
  margin-bottom: 8px;
}

.loading-hint {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
  max-width: 280px;
  line-height: 1.5;
  margin-bottom: 24px;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.loading-hint.visible {
  opacity: 1;
}

.loading-cancel-btn {
  padding: 12px 32px;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 8px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
}

.loading-cancel-btn:active {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.6);
}
