:root {
  --bws-ai-safe-bottom: max(12px, env(safe-area-inset-bottom));
}

body.bws-ai-open { overflow: hidden !important; }

.bws-ai-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147482500;
  display: grid;
  place-items: center;
  padding: max(12px, env(safe-area-inset-top)) 12px var(--bws-ai-safe-bottom);
  background: rgba(2, 12, 27, .64);
  backdrop-filter: blur(7px);
}
.bws-ai-overlay[hidden] { display: none !important; }

.bws-ai-workspace {
  width: min(1180px, 100%);
  height: min(820px, calc(100dvh - max(24px, env(safe-area-inset-top)) - var(--bws-ai-safe-bottom)));
  min-height: 420px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  color: #0f172a;
  background: #f8fafc;
  border: 1px solid rgba(148, 163, 184, .35);
  border-radius: 22px;
  box-shadow: 0 32px 100px rgba(2, 12, 27, .45);
  font-family: Inter, system-ui, sans-serif;
}

.bws-ai-head {
  min-height: 68px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: #fff;
  background: linear-gradient(135deg, #082f49, #0f766e);
}
.bws-ai-head h2 { margin: 0; font-size: clamp(17px, 2vw, 22px); }
.bws-ai-mark {
  width: 42px; height: 42px; display: grid; place-items: center;
  border-radius: 13px; background: rgba(255,255,255,.15); border: 1px solid rgba(255,255,255,.28);
  font-weight: 800; letter-spacing: .04em;
}
.bws-ai-head button {
  width: 42px; height: 42px; border: 0; border-radius: 12px; color: #fff;
  background: rgba(255,255,255,.12); font-size: 28px; line-height: 1; cursor: pointer;
}

.bws-ai-main { min-height: 0; overflow: auto; padding: 16px; }
.bws-ai-status {
  margin: 0 0 12px; padding: 10px 12px; border-radius: 11px;
  color: #0f172a; background: #e2e8f0; font-size: 13px;
}
.bws-ai-status[data-tone="success"] { color: #065f46; background: #d1fae5; }
.bws-ai-status[data-tone="warning"] { color: #92400e; background: #fef3c7; }
.bws-ai-status[data-tone="error"] { color: #991b1b; background: #fee2e2; }
.bws-ai-status[data-tone="loading"] { color: #075985; background: #e0f2fe; }

.bws-ai-result { min-height: 100%; }
.bws-ai-empty { min-height: 320px; display: grid; place-items: center; }
.bws-ai-empty span {
  width: 90px; height: 90px; display: grid; place-items: center; border-radius: 28px;
  color: #0f766e; background: #ccfbf1; border: 1px solid #99f6e4; font-size: 30px; font-weight: 850;
}
.bws-ai-result-head {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 12px;
}
.bws-ai-result-head h3 { margin: 0; color: #0f172a; font-size: clamp(17px, 2.2vw, 23px); }
.bws-ai-result-head p { margin: 4px 0 0; color: #64748b; font-size: 13px; }
.bws-ai-result-content { display: grid; gap: 14px; }

.bws-ai-form {
  display: grid; grid-template-columns: minmax(0, 1fr) 48px; gap: 10px;
  padding: 12px 16px max(12px, env(safe-area-inset-bottom));
  background: #fff; border-top: 1px solid #e2e8f0;
}
.bws-ai-form textarea {
  min-height: 48px; max-height: 130px; resize: vertical; padding: 12px 14px;
  border: 1px solid #cbd5e1; border-radius: 14px; outline: none; color: #0f172a; background: #fff;
  font: inherit;
}
.bws-ai-form textarea:focus { border-color: #0f766e; box-shadow: 0 0 0 3px rgba(15,118,110,.13); }
.bws-ai-form button {
  width: 48px; min-height: 48px; border: 0; border-radius: 14px; color: #fff;
  background: #0f766e; cursor: pointer; font-size: 19px;
}
.bws-ai-form button:disabled { opacity: .55; cursor: wait; }

.bws-ai-metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 9px; }
.bws-ai-metrics article {
  min-width: 0; padding: 11px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff;
}
.bws-ai-metrics span { display: block; overflow: hidden; color: #64748b; font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.bws-ai-metrics b { display: block; margin-top: 4px; color: #0f172a; font-size: 15px; }
.bws-ai-chart-wrap { height: clamp(290px, 44dvh, 470px); padding: 10px; border: 1px solid #e2e8f0; border-radius: 15px; background: #fff; }
.bws-ai-chart-wrap canvas { width: 100% !important; height: 100% !important; }

.bws-ai-result-actions { display: flex; justify-content: flex-end; gap: 8px; }
.bws-ai-result-actions button,
.bws-ai-feature-launch button,
.bws-ai-coordinate button {
  padding: 9px 15px; border: 1px solid #0f766e; border-radius: 10px; color: #fff; background: #0f766e; cursor: pointer; font-weight: 700;
}

.bws-ai-table-wrap { max-width: 100%; max-height: 360px; overflow: auto; border: 1px solid #e2e8f0; border-radius: 13px; background: #fff; }
.bws-ai-table-wrap table,
.bws-ai-cloned-result table { width: 100%; border-collapse: collapse; font-size: 12px; }
.bws-ai-table-wrap th,
.bws-ai-table-wrap td,
.bws-ai-cloned-result th,
.bws-ai-cloned-result td { padding: 8px 10px; border-bottom: 1px solid #e2e8f0; text-align: left; white-space: nowrap; }
.bws-ai-table-wrap th,
.bws-ai-cloned-result th { position: sticky; top: 0; z-index: 1; color: #334155; background: #f1f5f9; }

.bws-ai-search-grid,
.bws-ai-transfer-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.bws-ai-search-grid article,
.bws-ai-transfer-grid button {
  display: grid; gap: 5px; padding: 13px; text-align: left; border: 1px solid #e2e8f0; border-radius: 13px; background: #fff;
}
.bws-ai-search-grid span,
.bws-ai-transfer-grid span { color: #64748b; font-size: 12px; }
.bws-ai-search-grid small { color: #475569; }
.bws-ai-transfer-grid button { cursor: pointer; color: #0f172a; }

.bws-ai-upload {
  min-height: 180px; display: grid; place-items: center; gap: 10px; padding: 20px;
  border: 2px dashed #94a3b8; border-radius: 16px; background: #fff; cursor: pointer;
}
.bws-ai-upload span { font-weight: 800; color: #0f766e; }
.bws-ai-inline-status { color: #475569; font-size: 13px; }
.bws-ai-coordinate { display: grid; grid-template-columns: 1fr 1fr auto; align-items: end; gap: 10px; padding: 15px; border-radius: 15px; background: #fff; }
.bws-ai-coordinate label { display: grid; gap: 5px; color: #475569; font-size: 12px; }
.bws-ai-coordinate input { min-width: 0; padding: 10px; border: 1px solid #cbd5e1; border-radius: 9px; font: inherit; }
#bwsAiCoordinateStatus { grid-column: 1 / -1; color: #0f766e; font-size: 13px; }

.bws-ai-feature-launch { min-height: 240px; display: grid; place-items: center; align-content: center; gap: 14px; border: 1px solid #e2e8f0; border-radius: 16px; background: #fff; }
.bws-ai-cloned-result { display: grid; gap: 12px; overflow: auto; }
.bws-ai-cloned-result > * { max-width: 100%; overflow: auto; padding: 12px; border: 1px solid #e2e8f0; border-radius: 13px; background: #fff; }
.bws-ai-cloned-result [hidden] { display: none !important; }
.bws-ai-error { padding: 14px; border-radius: 12px; color: #991b1b; background: #fee2e2; }

.bws-ai-dem { margin: 0; display: grid; gap: 10px; }
.bws-ai-dem img { width: 100%; max-height: 560px; object-fit: contain; border-radius: 15px; background: #082f49; }
.bws-ai-dem figcaption { display: grid; grid-template-columns: auto 1fr auto 1fr; gap: 8px 12px; padding: 11px; border-radius: 12px; background: #fff; font-size: 12px; }
.bws-ai-dem figcaption span { color: #64748b; }

.bws-ai-menu-symbol { font-size: 11px !important; font-weight: 900; letter-spacing: -.02em; }

/* Panel fitur lama boleh bekerja sebagai mesin di belakang, tetapi tidak tampil saat AI memproses. */
body.bws-ai-background-run #spiPanel,
body.bws-ai-background-run #dependableFlowPanel,
body.bws-ai-background-run #waterBalanceEnginePanel,
body.bws-ai-background-run #demAnalysisPanel,
body.bws-ai-background-run #floodScenarioPanel { visibility: hidden !important; pointer-events: none !important; }

@media (max-width: 800px) {
  .bws-ai-overlay { padding: 0; }
  .bws-ai-workspace { width: 100%; height: 100dvh; min-height: 0; border-radius: 0; }
  .bws-ai-main { padding: 12px; }
  .bws-ai-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .bws-ai-search-grid, .bws-ai-transfer-grid { grid-template-columns: 1fr; }
  .bws-ai-coordinate { grid-template-columns: 1fr; }
  .bws-ai-chart-wrap { height: 360px; }
}

@media (max-width: 460px) {
  .bws-ai-head { min-height: 60px; padding: 9px 11px; }
  .bws-ai-mark { width: 38px; height: 38px; border-radius: 11px; }
  .bws-ai-form { padding: 9px max(9px, env(safe-area-inset-right)) max(9px, env(safe-area-inset-bottom)) max(9px, env(safe-area-inset-left)); }
  .bws-ai-chart-wrap { height: 315px; }
  .bws-ai-dem figcaption { grid-template-columns: auto 1fr; }
}
