/* ===== CSS Variables: Light (default) ===== */
:root {
  --bg: #f5f7fa;
  --panel: #ffffff;
  --panel2: #ffffff;
  --border: #e2e8f0;
  --fg: #1e293b;
  --fg-muted: #64748b;
  --accent: #0d9e7a;
  --accent-hover: #0b8a6a;
  --red: #e74c3c;
  --red-bg: rgba(231,76,60,0.10);
  --red-border: rgba(231,76,60,0.30);
  --err-bg: #fef2f2;
  --err-border: #fca5a5;
  --accent-muted: rgba(13,158,122,0.07);
  --accent-hover-bg: rgba(13,158,122,0.10);
  --overlay: rgba(0,0,0,0.55);
  --remove-btn-bg: rgba(0,0,0,0.50);
  --scrollbar-hover: #94a3b8;
  --canvas-bg: #eef0f4;
  --sidebar-w: 280px;
  --canvas-min: 420px;
  --chat-w: 360px;
  --header-h: 52px;
}

/* ===== CSS Variables: Dark theme ===== */
html[data-theme="dark"] {
  --bg: #0d1013;
  --panel: #161b1f;
  --panel2: #1c2227;
  --border: #242c31;
  --fg: #e6e6e6;
  --fg-muted: #8a90a0;
  --accent: #10a37f;
  --accent-hover: #0e8f6f;
  --red: #e74c3c;
  --red-bg: rgba(231,76,60,0.10);
  --red-border: rgba(231,76,60,0.35);
  --err-bg: #4a1d1d;
  --err-border: #7a2d2d;
  --accent-muted: rgba(16,163,127,0.05);
  --accent-hover-bg: rgba(16,163,127,0.10);
  --overlay: rgba(0,0,0,0.88);
  --remove-btn-bg: rgba(0,0,0,0.70);
  --scrollbar-hover: #3a4449;
  --canvas-bg: #0a0d0f;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  font: 13px/1.6 -apple-system, "Hiragino Kaku Gothic ProN", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--fg);
}

/* ===== HEADER ===== */
header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 16px;
  z-index: 100;
}

header h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  color: var(--fg);
}

header .header-sep {
  flex: 1;
}

.tone-label {
  font-size: 11px;
  color: var(--fg-muted);
  white-space: nowrap;
}

#tone-select {
  background: var(--panel2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
  min-width: 180px;
}
#tone-select:focus {
  outline: 1px solid var(--accent);
}

/* ===== THEME TOGGLE BUTTON ===== */
#theme-toggle {
  background: transparent;
  border: none;
  border-radius: 6px;
  padding: 5px 6px;
  cursor: pointer;
  color: var(--fg-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}
#theme-toggle:hover {
  background: var(--accent-muted);
  color: var(--fg);
}
#theme-toggle svg {
  width: 1em;
  height: 1em;
  vertical-align: -0.125em;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Icon visibility toggling via CSS */
#theme-toggle .icon-sun { display: block; }
#theme-toggle .icon-moon { display: none; }
html[data-theme="dark"] #theme-toggle .icon-sun { display: none; }
html[data-theme="dark"] #theme-toggle .icon-moon { display: block; }

/* ===== MAIN LAYOUT ===== */
.layout {
  position: fixed;
  top: var(--header-h);
  left: 0; right: 0; bottom: 0;
  display: flex;
  overflow: hidden;
}

/* ===== LEFT SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.tab-bar {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.tab-btn {
  flex: 1;
  padding: 10px 0;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--fg-muted);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.tab-btn:hover:not(.active) {
  color: var(--fg);
}

.tab-panel {
  display: none;
  flex: 1;
  flex-direction: column;
  overflow: hidden;
  padding: 12px;
  gap: 10px;
}
.tab-panel.active {
  display: flex;
}

.tab-panel h3 {
  margin: 0 0 4px 0;
  font-size: 11px;
  font-weight: 600;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.upload-area {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 16px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  position: relative;
}
.upload-area:hover {
  border-color: var(--accent);
  background: var(--accent-muted);
}
.upload-area input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}
.upload-area .upload-text {
  font-size: 11px;
  color: var(--fg-muted);
}

.thumb-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  overflow-y: auto;
  flex: 1;
}
.thumb-item {
  position: relative;
  aspect-ratio: 1;
  border-radius: 6px;
  overflow: hidden;
  background: var(--panel2);
  border: 1px solid var(--border);
}
.thumb-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.thumb-item .remove-btn {
  position: absolute;
  top: 2px; right: 2px;
  width: 18px; height: 18px;
  background: var(--remove-btn-bg);
  border: none;
  border-radius: 50%;
  color: #fff;
  font-size: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s;
}
.thumb-item:hover .remove-btn {
  opacity: 1;
}

.sidebar-status {
  font-size: 10px;
  color: var(--fg-muted);
  padding: 6px 0;
  min-height: 20px;
}

/* ===== CENTER CANVAS ===== */
.canvas-area {
  flex: 1;
  min-width: var(--canvas-min);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--canvas-bg);
  border-right: 1px solid var(--border);
}

