/* AI V2 embedded in app.html (#view-ai) — standalone styles remain in ai-v2.html */

.app-main > #view-ai.active {
  display: flex !important;
  flex-direction: column;
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 0 12px max(12px, env(safe-area-inset-bottom));
  box-sizing: border-box;
}

html.ai-v2-scroll-lock,
html.ai-v2-scroll-lock body.app-body {
  overflow: hidden !important;
}

.ai-v2-page {
  max-width: 920px;
  width: 100%;
  margin: 0 auto;
  padding: 12px 4px 24px;
  box-sizing: border-box;
  flex: 1 1 auto;
}

.ai-v2-hero { margin-bottom: 16px; }
.ai-v2-hero h1 { font-size: 1.45rem; margin: 0 0 6px; }
.ai-v2-hero p { color: var(--muted); font-size: 14px; margin: 0; line-height: 1.5; }
.ai-v2-tabs { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.ai-v2-tab { padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: transparent; color: var(--text); cursor: pointer; font-size: 13px; }
.ai-v2-tab.active { background: rgba(201,168,76,.15); border-color: var(--gold); color: var(--gold2); }
.ai-v2-panel { display: none; }
.ai-v2-panel.active { display: block; }
.ai-v2-chat-layout { display: block; }
.ai-v2-chat-scroll {
  max-height: min(42vh, 400px);
  overflow-y: auto;
  overflow-x: hidden;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
  background: var(--card);
  -webkit-overflow-scrolling: touch;
}
.ai-v2-msg { margin-bottom: 12px; font-size: 14px; }
.ai-v2-msg.is-user .ai-v2-msg-text { background: rgba(59,130,246,.12); padding: 10px 12px; border-radius: 10px; }
.ai-v2-msg.is-assistant .ai-v2-msg-text { padding: 4px 0; color: var(--text); }
.ai-v2-msg-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.ai-v2-msg.is-err .ai-v2-msg-text { color: var(--red); }
.ai-v2-msg-role { font-size: 11px; color: var(--muted); margin-bottom: 4px; text-transform: uppercase; letter-spacing: .04em; }
.ai-v2-composer { display: flex; flex-direction: column; gap: 8px; }
.ai-v2-draft-card {
  margin-top: 14px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  scroll-margin-bottom: max(32px, env(safe-area-inset-bottom));
}
.ai-v2-draft-card h3 { margin: 0 0 8px; font-size: 15px; }
.ai-v2-meta { font-size: 13px; color: var(--muted); }
.ai-v2-items { margin: 0; padding-left: 18px; font-size: 13px; }
.ai-v2-items-scroll {
  max-height: min(220px, 35vh);
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  margin: 8px 0 12px;
}
.ai-v2-actions { display: flex; gap: 8px; flex-wrap: wrap; padding-bottom: 4px; }
.ai-v2-history-wrap {
  max-height: min(58vh, 560px);
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px 12px;
  background: var(--card);
}
.ai-v2-hist-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.ai-v2-hist-row:last-child { border-bottom: 0; }
.ai-v2-hist-actions { display: flex; gap: 6px; flex-shrink: 0; flex-wrap: wrap; }
.ai-v2-empty { color: var(--muted); font-size: 14px; padding: 16px 0; }
.ai-v2-toast { margin-top: 8px; font-size: 13px; padding: 10px 12px; border-radius: 8px; background: rgba(201,168,76,.1); border: 1px solid rgba(201,168,76,.2); }
.ai-v2-toast.is-err { background: rgba(239,68,68,.12); color: var(--red); border-color: rgba(239,68,68,.25); }
.ai-v2-toast.is-ok { background: rgba(34,197,94,.12); color: var(--green); border-color: rgba(34,197,94,.25); }
.ai-v2-toast.is-info { background: rgba(59,130,246,.1); color: var(--text); border-color: rgba(59,130,246,.2); }
.ai-v2-toast[hidden] { display: none; }
#ai-v2-pf-overlay { position: fixed; inset: 0; z-index: 9000; background: rgba(0,0,0,.65); display: none; align-items: center; justify-content: center; padding: 12px; }
#ai-v2-pf-overlay.open { display: flex; }
body.ai-v2-preview-open #ai-v2-pf-overlay.open { z-index: 100001; }
.ai-v2-pf-box { background: var(--card); border-radius: 12px; max-width: min(920px,100%); width: 100%; max-height: min(92vh,920px); min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.ai-v2-pf-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--border); flex-shrink: 0; background: var(--card); position: relative; z-index: 2; }
.ai-v2-pf-head-actions { display: flex; gap: 8px; flex-shrink: 0; flex-wrap: wrap; }
.ai-v2-pf-body { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 8px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; background: #e8e8ec; }
.ai-v2-pf-pdf-view { display: flex; flex-direction: column; align-items: center; gap: 12px; width: 100%; padding: 4px 0 16px; box-sizing: border-box; }
.ai-v2-pf-pdf-page { width: 100%; max-width: 100%; margin: 0 auto; background: #fff; border-radius: 6px; overflow: hidden; box-shadow: 0 2px 12px rgba(0,0,0,.12); line-height: 0; }
.ai-v2-pf-pdf-page-canvas { display: block; max-width: 100%; height: auto; vertical-align: top; }
.ai-v2-pf-pdf-loading { padding: 24px; text-align: center; color: var(--muted); }
.ai-v2-pf-pdf-fallback-msg { padding: 24px 20px; text-align: center; color: var(--text); font-size: 14px; line-height: 1.5; max-width: 360px; margin: 24px auto; }
.ai-v2-pf-pdf-view--embed { min-height: 0; }
.ai-v2-pf-frame { width: 100%; min-height: 70vh; border: 0; border-radius: 6px; background: #fff; display: block; box-shadow: 0 2px 12px rgba(0,0,0,.12); }
.ai-v2-badge { font-size: 11px; color: var(--gold2); border: 1px solid rgba(201,168,76,.3); padding: 4px 8px; border-radius: 6px; white-space: nowrap; }
#ai-v2-modal { position: fixed; inset: 0; z-index: 9100; background: rgba(0,0,0,.55); display: none; align-items: center; justify-content: center; padding: 20px; }
#ai-v2-modal.open { display: flex; }
.ai-v2-modal-box { background: var(--card, #101730); border: 1px solid var(--border); border-radius: 12px; max-width: 400px; width: 100%; padding: 20px; }
.ai-v2-modal-box.is-wide { max-width: 480px; }
.ai-v2-modal-box.is-choice-layout {
  max-height: min(90vh, 720px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-bottom: 16px;
}
.ai-v2-modal-head { flex-shrink: 0; }
.ai-v2-modal-box h4 { margin: 0 0 8px; font-size: 16px; }
.ai-v2-modal-box p { margin: 0 0 16px; font-size: 14px; color: var(--muted); line-height: 1.45; }
.ai-v2-modal-box.is-choice-layout p { margin-bottom: 12px; }
.ai-v2-modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; flex-shrink: 0; }
.ai-v2-modal-actions[hidden] { display: none; }
.ai-v2-modal-actions.is-stack { flex-direction: column; align-items: stretch; }
.ai-v2-modal-actions.is-stack .btn { width: 100%; text-align: center; white-space: normal; line-height: 1.35; padding-top: 10px; padding-bottom: 10px; }
.ai-v2-modal-choice-panel { display: flex; flex-direction: column; flex: 1; min-height: 0; gap: 0; }
.ai-v2-modal-choice-panel[hidden] { display: none; }
.ai-v2-modal-choices {
  flex: 1; min-height: 0; max-height: min(55vh, 420px); overflow-y: auto;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  display: flex; flex-direction: column; gap: 8px; padding-right: 4px; margin-right: -4px;
}
.ai-v2-modal-choices .btn {
  width: 100%; text-align: center; white-space: normal; line-height: 1.35;
  padding-top: 10px; padding-bottom: 10px; flex-shrink: 0;
}
.ai-v2-modal-choice-foot { flex-shrink: 0; padding-top: 12px; margin-top: 8px; border-top: 1px solid var(--border); }
.ai-v2-modal-choice-foot .btn { width: 100%; text-align: center; }
#ai-v2-editor { position: fixed; inset: 0; z-index: 9200; background: rgba(0,0,0,.6); display: none; align-items: center; justify-content: center; padding: 16px; }
#ai-v2-editor.open { display: flex; }
.ai-v2-ed-box { background: var(--card, #101730); border: 1px solid var(--border); border-radius: 12px; max-width: 640px; width: 100%; max-height: min(92vh, 860px); display: flex; flex-direction: column; overflow: hidden; }
.ai-v2-ed-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.ai-v2-ed-head h4 { margin: 0; font-size: 16px; }
.ai-v2-ed-body { flex: 1; overflow-y: auto; padding: 14px 16px; -webkit-overflow-scrolling: touch; }
.ai-v2-ed-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 16px; border-top: 1px solid var(--border); flex-shrink: 0; }
.ai-v2-ed-section { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px 12px; margin: 0 0 14px; }
.ai-v2-ed-section legend { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--gold2); padding: 0 6px; }
.ai-v2-ed-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.ai-v2-ed-field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.ai-v2-ed-field.is-wide { grid-column: 1 / -1; }
.ai-v2-ed-field .fi { width: 100%; box-sizing: border-box; }
.ai-v2-ed-item { display: grid; grid-template-columns: 1fr 64px 96px 34px; gap: 6px; margin-bottom: 8px; align-items: center; }
.ai-v2-ed-assets-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.ai-v2-ed-asset { display: flex; flex-direction: column; gap: 6px; }
.ai-v2-ed-asset-label { font-size: 12px; color: var(--muted); }
.ai-v2-ed-asset-preview { border: 1px dashed var(--border); border-radius: 8px; min-height: 64px; display: flex; align-items: center; justify-content: center; padding: 6px; background: rgba(255,255,255,.02); }
.ai-v2-ed-asset-preview img { max-width: 100%; max-height: 72px; object-fit: contain; }
.ai-v2-ed-asset-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.ai-v2-ed-item-remove { color: var(--red); flex-shrink: 0; }
.ai-v2-status { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); text-transform: uppercase; letter-spacing: .04em; vertical-align: middle; }
.ai-v2-status.is-draft { color: var(--muted); }
.ai-v2-status.is-unpaid { color: #f59e0b; border-color: rgba(245,158,11,.4); background: rgba(245,158,11,.1); }
.ai-v2-status.is-paid { color: var(--green); border-color: rgba(34,197,94,.4); background: rgba(34,197,94,.1); }
.ai-v2-hist-status { font-size: 12px; padding: 4px 6px; border-radius: 6px; background: transparent; color: var(--text); border: 1px solid var(--border); }
.ai-v2-hist-controls { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.ai-v2-hist-controls input[type="search"] { flex: 1; min-width: 0; }
.ai-v2-hist-controls select { flex-shrink: 0; width: auto; }

@media (max-width: 640px) {
  .app-main > #view-ai.active { padding: 0 8px max(8px, env(safe-area-inset-bottom)); }
  #ai-v2-prompt { font-size: 16px; }
}

@media (max-width: 768px) {
  body.ai-v2-preview-open .app-header {
    display: none !important;
  }
  body.ai-v2-preview-open #ai-v2-pf-overlay.open {
    z-index: 100001 !important;
    padding: 0;
    align-items: stretch;
  }
  body.ai-v2-preview-open .ai-v2-pf-box {
    max-width: 100%;
    max-height: 100dvh;
    height: 100dvh;
    border-radius: 0;
  }
  body.ai-v2-preview-open .ai-v2-pf-head {
    position: sticky;
    top: 0;
    padding-top: max(12px, env(safe-area-inset-top, 0px));
  }
  .ai-v2-pf-head { flex-direction: column; align-items: flex-start; }
  .ai-v2-pf-head-actions { width: 100%; justify-content: flex-end; }
  .ai-v2-pf-frame { min-height: 60vh; }
  #ai-v2-modal { padding: 12px; }
  .ai-v2-modal-box.is-choice-layout { max-height: min(92vh, 720px); }
  .ai-v2-modal-choices { max-height: min(50vh, 360px); }
}

@media (max-width: 600px) {
  .ai-v2-page { padding: 8px 0 20px; }
  .ai-v2-chat-scroll { max-height: min(38vh, 320px); }
  .ai-v2-history-wrap { max-height: min(52vh, 480px); }
  .ai-v2-hist-row { flex-direction: column; align-items: stretch; gap: 10px; }
  .ai-v2-hist-actions { width: 100%; flex-wrap: wrap; justify-content: flex-start; gap: 6px; }
  .ai-v2-hist-actions .btn-sm { flex: 1 1 calc(50% - 6px); min-width: 0; text-align: center; }
  .ai-v2-hist-status { width: 100%; max-width: 100%; box-sizing: border-box; }
}

@media (max-width: 540px) {
  .ai-v2-ed-assets-grid { grid-template-columns: 1fr; }
  .ai-v2-ed-grid { grid-template-columns: 1fr; }
  .ai-v2-ed-item { grid-template-columns: 1fr 52px 80px 30px; }
}
