/**
 * @fileoverview 学校向け電子振り返りシステム (SRE) - モダンデザインシステム & スタイルシート (旧SRE完全互換・画像対応版)
 */

/* ==========================================================================
   1. デザインシステム変数
   ========================================================================== */
:root {
  /* カラーパレット */
  --color-bg: #f8fafc;
  --color-surface: #ffffff;
  --color-surface-glass: rgba(255, 255, 255, 0.9);
  --color-surface-subtle: #f1f5f9;
  --color-surface-hover: #e2e8f0;
  
  --color-primary: #4f46e5;
  --color-primary-light: #6366f1;
  --color-primary-dark: #4338ca;
  --color-primary-gradient: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  --color-primary-glow: rgba(79, 70, 229, 0.25);

  --color-success: #10b981;
  --color-success-bg: #ecfdf5;
  --color-success-border: #a7f3d0;

  --color-warning: #f59e0b;
  --color-warning-bg: #fffbeb;

  --color-danger: #ef4444;
  --color-danger-bg: #fef2f2;
  --color-danger-border: #fecaca;

  --color-text-main: #0f172a;
  --color-text-muted: #64748b;
  --color-text-subtle: #94a3b8;
  --color-border: #e2e8f0;
  --color-border-focus: #818cf8;

  /* S〜D 評価グレードカラー */
  --color-grade-s: #10b981;
  --color-grade-s-bg: #dcfce7;
  --color-grade-a: #4f46e5;
  --color-grade-a-bg: #e0e7ff;
  --color-grade-b: #0284c7;
  --color-grade-b-bg: #e0f2fe;
  --color-grade-c: #d97706;
  --color-grade-c-bg: #fef3c7;
  --color-grade-d: #dc2626;
  --color-grade-d-bg: #fee2e2;

  /* タイポグラフィ */
  --font-sans: 'Outfit', 'Noto Sans JP', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;

  /* シャドウ階層 */
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -2px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.03);
  --shadow-glow: 0 0 20px var(--color-primary-glow);

  /* 角丸 */
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 20px;
  --radius-full: 9999px;

  /* トランジション */
  --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-normal: 250ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-bounce: 400ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ダークモード対応 */
@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #0b0f19;
    --color-surface: #131b2e;
    --color-surface-glass: rgba(19, 27, 46, 0.9);
    --color-surface-subtle: #1e293b;
    --color-surface-hover: #334155;

    --color-text-main: #f8fafc;
    --color-text-muted: #94a3b8;
    --color-text-subtle: #64748b;
    --color-border: #1e293b;
    --color-border-focus: #6366f1;

    --color-success-bg: rgba(16, 185, 129, 0.15);
    --color-danger-bg: rgba(239, 68, 68, 0.15);
  }
}

/* リセット */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background-color: var(--color-bg);
  color: var(--color-text-main);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-image: 
    radial-gradient(at 0% 0%, rgba(79, 70, 229, 0.08) 0px, transparent 50%),
    radial-gradient(at 100% 100%, rgba(124, 58, 237, 0.08) 0px, transparent 50%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

/* コンテナ */
.app-container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 1.25rem 1rem 3rem;
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* ヘッダー */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--color-border);
  flex-wrap: wrap;
  gap: 0.75rem;
}

.brand-wrapper {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: var(--color-primary-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffffff;
  font-size: 1.25rem;
  box-shadow: var(--shadow-sm);
}

.brand-title {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: var(--color-primary-gradient);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.brand-subtitle {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  font-weight: 500;
}

.user-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 0.875rem;
  background-color: var(--color-surface-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  font-size: 0.8125rem;
  color: var(--color-text-muted);
  font-weight: 500;
  max-width: 320px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--color-success);
  box-shadow: 0 0 6px var(--color-success);
  flex-shrink: 0;
}

/* ==========================================================================
   2. タブナビゲーション
   ========================================================================== */
.tab-navigation {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  background-color: var(--color-surface-subtle);
  padding: 0.375rem;
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
}

.tab-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem 0.5rem;
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  font-size: 0.9375rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  -webkit-user-select: none;
  user-select: none;
}