.canvas-header {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  font-weight: 600;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  flex-shrink: 0;
}

#lp-canvas {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
  align-items: center;
}

#lp-canvas:empty::after {
  content: "生成された楽天LP画像がここに縦積みで表示されます";
  color: var(--fg-muted);
  font-size: 12px;
  margin: auto;
  text-align: center;
  padding: 40px;
  opacity: 0.5;
}

.lp-image-card {
  width: 100%;
  overflow: hidden;
  position: relative;
  flex-shrink: 0;
}

#lp-canvas > * {
  flex-shrink: 0;
}

.lp-image-card img {
  width: 100%;
  display: block;
  cursor: zoom-in;
  transition: opacity 0.2s;
}
.lp-image-card img:hover {
  opacity: 0.9;
}

.lp-image-card .dl-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  border: none;
  border-radius: 5px;
  font-size: 11px;
  padding: 4px 10px;
  cursor: pointer;
  transition: opacity 0.15s, background 0.15s;
  z-index: 10;
}
.lp-image-card .dl-btn:hover {
  background: rgba(0, 0, 0, 0.80);
}
@media (hover: hover) {
  .lp-image-card .dl-btn { opacity: 0; }
  .lp-image-card:hover .dl-btn { opacity: 1; }
}
@media (hover: none) {
  .lp-image-card .dl-btn { opacity: 0.6; }
}

/* ===== RIGHT CHAT ===== */
.chat-area {
  width: var(--chat-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  overflow: hidden;
}

.chat-header {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  font-weight: 600;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#chat {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#chat > * {
  flex-shrink: 0;
}

.msg {
  padding: 8px 12px;
  border-radius: 10px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 13px;
  line-height: 1.55;
}
.msg.user {
  background: var(--accent);
  align-self: flex-end;
  max-width: 88%;
  color: #fff;
}
.msg.ai {
  background: var(--panel2);
  border: 1px solid var(--border);
  align-self: flex-start;
  max-width: 95%;
  color: var(--fg);
}
.msg.tool {
  background: none;
  color: var(--fg-muted);
  font-size: 11px;
  align-self: flex-start;
  padding: 0 2px;
}
.msg.err {
  background: var(--err-bg);
  border: 1px solid var(--err-border);
  align-self: flex-start;
  max-width: 95%;
  color: var(--red);
}
.msg.info {
  background: none;
  color: var(--fg-muted);
  font-size: 11px;
  align-self: center;
  font-style: italic;
}

.spinner {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--fg-muted);
  padding: 4px;
  align-self: flex-start;
  font-size: 13px;
}
.spinner .spark {
  color: var(--accent);
  animation: spark 1.4s ease-in-out infinite;
  font-size: 15px;
  display: flex;
  align-items: center;
}
.spinner .spark svg {
  width: 1em;
  height: 1em;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.spinner .elapsed {
  color: var(--fg-muted);
  font-size: 11px;
}
@keyframes spark {
  0%   { transform: scale(1) rotate(0deg); }
  50%  { transform: scale(1.4) rotate(180deg); }
  100% { transform: scale(1) rotate(360deg); }
}

.chat-input-area {
  padding: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

#prompt {
  width: 100%;
  min-height: 72px;
  max-height: 160px;
  background: var(--panel2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  resize: vertical;
  font: inherit;
  font-size: 13px;
}
#prompt:focus {
  outline: 1px solid var(--accent);
}
#prompt::placeholder {
  color: var(--fg-muted);
}

.chat-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.send-hint {
  font-size: 10px;
  color: var(--fg-muted);
}

#send-btn {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 7px;
  padding: 7px 16px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: background 0.15s;
}
#send-btn:hover:not(:disabled) { background: var(--accent-hover); }
#send-btn:disabled { opacity: 0.4; cursor: default; }

/* ===== LIGHTBOX ===== */
.lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}
.lightbox.open {
  display: flex;
}
.lightbox img {
  max-width: 90vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 6px;
  pointer-events: none;
}

