/* ============================================
   OEKAKI - お絵描きアプリ スタイルシート
   ============================================ */

:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #161628;
  --bg-tertiary: #1e1e35;
  --bg-elevated: #252540;
  --surface: #1a1a30;
  --surface-hover: #252545;
  --border: rgba(255,255,255,0.08);
  --border-strong: rgba(255,255,255,0.15);

  --accent-purple: #a78bfa;
  --accent-pink: #f472b6;
  --accent-cyan: #38bdf8;
  --accent-green: #34d399;
  --accent-orange: #fb923c;
  --accent-red: #f87171;
  --accent-yellow: #fbbf24;

  --text-primary: #e4e4f0;
  --text-secondary: #8888aa;
  --text-muted: #55556a;

  --sidebar-width: 248px;
  --topbar-height: 54px;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 22px;

  --shadow-sm: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 20px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 40px rgba(0,0,0,0.6);

  --transition: 0.18s ease;
}

/* ダークモード（デフォルト）/ライトモード */
body.light-mode {
  --bg-primary: #f0f0f8;
  --bg-secondary: #e8e8f5;
  --bg-tertiary: #ddddf0;
  --bg-elevated: #d5d5ea;
  --surface: #e8e8f5;
  --surface-hover: #d8d8ee;
  --border: rgba(0,0,0,0.08);
  --border-strong: rgba(0,0,0,0.15);
  --text-primary: #1a1a2e;
  --text-secondary: #555570;
  --text-muted: #99998a;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

body {
  font-family: 'Noto Sans JP', 'Inter', system-ui, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  display: flex;
  font-size: 13px;
}

/* ============================================
   サイドバー
   ============================================ */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100vh;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width var(--transition), min-width var(--transition), transform var(--transition);
  z-index: 100;
  position: relative;
}

.sidebar.collapsed {
  width: 0;
  min-width: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg-secondary);
}

.app-logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-name {
  font-family: 'Inter', sans-serif;
  font-weight: 700;
  font-size: 16px;
  background: linear-gradient(135deg, var(--accent-purple), var(--accent-pink));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 1px;
}

.toggle-btn, .mobile-menu-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
}

.toggle-btn:hover, .mobile-menu-btn:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}

.sidebar-content::-webkit-scrollbar {
  width: 4px;
}
.sidebar-content::-webkit-scrollbar-track { background: transparent; }
.sidebar-content::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 4px;
}

/* ============================================
   ツールセクション
   ============================================ */
.tool-section {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.tool-section.collapsed {
  padding-bottom: 10px;
}
.tool-section.collapsed > *:not(.section-label) {
  display: none !important;
}

.tool-section:last-child {
  border-bottom: none;
}

.section-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
}
.tool-section.collapsed .section-label {
  margin-bottom: 0px;
}
.section-toggle-icon svg {
  transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.value-badge {
  font-size: 11px;
  font-weight: 600;
  color: var(--accent-purple);
  text-transform: none;
  letter-spacing: 0;
  background: rgba(167,139,250,0.12);
  padding: 1px 6px;
  border-radius: 20px;
}

/* ツールグリッド */
.tool-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.tool-btn {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: var(--radius-md);
  padding: 10px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  font-size: 10px;
  font-weight: 500;
  transition: all var(--transition);
  font-family: inherit;
  min-height: 58px;
}

.tool-btn:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
  border-color: var(--border-strong);
  transform: translateY(-1px);
  box-shadow: var(--shadow-sm);
}

.tool-btn.active {
  background: linear-gradient(135deg, rgba(167,139,250,0.25), rgba(244,114,182,0.2));
  border-color: var(--accent-purple);
  color: var(--accent-purple);
  box-shadow: 0 0 12px rgba(167,139,250,0.2);
}

/* スライダー */
.slider-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 5px;
  border-radius: 3px;
  background: var(--bg-elevated);
  outline: none;
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-purple), var(--accent-pink));
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(167,139,250,0.4);
  border: 2px solid rgba(255,255,255,0.2);
  transition: transform var(--transition);
}

.slider::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

.slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent-purple), var(--accent-pink));
  cursor: pointer;
  border: 2px solid rgba(255,255,255,0.2);
}

.size-presets {
  display: flex;
  gap: 4px;
}

.size-preset {
  flex: 1;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  padding: 3px 2px;
  font-size: 9px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
}

.size-preset:hover, .size-preset.active {
  background: rgba(167,139,250,0.15);
  border-color: var(--accent-purple);
  color: var(--accent-purple);
}