.tab-btn:hover {
  color: var(--color-text-main);
  background-color: rgba(255, 255, 255, 0.4);
}

.tab-btn.active {
  background-color: var(--color-surface);
  color: var(--color-primary);
  box-shadow: var(--shadow-sm);
}

/* ==========================================================================
   3. カード & パネル
   ========================================================================== */
.card {
  background: var(--color-surface-glass);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 1.75rem;
  box-shadow: var(--shadow-lg);
}

.tab-panel {
  display: none;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity var(--transition-normal), transform var(--transition-normal);
}

.tab-panel.active {
  display: block;
  opacity: 1;
  transform: translateY(0);
}

/* ==========================================================================
   4. フォーム要素
   ========================================================================== */
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group.last {
  margin-bottom: 0;
}

.form-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--color-text-main);
  margin-bottom: 0.5rem;
}

.label-required {
  font-size: 0.6875rem;
  padding: 0.125rem 0.5rem;
  background-color: var(--color-primary-glow);
  color: var(--color-primary);
  border-radius: var(--radius-sm);
  font-weight: 700;
}

.form-control, .form-select {
  width: 100%;
  padding: 0.75rem 0.875rem;
  font-size: 0.9375rem;
  font-family: inherit;
  color: var(--color-text-main);
  background-color: var(--color-surface);
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.form-control:focus, .form-select:focus {
  border-color: var(--color-border-focus);
  box-shadow: 0 0 0 3px var(--color-primary-glow);
}

textarea.form-control {
  min-height: 120px;
  resize: vertical;
  line-height: 1.7;
}

/* 画像アップロード & プレビュー */
.image-upload-wrapper {
  margin-top: 0.25rem;
}

.file-input {
  width: 100%;
  padding: 0.625rem;
  background-color: var(--color-surface);
  border: 1.5px dashed var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 0.875rem;
}

.image-preview-container {
  position: relative;
  display: inline-block;
  margin-top: 0.75rem;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
}

.preview-thumbnail {
  display: block;
  max-width: 180px;
  max-height: 140px;
  object-fit: cover;
}

.btn-remove-image {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 24px;
  height: 24px;
  background: rgba(15, 23, 42, 0.75);
  color: #ffffff;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: bold;
  transition: background var(--transition-fast);
}

.btn-remove-image:hover {
  background: var(--color-danger);
}

.form-hint {
  display: block;
  margin-top: 0.375rem;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

/* 編集モード時のバナー */
.edit-mode-banner {
  background: var(--color-warning-bg);
  border: 1px solid var(--color-warning);
  border-radius: var(--radius-md);
  padding: 0.75rem 1rem;
  margin-bottom: 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.875rem;
  font-weight: 600;
}

.btn-cancel-edit {
  padding: 0.25rem 0.625rem;
  font-size: 0.75rem;
}

.char-counter-wrapper {
  display: flex;
  justify-content: flex-end;
  margin-top: 0.375rem;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  font-family: var(--font-mono);
}

/* ==========================================================================
   5. 評価グレードバッジ (S〜D)
   ========================================================================== */
.grade-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.25rem 0.625rem;
  border-radius: var(--radius-full);
  font-weight: 700;
  font-size: 0.8125rem;
  white-space: nowrap;
}

.grade-badge.grade-s { background: var(--color-grade-s-bg); color: var(--color-grade-s); }
.grade-badge.grade-a { background: var(--color-grade-a-bg); color: var(--color-grade-a); }
.grade-badge.grade-b { background: var(--color-grade-b-bg); color: var(--color-grade-b); }
.grade-badge.grade-c { background: var(--color-grade-c-bg); color: var(--color-grade-c); }
.grade-badge.grade-d { background: var(--color-grade-d-bg); color: var(--color-grade-d); }

/* ==========================================================================
   6. 振り返りタイムライン & 履歴カード
   ========================================================================== */
.filter-header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.section-heading {
  font-weight: 700;
  font-size: 1.0625rem;
}

.filter-select-auto {
  width: auto;
  min-width: 160px;
}

.timeline-container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.history-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 1.25rem;
  box-shadow: var(--shadow-sm);
  transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.history-card:hover {
  box-shadow: var(--shadow-md);
}

.history-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.history-meta-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.history-date {
  font-weight: 700;
  font-size: 0.9375rem;
}

.history-period-badge {
  background-color: var(--color-surface-subtle);
  padding: 0.125rem 0.5rem;
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--color-text-muted);
}