/* ===== AUTH UI ===== */
.auth-area {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.auth-email {
  font-size: 11px;
  color: var(--fg-muted);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.auth-btn {
  background: var(--panel2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.auth-btn:hover {
  border-color: var(--accent);
  background: var(--accent-hover-bg);
}
.auth-btn--logout {
  color: var(--fg-muted);
  font-size: 11px;
}

/* ===== PJT PANEL ===== */
.pjt-panel {
  position: fixed;
  top: var(--header-h);
  left: 0; right: 0;
  height: 40px;
  background: var(--panel2);
  border-bottom: 1px solid var(--border);
  z-index: 90;
  display: flex;
  align-items: center;
  padding: 0 14px;
}

.pjt-panel-inner {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

.pjt-label {
  font-size: 11px;
  color: var(--fg-muted);
  white-space: nowrap;
}

.pjt-select {
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
  cursor: pointer;
  min-width: 160px;
  max-width: 260px;
}
.pjt-select:focus {
  outline: 1px solid var(--accent);
}

.pjt-btn {
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 10px;
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.pjt-btn:hover {
  border-color: var(--fg-muted);
  background: var(--panel2);
}
.pjt-btn--primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.pjt-btn--primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.pjt-btn--danger {
  color: var(--red);
}
.pjt-btn--danger:hover {
  border-color: var(--red);
  background: var(--red-bg);
}

.pjt-status {
  font-size: 11px;
  color: var(--fg-muted);
  margin-left: 4px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Adjust layout top offset to account for PJT panel (header 52px + pjt-panel 40px = 92px) */
.layout {
  top: calc(var(--header-h) + 40px) !important;
}

/* ===== AUTH GATE MESSAGE ===== */
.auth-gate-msg {
  background: var(--red-bg);
  border: 1px solid var(--red-border);
  color: var(--red);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  text-align: center;
}

/* ===== SVG GLOBAL SAFETY (prevents runaway inline SVGs) ===== */
svg {
  max-width: 100%;
  max-height: 100%;
}

/* ===== SVG SIZE RULES: header h1 cart icon ===== */
header h1 svg,
header h1 .icon-cart {
  width: 1.2em;
  height: 1.2em;
  vertical-align: middle;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ===== SVG SIZE RULES: upload area icons ===== */
.upload-area .upload-icon {
  font-size: 24px;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--fg-muted);
}
.upload-area .upload-icon svg {
  width: 40px;
  height: 40px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ===== SVG SIZE RULES: msg icons ===== */
.msg svg {
  width: 1em;
  height: 1em;
  vertical-align: middle;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
}

/* ===== LOGIN WALL ===== */
#login-wall {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#login-wall.hidden {
  display: none;
}

.wall-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  max-width: 400px;
  width: 90%;
  box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}

.wall-logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.wall-icon-cart {
  width: 2em;
  height: 2em;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  color: var(--accent);
}

.wall-app-name {
  font-size: 18px;
  font-weight: 700;
  color: var(--fg);
}

.wall-desc {
  margin: 0;
  font-size: 13px;
  color: var(--fg-muted);
  text-align: center;
  line-height: 1.6;
}

#wall-login-btn {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 7px;
  padding: 10px 24px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s;
  white-space: nowrap;
}
#wall-login-btn:hover {
  background: var(--accent-hover);
}

#wall-logout-btn {
  background: var(--panel2);
  color: var(--fg-muted);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 20px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s, border-color 0.15s;
}
#wall-logout-btn:hover {
  border-color: var(--fg-muted);
  background: var(--accent-hover-bg);
}

/* ===== SCROLLBARS ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-hover); }

/* ===== CLEAR CHAT BUTTON ===== */
#clear-chat-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--fg-muted);
  font-size: 10px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  padding: 3px 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  flex-shrink: 0;
}
#clear-chat-btn:hover {
  background: var(--red-bg);
  border-color: var(--red-border);
  color: var(--red);
}
#clear-chat-btn svg {
  width: 12px;
  height: 12px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

/* ===== PERM MODE BADGE ===== */
#perm-mode {
  font-size: 10px;
  font-weight: 600;
  color: var(--fg-muted);
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 7px;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  white-space: nowrap;
}
#perm-mode:hover {
  background: var(--accent-hover-bg);
  border-color: var(--accent);
  color: var(--accent);
}

/* ===== LOADING OVERLAY ===== */
@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

#loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 1500;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: #fff;
  font-size: 13px;
  pointer-events: all;
}

#loading-overlay .loading-spinner {
  width: 40px;
  height: 40px;
  stroke: #fff;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  animation: spin 0.9s linear infinite;
}

html[data-theme="dark"] #loading-overlay {
  background: rgba(0, 0, 0, 0.65);
}