/* ============================================
   カラーパレット
   ============================================ */
.color-picker-wrap {
  margin-bottom: 10px;
}

.current-color-display {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
}

.current-color-box {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border-strong);
  cursor: pointer;
  transition: transform var(--transition);
  flex-shrink: 0;
}

.current-color-box:hover {
  transform: scale(1.05);
}

.native-color-picker {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  pointer-events: none;
}

.color-hex {
  font-family: 'Inter', monospace;
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.palette-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  margin-bottom: 8px;
}

.palette-color {
  aspect-ratio: 1;
  border-radius: 5px;
  cursor: pointer;
  border: 1px solid rgba(255,255,255,0.1);
  transition: transform var(--transition), box-shadow var(--transition);
}

.palette-color:hover {
  transform: scale(1.15);
  box-shadow: 0 2px 10px rgba(0,0,0,0.4);
  z-index: 1;
  position: relative;
}

.palette-color.selected {
  box-shadow: 0 0 0 2px white, 0 0 0 4px var(--accent-purple);
  transform: scale(1.1);
}

.palette-color-custom {
  background: conic-gradient(from 0deg, #ff0000, #ffff00, #00ff00, #00ffff, #0000ff, #ff00ff, #ff0000);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(255,255,255,0.3);
}

.add-color-btn {
  width: 100%;
  background: transparent;
  border: 1px dashed var(--border-strong);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 5px 8px;
  font-size: 10px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  justify-content: center;
  font-family: inherit;
  transition: all var(--transition);
}

.add-color-btn:hover {
  border-color: var(--accent-purple);
  color: var(--accent-purple);
  background: rgba(167,139,250,0.08);
}

/* ============================================
   アクションボタン
   ============================================ */
.canvas-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.action-btn {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-radius: var(--radius-md);
  padding: 7px 8px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  justify-content: center;
  font-family: inherit;
  transition: all var(--transition);
}

.action-btn:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.action-btn.danger:hover {
  background: rgba(248,113,113,0.15);
  border-color: var(--accent-red);
  color: var(--accent-red);
}

.action-btn.success {
  background: rgba(52,211,153,0.1);
  border-color: rgba(52,211,153,0.3);
  color: var(--accent-green);
}

.action-btn.success:hover {
  background: rgba(52,211,153,0.2);
  border-color: var(--accent-green);
}

/* ============================================
   レイヤー
   ============================================ */
.layer-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.layer-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  user-select: none;
}

.layer-item.active {
  background: rgba(167,139,250,0.15);
  border-color: var(--accent-purple);
}

.layer-item:hover:not(.active) {
  background: var(--surface-hover);
}

.layer-thumb {
  width: 28px;
  height: 20px;
  border-radius: 3px;
  background: repeating-conic-gradient(#444 0% 25%, #555 0% 50%) 0 0 / 6px 6px;
  border: 1px solid var(--border);
  flex-shrink: 0;
  overflow: hidden;
}

.layer-thumb canvas {
  width: 100%;
  height: 100%;
}

.layer-name {
  flex: 1;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layer-toggle {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px;
  display: flex;
  align-items: center;
  border-radius: 3px;
  transition: color var(--transition);
}

.layer-toggle:hover { color: var(--text-primary); }

.layer-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px;
  display: flex;
  align-items: center;
  border-radius: 3px;
  transition: color var(--transition);
  font-size: 14px;
  line-height: 1;
}

.layer-delete:hover { color: var(--accent-red); }

.mini-btn {
  background: rgba(167,139,250,0.15);
  border: 1px solid rgba(167,139,250,0.3);
  color: var(--accent-purple);
  border-radius: 4px;
  padding: 2px 5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
}

.mini-btn:hover {
  background: rgba(167,139,250,0.25);
}

/* ============================================
   メインエリア
   ============================================ */
.canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  overflow: hidden;
  position: relative;
}

/* トップバー */
.topbar {
  height: var(--topbar-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  flex-shrink: 0;
  z-index: 50;
}

.topbar-center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.canvas-info {
  font-family: 'Inter', monospace;
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.pencil-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(167,139,250,0.1);
  border: 1px solid rgba(167,139,250,0.3);
  border-radius: 20px;
  padding: 3px 10px;
  font-size: 11px;
  color: var(--accent-purple);
  font-weight: 500;
}

.pencil-indicator.hidden { display: none; }

.pressure-bar {
  width: 40px;
  height: 4px;
  background: rgba(167,139,250,0.2);
  border-radius: 2px;
  overflow: hidden;
}

.pressure-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-purple), var(--accent-pink));
  border-radius: 2px;
  width: 0%;
  transition: width 0.1s ease;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.topbar-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  transition: all var(--transition);
}