.history-subject-badge {
  background-color: var(--color-primary-glow);
  color: var(--color-primary);
  padding: 0.125rem 0.625rem;
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  font-weight: 700;
}

.history-grades {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.history-text {
  font-size: 0.9375rem;
  line-height: 1.7;
  color: var(--color-text-main);
  white-space: pre-wrap;
  word-break: break-word;
  margin-bottom: 0.75rem;
}

/* 添付写真サムネイル */
.history-image-container {
  margin: 0.75rem 0;
}

.history-image-thumbnail {
  max-width: 200px;
  max-height: 150px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  cursor: pointer;
  transition: transform var(--transition-fast);
  object-fit: cover;
}

.history-image-thumbnail:hover {
  transform: scale(1.03);
}

/* 先生からのフィードバック吹き出し */
.teacher-feedback-box {
  background: linear-gradient(135deg, #eff6ff 0%, #e0e7ff 100%);
  border-left: 4px solid var(--color-primary);
  border-radius: var(--radius-md);
  padding: 0.875rem 1rem;
  margin-top: 0.75rem;
}

.teacher-feedback-title {
  font-size: 0.8125rem;
  font-weight: 700;
  color: var(--color-primary);
  display: flex;
  align-items: center;
  gap: 0.375rem;
  margin-bottom: 0.25rem;
}

.teacher-feedback-text {
  font-size: 0.875rem;
  color: var(--color-text-main);
  line-height: 1.6;
}

.history-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.75rem;
  padding-top: 0.5rem;
  border-top: 1px dashed var(--color-border);
}

.btn-edit-record {
  padding: 0.375rem 0.75rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--color-primary);
  background-color: var(--color-surface-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-edit-record:hover {
  background-color: var(--color-primary-glow);
  border-color: var(--color-primary);
}

/* ==========================================================================
   7. アクションボタン & ユーティリティ
   ========================================================================== */
.btn-primary {
  width: 100%;
  padding: 0.875rem 1.25rem;
  font-size: 1rem;
  font-weight: 700;
  color: #ffffff;
  background: var(--color-primary-gradient);
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  box-shadow: var(--shadow-md);
  transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg), var(--shadow-glow);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-re-auth {
  margin-top: 1.5rem;
  max-width: 280px;
  margin-left: auto;
  margin-right: auto;
}

.btn-secondary {
  padding: 0.625rem 1.25rem;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--color-text-main);
  background-color: var(--color-surface-subtle);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #ffffff;
  animation: spin 0.7s linear infinite;
  display: none;
}

.btn-primary.loading .spinner {
  display: inline-block;
}

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

.skeleton-line {
  height: 20px;
  background: linear-gradient(90deg, var(--color-surface-subtle) 25%, var(--color-surface-hover) 50%, var(--color-surface-subtle) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
  margin-bottom: 1rem;
}

.skeleton-line.w-40 { width: 40%; }
.skeleton-line.w-70 { width: 70%; }
.skeleton-line.w-100 { width: 100%; }

.skeleton-box {
  height: 120px;
  background: linear-gradient(90deg, var(--color-surface-subtle) 25%, var(--color-surface-hover) 50%, var(--color-surface-subtle) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-md);
  margin-bottom: 1rem;
}

.loading-text {
  text-align: center;
  color: var(--color-text-muted);
  font-size: 0.875rem;
  margin-top: 1rem;
}

.error-submessage {
  margin-top: 0.5rem;
  color: var(--color-text-muted);
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.hidden {
  display: none !important;
}

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--color-text-muted);
}

.empty-icon {
  font-size: 2.5rem;
  margin-bottom: 0.75rem;
}

.empty-text {
  margin-top: 0.5rem;
}

@media (max-width: 600px) {
  .form-row {
    grid-template-columns: 1fr;
  }
}
