/* ===== CSS変数 ===== */
:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --surface2: #f8f9fa;
  --border: #e0e4e8;
  --text: #1a2030;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-light: #eff6ff;
  --accent2: #7c3aed;
  --radius: 10px;
  --shadow: 0 2px 12px rgba(0,0,0,0.07);
  --sidebar-width: 280px;
  --header-height: 52px;
  --pattern-height: 210px;
}

/* ===== リセット ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ===== ヘッダー ===== */
header {
  height: var(--header-height);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
  flex-shrink: 0;
}
header h1 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.02em;
}
.header-actions { display: flex; gap: 6px; }

/* ===== ボタン ===== */
.btn {
  padding: 5px 14px;
  border: none;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-accent { background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #fff; }
.btn-accent:hover { opacity: 0.9; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(37,99,235,0.3); }
.btn-ppt { background: #d04a02; color: #fff; }
.btn-ppt:hover { background: #b33d00; transform: translateY(-1px); box-shadow: 0 3px 10px rgba(208,74,2,0.35); }
.btn-ppt:disabled { opacity: 0.6; transform: none; cursor: wait; }

/* ===== メインレイアウト ===== */
main {
  display: flex;
  flex: 1;
  height: calc(100vh - var(--header-height));
  overflow: hidden;
}

/* ===== 設定サイドバー ===== */
.settings-panel {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 0;
}
.settings-panel h2 {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.setting-group {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  margin-bottom: 8px;
}
.setting-group h3 {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.h3-sub {
  font-size: 0.65rem;
  font-weight: 400;
  color: var(--text-muted);
  background: var(--bg);
  padding: 1px 6px;
  border-radius: 10px;
}
.setting-group label {
  display: block;
  font-size: 0.76rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.setting-group label:last-child { margin-bottom: 0; }
.input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 3px;
}
.input-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  cursor: pointer;
}
.value-display {
  min-width: 50px;
  text-align: right;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 5px;
}
select {
  display: block;
  width: 100%;
  margin-top: 3px;
  padding: 5px 7px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  font-size: 0.8rem;
  color: var(--text);
  cursor: pointer;
}

/* ===== コマ数グリッド ===== */
.koma-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  margin-bottom: 6px;
}
.koma-btn {
  border: 1.5px solid var(--border);
  border-radius: 5px;
  background: var(--surface);
  cursor: pointer;
  padding: 4px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: all 0.15s;
}
.koma-btn:hover { border-color: var(--accent); background: var(--accent-light); }
.koma-btn.active {
  border-color: var(--accent);
  background: var(--accent-light);
  box-shadow: 0 0 0 2px rgba(37,99,235,0.2);
}
.koma-visual {
  background: rgba(37,99,235,0.12);
  border: 1px solid rgba(37,99,235,0.35);
  border-radius: 2px;
  transition: all 0.15s;
}
.koma-btn.active .koma-visual {
  background: rgba(37,99,235,0.28);
  border-color: var(--accent);
}
.koma-label {
  font-size: 0.62rem;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.2;
}
.koma-btn.active .koma-label { color: var(--accent); font-weight: 700; }
.koma-info {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--accent);
  text-align: center;
  padding: 3px;
  background: var(--accent-light);
  border-radius: 5px;
}

/* ===== デモ台数グリッド ===== */
.demo-count-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.demo-count-btn {
  width: 32px;
  height: 32px;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}
.demo-count-btn:hover { border-color: var(--accent); color: var(--accent); }
.demo-count-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: 0 2px 8px rgba(37,99,235,0.3);
}

/* ===== 情報テーブル ===== */
.info-table { font-size: 0.74rem; color: var(--text-muted); }
.info-table .row {
  display: flex;
  justify-content: space-between;
  padding: 2px 0;
  border-bottom: 1px dashed var(--border);
}
.info-table .row:last-child { border-bottom: none; }
.info-table .row span:last-child { font-weight: 700; color: var(--text); }

/* ===== メインコンテンツ ===== */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ===== パターンギャラリー ===== */
.pattern-section {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 12px;
  flex-shrink: 0;
}
.pattern-section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.pattern-section-title {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text);
}
.pattern-count {
  font-size: 0.72rem;
  color: var(--text-muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 1px 7px;
  border-radius: 10px;
}
.pattern-gallery {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}
.pattern-gallery::-webkit-scrollbar { height: 4px; }
.pattern-gallery::-webkit-scrollbar-track { background: var(--bg); border-radius: 2px; }
.pattern-gallery::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.pattern-card {
  flex-shrink: 0;
  width: 132px;
  background: var(--surface2);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  cursor: pointer;
  transition: all 0.15s;
  scroll-snap-align: start;
}
.pattern-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
.pattern-card.active {
  border-color: var(--accent);
  background: var(--accent-light);
  box-shadow: 0 0 0 2px rgba(37,99,235,0.2), 0 4px 12px rgba(37,99,235,0.15);
}
.pattern-card.random-card { border-style: dashed; }
.pattern-card.random-card.active { border-style: solid; }
.pattern-thumb {
  display: block;
  width: 100%;
  height: 76px;
  border-radius: 4px;
  background: #f0ede5;
  margin-bottom: 5px;
}
.pattern-name {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pattern-card.active .pattern-name { color: var(--accent); }
.pattern-desc {
  font-size: 0.62rem;
  color: var(--text-muted);
  margin-top: 2px;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ===== Canvasエリア ===== */
.canvas-area {
  flex: 1;
  display: flex;
  gap: 10px;
  padding: 10px;
  overflow: hidden;
  background: var(--bg);
  min-height: 0;
}
.canvas-container {
  flex: 1;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}
.canvas-header {
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-shrink: 0;
}
.canvas-title { font-size: 0.85rem; font-weight: 700; color: var(--text); }
.canvas-subtitle { font-size: 0.7rem; color: var(--text-muted); }
canvas { display: block; flex: 1; width: 100%; min-height: 0; }

/* ===== ヘッダー ユーザー情報 ===== */
.header-user {
  display: flex;
  align-items: center;
  gap: 6px;
}
.user-email {
  font-size: 0.75rem;
  color: var(--text-muted);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.btn-sm {
  padding: 3px 10px;
  font-size: 0.75rem;
}
.btn-danger {
  background: #fee2e2;
  color: #dc2626;
  border: 1px solid #fca5a5;
}
.btn-danger:hover { background: #fecaca; }

/* ===== ログイン画面オーバーレイ ===== */
#auth-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}
#auth-overlay.hidden { display: none; }

.auth-box {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  width: 380px;
  max-width: calc(100vw - 32px);
  padding: 32px 28px;
}
.auth-logo {
  font-size: 1rem;
  font-weight: 800;
  color: var(--accent);
  text-align: center;
  margin-bottom: 24px;
  letter-spacing: 0.02em;
}
.auth-tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: 20px;
}
.auth-tab {
  flex: 1;
  background: none;
  border: none;
  padding: 8px 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all 0.15s;
}
.auth-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}
.auth-label input {
  width: 100%;
  padding: 8px 10px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s;
  outline: none;
}
.auth-label input:focus { border-color: var(--accent); }
.auth-err {
  font-size: 0.78rem;
  color: #dc2626;
  min-height: 18px;
}
.auth-submit {
  width: 100%;
  padding: 10px;
  font-size: 0.9rem;
  margin-top: 4px;
}

/* ===== 汎用モーダル ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  z-index: 500;
  backdrop-filter: blur(2px);
}
.modal-overlay.hidden { display: none; }

.modal-box {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--surface);
  border-radius: 14px;
  box-shadow: 0 16px 48px rgba(0,0,0,0.25);
  width: 360px;
  max-width: calc(100vw - 32px);
  z-index: 510;
  overflow: hidden;
}
.modal-box.hidden { display: none; }
.modal-box-wide { width: 560px; }

.modal-header {
  background: var(--accent);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 700;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-close {
  background: none;
  border: none;
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  padding: 0 4px;
  opacity: 0.8;
  line-height: 1;
}
.modal-close:hover { opacity: 1; }

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: 60vh;
  overflow-y: auto;
}
.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* ===== マイレイアウト一覧 ===== */
.layouts-loading {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  padding: 16px;
}
.layouts-empty {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  padding: 24px;
}
.layout-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  gap: 12px;
}
.layout-item-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.layout-item-name {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.layout-item-date {
  font-size: 0.7rem;
  color: var(--text-muted);
}
.layout-item-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

/* ===== スマホ対応 ===== */
@media (max-width: 900px) {
  body { overflow: auto; }
  main { flex-direction: column; height: auto; overflow: visible; }
  .settings-panel {
    width: 100%; min-width: 0;
    border-right: none; border-bottom: 1px solid var(--border);
    overflow-y: visible;
  }
  .main-content { overflow: visible; }
  .canvas-area { flex-direction: column; height: auto; }
  .canvas-container { min-height: 280px; }
  canvas { min-height: 260px; }
}

/* ===== 印刷 ===== */
@media print {
  body { overflow: visible; }
  header, .settings-panel, .pattern-section { display: none; }
  main { display: block; height: auto; }
  .main-content { overflow: visible; }
  .canvas-area { display: flex; flex-wrap: wrap; gap: 8px; padding: 0; height: auto; }
  .canvas-container { flex: 1; min-width: 48%; box-shadow: none; border: 1px solid #ccc; }
  canvas { width: 100%; }
}