.topbar-btn:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.topbar-btn.active {
  background: rgba(167,139,250,0.15);
  border-color: var(--accent-purple);
  color: var(--accent-purple);
}

.topbar-btn.primary {
  background: linear-gradient(135deg, var(--accent-purple), var(--accent-pink));
  color: white;
  border: none;
  font-weight: 600;
}

.topbar-btn.primary:hover {
  opacity: 0.9;
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(167,139,250,0.4);
}

/* ============================================
   キャンバスコンテナ
   ============================================ */
.canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at 20% 50%, rgba(167,139,250,0.04) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 20%, rgba(244,114,182,0.03) 0%, transparent 60%),
    var(--bg-primary);
}

.canvas-wrapper {
  position: relative;
  transform-origin: center center;
  flex-shrink: 0;
  will-change: transform;
}

.main-canvas {
  display: block;
  background: white;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5), 0 2px 0 rgba(255,255,255,0.03);
  cursor: crosshair;
  touch-action: none;
}

.overlay-canvas {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 10;
  touch-action: none;
}

/* カスタムカーソル非表示（Canvasで描画） */
.canvas-container.drawing .main-canvas {
  cursor: none;
}

/* グリッドオーバーレイ */
.grid-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 5;
  background-image:
    linear-gradient(rgba(167,139,250,0.08) 1px, transparent 1px),
    linear-gradient(90deg, rgba(167,139,250,0.08) 1px, transparent 1px);
  background-size: 40px 40px;
}

.grid-overlay.hidden { display: none; }

/* ============================================
   ズームコントロール
   ============================================ */
.zoom-controls {
  position: absolute;
  bottom: 20px;
  right: 20px;
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 6px 12px;
  box-shadow: var(--shadow-md);
}

.zoom-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 600;
  font-family: inherit;
  display: flex;
  align-items: center;
  transition: all var(--transition);
}

.zoom-btn:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.zoom-label {
  font-family: 'Inter', monospace;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  min-width: 42px;
  text-align: center;
  padding: 0 4px;
}

/* ============================================
   モーダル
   ============================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.15s ease;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-xl);
  padding: 28px;
  width: min(400px, 90vw);
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.2s ease;
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 10px;
  color: var(--text-primary);
}

.modal-desc {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
  line-height: 1.6;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.modal-btn {
  padding: 9px 20px;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
  border: none;
}

.modal-btn.secondary {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.modal-btn.secondary:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.modal-btn.danger {
  background: linear-gradient(135deg, #ef4444, #dc2626);
  color: white;
}

.modal-btn.danger:hover {
  opacity: 0.9;
  box-shadow: 0 4px 16px rgba(239,68,68,0.4);
}

/* ============================================
   トースト通知
   ============================================ */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text-primary);
  padding: 11px 22px;
  border-radius: var(--radius-xl);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  z-index: 2000;
  transition: all 0.25s ease;
  opacity: 0;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.hidden {
  display: none;
}

/* ============================================
   アニメーション
   ============================================ */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(16px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ============================================
   モバイル・iPad向けレスポンシブ
   ============================================ */
.mobile-menu-btn {
  display: flex;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  transition: color var(--transition);
}
.mobile-menu-btn:hover {
  color: var(--text-primary);
}

@media (max-width: 768px) {

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform var(--transition);
    box-shadow: var(--shadow-lg);
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  .sidebar.collapsed {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    transform: translateX(-100%);
  }

  .toggle-btn {
    display: none;
  }

  .topbar {
    padding: 0 10px;
  }

  .topbar-btn span {
    display: none;
  }

  .zoom-controls {
    bottom: 10px;
    right: 10px;
  }
}

/* ============================================
   Apple Pencil 筆圧カーソル
   ============================================ */
.pencil-cursor {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  transform: translate(-50%, -50%);
  transition: width 0.05s, height 0.05s;
  border-radius: 50%;
  border: 1.5px solid rgba(255,255,255,0.6);
  mix-blend-mode: difference;
}

/* ============================================
   不透明度スライダー（チェッカー背景）
   ============================================ */
.opacity-slider {
  background:
    linear-gradient(to right, transparent, #a78bfa),
    repeating-conic-gradient(#444 0% 25%, #555 0% 50%) 0 0 / 8px 8px;
}
