*{box-sizing:border-box}
html,body{margin:0;width:100%;height:100%;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Noto Sans JP",sans-serif;background:#fff;color:#1f2328}
body{overflow:hidden}
button,input,textarea,select{font:inherit}
button{touch-action:manipulation}
.hidden{display:none!important}

:root{--sidebar-width:280px;--topbar-height:56px}

.app-shell{min-height:100dvh;background:#fff}

/* PC: 左メニューそのものは常に固定 */
.sidebar{position:fixed;inset:0 auto 0 0;width:var(--sidebar-width);height:100dvh;background:#111827;color:#fff;display:flex;flex-direction:column;padding:18px;z-index:40;overflow:hidden}
.brand-row{display:flex;align-items:center;justify-content:space-between;flex:0 0 auto}
.brand{font-weight:800;font-size:18px}
.new-chat-btn,.side-link{width:100%;border:1px solid #334155;background:#1f2937;color:#fff;border-radius:12px;padding:12px 14px;text-align:left;text-decoration:none;cursor:pointer}
.new-chat-btn{margin:18px 0 12px;flex:0 0 auto}
.chat-list{display:flex;flex-direction:column;gap:6px;min-height:0;overflow-y:auto;overscroll-behavior:contain;scrollbar-width:thin}
.chat-item{border:0;background:transparent;color:#dbe4ef;text-align:left;padding:10px 12px;border-radius:10px;cursor:pointer;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;flex:0 0 auto}
.chat-item:hover,.chat-item.active{background:#263244;color:#fff}
.side-bottom{margin-top:auto;display:grid;gap:8px;flex:0 0 auto;padding-top:12px;background:#111827}

/* 右側だけをスクロール領域にする */
.main-panel{margin-left:var(--sidebar-width);width:calc(100% - var(--sidebar-width));height:100dvh;min-width:0;display:grid;grid-template-rows:var(--topbar-height) minmax(0,1fr) auto;background:#fff;overflow:hidden}
.topbar{display:flex;align-items:center;gap:12px;padding:0 18px;border-bottom:1px solid #e5e7eb;font-weight:700;background:#fff;min-width:0;z-index:10}
.topbar>div{flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;min-width:0}
.ghost-btn,.icon-btn,.text-btn{border:0;background:transparent;cursor:pointer}
.ghost-btn{padding:8px 12px;border:1px solid #e5e7eb;border-radius:10px;background:#fff;white-space:nowrap}

.messages{min-height:0;overflow-y:auto;overscroll-behavior:contain;padding:26px max(24px,calc((100% - 900px)/2));background:#fff;scroll-behavior:smooth}
.empty-state{text-align:center;margin:14vh auto;color:#6b7280;padding:0 18px}
.empty-state h1{color:#111827;font-size:28px}
.message{display:flex;margin:18px 0;min-width:0}
.message.user{justify-content:flex-end}
.bubble{max-width:min(760px,88%);border-radius:18px;padding:14px 16px;min-width:0}
.user-bubble{background:#eef2ff}
.assistant-bubble{background:#f7f7f8;width:min(760px,92%)}
.message-source{display:block;max-width:min(220px,100%);max-height:280px;border-radius:12px;object-fit:contain;margin-bottom:10px}
.message-text{line-height:1.65;overflow-wrap:anywhere}
.job-status{font-weight:700;margin-bottom:10px}
.job-status.failed{color:#b42318}
.job-status.completed{color:#067647}
.result-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:10px}
.result-image{display:block;min-width:0}
.result-image img{display:block;width:100%;height:auto;max-height:720px;object-fit:contain;border-radius:12px;background:#eceff3}
.meta{font-size:12px;color:#6b7280;margin-top:10px}

.composer-wrap{position:relative;padding:10px max(18px,calc((100% - 900px)/2)) max(18px,env(safe-area-inset-bottom));background:linear-gradient(180deg,rgba(255,255,255,0),#fff 24%);z-index:12}
.upload-preview{display:flex;align-items:center;gap:10px;border:1px solid #e5e7eb;padding:8px;border-radius:12px;margin-bottom:8px;background:#fff}
.upload-preview img{width:58px;height:58px;border-radius:8px;object-fit:cover;flex:0 0 auto}
.upload-preview div{display:flex;align-items:center;gap:10px;min-width:0}
.upload-preview strong{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.text-btn{color:#dc2626}
.composer{display:flex;align-items:flex-end;gap:10px;border:1px solid #d1d5db;border-radius:18px;padding:10px;background:#fff;box-shadow:0 6px 26px rgba(0,0,0,.06)}
.attach-btn{width:38px;height:38px;min-width:38px;border-radius:50%;display:grid;place-items:center;background:#f3f4f6;font-size:24px;cursor:pointer}
.composer textarea{flex:1;min-width:0;resize:none;border:0;outline:0;padding:8px 4px;max-height:180px;line-height:1.5;background:transparent}
.send-btn,.primary-btn{border:0;background:#111827;color:#fff;border-radius:12px;padding:10px 16px;cursor:pointer;white-space:nowrap}
.send-btn:disabled{opacity:.5}
.shortcut-btn{border:1px solid #d1d5db;background:#fff;color:#111827;border-radius:999px;padding:8px 13px;font-size:13px;font-weight:700;cursor:pointer;white-space:nowrap;transition:background .15s ease,border-color .15s ease,transform .05s ease}
.shortcut-btn:hover{background:#f3f4f6;border-color:#9ca3af}
.shortcut-btn:active{transform:translateY(1px)}
.options-row{display:flex;align-items:center;gap:12px;flex-wrap:wrap;margin-top:8px;color:#4b5563;font-size:13px}
.options-row label{display:flex;align-items:center;gap:6px;min-width:0}
.options-row select{border:1px solid #e5e7eb;border-radius:8px;background:#fff;padding:6px 8px}

.drawer-backdrop{position:fixed;inset:0;background:rgba(15,23,42,.45);display:flex;justify-content:flex-end;z-index:80}
.drawer{width:min(680px,92vw);height:100dvh;background:#fff;padding:20px;overflow:auto}
.drawer-head{display:flex;align-items:center;justify-content:space-between}
.drawer-head h2{margin:0}
.icon-btn{font-size:24px}
.library-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:12px;margin-top:20px}
.library-item{border:1px solid #e5e7eb;border-radius:12px;overflow:hidden;background:#fff}
.library-item img{display:block;width:100%;aspect-ratio:1/1;object-fit:cover}
.library-item div{display:flex;justify-content:space-between;padding:8px;font-size:12px}
.library-item small{color:#6b7280}

.auth-page{min-height:100dvh;display:grid;place-items:center;background:#f3f4f6;padding:20px}
.auth-card{width:min(420px,100%);background:#fff;padding:28px;border-radius:18px;box-shadow:0 12px 50px rgba(0,0,0,.08);display:grid;gap:16px}
.auth-card h1{margin:0}
.auth-card label{display:grid;gap:6px;font-size:14px}
.auth-card input{border:1px solid #d1d5db;border-radius:10px;padding:11px;min-width:0;width:100%}
.auth-card a{color:#374151}
.error-box{background:#fff1f2;color:#b42318;border-radius:10px;padding:10px}
.mobile-only{display:none}
.sidebar-backdrop{display:none}

/* Drag & Drop */
.drop-overlay{position:fixed;inset:0;z-index:1000;background:rgba(17,24,39,.56);backdrop-filter:blur(4px);display:grid;place-items:center;padding:24px;pointer-events:none}
.drop-overlay-card{width:min(460px,90vw);min-height:240px;border:3px dashed rgba(255,255,255,.9);border-radius:26px;background:rgba(17,24,39,.78);color:#fff;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:9px;text-align:center;box-shadow:0 24px 80px rgba(0,0,0,.28)}
.drop-overlay-icon{width:64px;height:64px;border-radius:50%;display:grid;place-items:center;background:#fff;color:#111827;font-size:38px;line-height:1;margin-bottom:4px}
.drop-overlay-card strong{font-size:22px}
.drop-overlay-card span{font-size:13px;color:#d1d5db}
body.is-dragging-file .composer{border-color:#111827;box-shadow:0 0 0 3px rgba(17,24,39,.1),0 6px 26px rgba(0,0,0,.06)}

/* 小さめPC / タブレット */
@media (max-width:1100px) and (min-width:821px){
  :root{--sidebar-width:240px}
  .sidebar{padding:14px}
  .messages{padding-left:22px;padding-right:22px}
  .composer-wrap{padding-left:22px;padding-right:22px}
}

/* スマホ・タブレット縦: サイドバーは固定ドロワー */
@media(max-width:820px){
  body{overflow:hidden}
  .app-shell{display:block;min-height:100dvh}
  .sidebar{position:fixed;left:0;top:0;bottom:0;width:min(84vw,320px);height:100dvh;z-index:90;transform:translateX(-105%);transition:transform .2s ease;box-shadow:14px 0 36px rgba(0,0,0,.18)}
  .sidebar.open{transform:translateX(0)}
  .sidebar-backdrop{position:fixed;inset:0;z-index:85;background:rgba(15,23,42,.48);display:block}
  .sidebar-backdrop.hidden{display:none!important}
  #closeSidebar{display:grid;width:42px;height:42px;min-width:42px;padding:0;place-items:center;border:1px solid #475569;border-radius:12px;background:#1f2937;color:#fff;font-size:30px;line-height:1;cursor:pointer}
  #closeSidebar:active{background:#334155}
  body.sidebar-open{overflow:hidden}
  .main-panel{margin-left:0;width:100%;height:100dvh;grid-template-rows:var(--topbar-height) minmax(0,1fr) auto}
  .mobile-only{display:inline-grid;place-items:center}
  .messages{padding:18px 12px 22px}
  .composer-wrap{padding:8px 10px max(12px,env(safe-area-inset-bottom))}
  .shortcut-btn{min-height:40px;font-size:14px;padding:8px 14px}
  .options-row{gap:8px;overflow-x:auto;flex-wrap:nowrap;padding-bottom:2px;scrollbar-width:none}
  .options-row::-webkit-scrollbar{display:none}
  .options-row label{white-space:nowrap;flex:0 0 auto}
  /* iPhone Safariは16px未満のフォーム部品をタップすると画面を自動拡大するため16px固定 */
  .options-row select{font-size:16px;line-height:1.2;min-height:40px;touch-action:manipulation}
  .library-grid{grid-template-columns:repeat(2,minmax(0,1fr))}
  .result-grid{grid-template-columns:1fr}
  .result-grid.multi{grid-template-columns:repeat(2,minmax(0,1fr));gap:8px}
  .result-grid.multi .result-image img{max-height:none}
  .bubble{max-width:94%}
  .assistant-bubble{width:94%}
  .topbar{padding:0 10px}
  .empty-state{margin:10vh auto}
  .empty-state h1{font-size:23px}
}

@media(max-width:560px){
  :root{--topbar-height:52px}
  .topbar{gap:7px}
  .ghost-btn{padding:7px 9px;font-size:13px}
  .messages{padding:12px 8px 18px}
  .message{margin:12px 0}
  .bubble{padding:11px 12px;border-radius:15px;max-width:96%}
  .assistant-bubble{width:96%}
  .message-source{max-width:180px;max-height:230px}
  .composer{gap:7px;padding:8px;border-radius:16px}
  .attach-btn{width:36px;height:36px;min-width:36px;font-size:22px}
  .send-btn{padding:9px 12px}
  .options-row{font-size:12px}
  .library-grid{grid-template-columns:repeat(2,minmax(0,1fr));gap:8px}
  .result-grid.multi{gap:6px}
  .result-grid.multi .result-image img{border-radius:9px}
  .drawer{width:100vw;padding:14px}
  .drop-overlay-card{min-height:200px}
}

/* 全データ削除 */
.danger-link{border-color:#7f1d1d;background:#2b1518;color:#fecaca}
.danger-link:hover{background:#3a171b;border-color:#991b1b;color:#fff}
.danger-link:disabled{opacity:.55;cursor:not-allowed}

/* 別カットボタンを枚数セレクトと同じ行に配置 */
.options-row > .shortcut-btn{flex:0 0 auto;min-height:36px}
@media(max-width:820px){
  .options-row > .shortcut-btn{min-height:40px;font-size:14px;padding:8px 14px}
}

/* =========================================================
   長い添付ファイル名による横はみ出し防止
   ========================================================= */
html,
body,
.app-shell,
.main-panel,
.composer-wrap {
  max-width:100%;
}

html,
body {
  overflow-x:hidden;
}

.upload-preview {
  width:100%;
  max-width:100%;
  min-width:0;
  overflow:hidden;
}

.upload-preview > div {
  flex:1 1 auto;
  width:0;
  min-width:0;
  max-width:100%;
  overflow:hidden;
}

.upload-preview strong,
#previewName {
  display:block;
  flex:1 1 auto;
  min-width:0;
  max-width:100%;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}

.upload-preview .text-btn {
  flex:0 0 auto;
  white-space:nowrap;
}

@media(max-width:820px){
  .composer-wrap,
  .upload-preview,
  .composer,
  .options-row {
    min-width:0;
    max-width:100%;
  }

  .upload-preview > div {
    max-width:calc(100% - 68px);
  }
}

/* =========================================================
   画像の縦横比を必ず維持（引き伸ばし防止）
   ========================================================= */
img {
  max-width:100%;
  height:auto;
}

/* 添付プレビュー：固定枠内でも画像自体は変形させない */
.upload-preview img,
#previewImage {
  width:58px;
  height:58px;
  max-width:58px;
  max-height:58px;
  object-fit:contain;
  object-position:center;
  aspect-ratio:auto;
  background:#f3f4f6;
}

/* チャットに表示する元画像 */
.message-source {
  width:auto;
  height:auto;
  max-width:min(220px,100%);
  max-height:280px;
  object-fit:contain;
  object-position:center;
  aspect-ratio:auto;
}

/* 生成画像：元ファイルの縦横比をそのまま表示 */
.result-image {
  display:block;
  min-width:0;
  overflow:hidden;
}

.result-image img,
.result-grid img,
.result-grid.multi img {
  display:block;
  width:100%;
  height:auto !important;
  max-width:100%;
  object-fit:contain !important;
  object-position:center;
  aspect-ratio:auto !important;
}

/* ライブラリも正方形へ引き伸ばさず、実画像比率で表示 */
.library-item img,
.library-grid img {
  display:block;
  width:100%;
  height:auto !important;
  max-width:100%;
  aspect-ratio:auto !important;
  object-fit:contain !important;
  object-position:center;
  background:#f3f4f6;
}

@media(max-width:820px){
  .upload-preview img,
  #previewImage {
    width:58px;
    height:58px;
    flex:0 0 58px;
  }

  .message-source {
    width:auto;
    height:auto;
    max-width:min(180px,100%);
    max-height:230px;
  }

  .result-image img,
  .result-grid img,
  .result-grid.multi img,
  .library-item img,
  .library-grid img {
    width:100%;
    height:auto !important;
    aspect-ratio:auto !important;
    object-fit:contain !important;
  }
}
