/* DBVERSE mobile — v5 (ER 2D/3D + 入力クリアボタン対応) */
*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg: #0d1117;
  --bg2: #161b22;
  --bg3: #21262d;
  --fg: #e6edf3;
  --fg-dim: #8b949e;
  --accent: #4a8cff;
  --border: #30363d;
  --danger: #e5534b;
  --radius: 10px;
}

html, body {
  margin: 0; padding: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}
body { display: flex; flex-direction: column; }

/* ─── ヘッダ ──────────────────────── */
#hdr {
  display: flex; align-items: center;
  padding: 0 6px;
  padding-top: env(safe-area-inset-top);
  height: calc(44px + env(safe-area-inset-top));
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}
#hdr button {
  width: 40px; height: 40px;
  padding: 0;
  background: transparent; border: none;
  color: var(--fg); font-size: 24px;
  cursor: pointer; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
}
#hdr button:disabled { opacity: .15; }
#hdr button:active:not(:disabled) { background: var(--bg3); }
#hdr h1 {
  margin: 0; font-size: 15px; font-weight: 600;
  letter-spacing: .03em; flex: 1; text-align: center;
}
#hdr-sub {
  font-size: 11px; color: var(--fg-dim);
  max-width: 35%; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
  padding-right: 8px;
}

/* ─── メイン ──────────────────────── */
#main { flex: 1; position: relative; overflow: hidden; }
.view {
  position: absolute; inset: 0;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 12px;
  display: none;
  padding-bottom: calc(70px + env(safe-area-inset-bottom));
}
.view.active { display: block; }

/* ─── メニュー ───────────────────── */
.menu-item {
  display: flex; align-items: center; gap: 14px;
  width: 100%; padding: 16px 14px;
  margin-bottom: 10px;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--fg);
  font-size: 16px; text-align: left; cursor: pointer;
  font-family: inherit;
  transition: background .12s;
}
.menu-item:active { background: var(--bg3); }
.menu-item .ico { font-size: 22px; line-height: 1; }
.menu-item .label { flex: 1; font-weight: 500; }
.menu-item .cnt {
  color: var(--fg-dim); font-size: 12px;
  background: var(--bg3); padding: 2px 8px; border-radius: 10px;
}
.db-info {
  margin-top: 20px;
  color: var(--fg-dim); font-size: 12px; text-align: center;
}

/* ─── 入力欄 ─────────────────────── */
#table-filter, #data-filter, #ask-input {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
  font-size: 16px;
  font-family: inherit;
  margin-bottom: 10px;
  transition: border-color .12s;
}
#table-filter:focus, #data-filter:focus, #ask-input:focus {
  outline: none; border-color: var(--accent);
}
#ask-input {
  resize: none;
  line-height: 1.4;
  min-height: 70px;
  max-height: 160px;
}

/* ─── 質問入力欄ラッパー + クリアボタン ─── */
#ask-input-wrap {
  position: relative;
  display: block;
  margin-bottom: 10px;
}
#ask-input-wrap #ask-input {
  margin-bottom: 0;
  padding-right: 46px;
}
#ask-input-clear {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 30px;
  height: 30px;
  padding: 0;
  margin: 0;
  background: rgba(255, 255, 255, .15);
  border: 1px solid rgba(255, 255, 255, .2);
  border-radius: 50%;
  color: #e6edf3;
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  font-family: inherit;
  display: flex;              /* ← 常時表示 */
  align-items: center;
  justify-content: center;
  z-index: 2;
}
#ask-input-clear:active {
  background: rgba(255, 255, 255, .3);
}

/* ─── テーブル一覧 ────────────────── */
.titem {
  display: flex; align-items: center;
  padding: 14px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background .1s;
}
.titem:active { background: var(--bg2); }
.titem .name {
  flex: 1;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 14px;
  word-break: break-all;
}
.titem .rows {
  color: var(--fg-dim); font-size: 11px; margin-left: 8px;
  background: var(--bg2); padding: 2px 8px; border-radius: 10px;
  flex-shrink: 0;
}

/* ─── データ閲覧 ──────────────────── */
#data-head { display: flex; gap: 8px; margin-bottom: 8px; }
#data-head input { flex: 1; margin: 0; }
#data-reload {
  width: 46px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg); font-size: 20px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
#data-reload:active { background: var(--bg3); }
#data-meta {
  font-size: 12px; color: var(--fg-dim);
  margin-bottom: 8px; padding: 0 2px;
}
#data-body {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg2);
  max-height: calc(100dvh - 290px);
  min-height: 200px;
}
#data-body table {
  border-collapse: separate;
  border-spacing: 0;
  font-size: 12px;
  font-family: ui-monospace, Menlo, monospace;
  min-width: max-content;
}
#data-body th, #data-body td {
  padding: 8px 10px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  text-align: left;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
}
#data-body th {
  background: var(--bg3);
  position: sticky;
  top: 0;
  font-weight: 600;
  z-index: 2;
}
#data-body th:first-child,
#data-body td:first-child {
  position: sticky;
  left: 0;
  background: var(--bg2);
  z-index: 1;
  box-shadow: 2px 0 0 var(--border);
}
#data-body th:first-child {
  background: var(--bg3);
  z-index: 3;
}
#data-body tr:last-child td { border-bottom: none; }
#data-body td.null { color: var(--fg-dim); font-style: italic; }

#data-foot {
  display: flex; align-items: center; gap: 8px;
  margin-top: 12px;
}
#data-foot button {
  width: 42px; height: 42px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg); font-size: 18px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
#data-foot button:active:not(:disabled) { background: var(--bg3); }
#data-foot button:disabled { opacity: .3; }
#pg-info {
  flex: 1; text-align: center;
  font-size: 12px; color: var(--fg-dim);
}

/* ─── 質問 ───────────────────────── */
#ask-run {
  width: 100%;
  padding: 14px;
  background: var(--accent); color: #fff;
  border: none; border-radius: var(--radius);
  font-size: 15px; font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  margin-bottom: 14px;
  transition: filter .12s;
}
#ask-run:active:not(:disabled) { filter: brightness(.9); }
#ask-run:disabled { opacity: .5; }

.ask-item {
  margin-bottom: 12px;
  padding: 12px 14px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.ask-q {
  color: var(--accent);
  font-size: 13px; font-weight: 600;
  margin-bottom: 10px;
  line-height: 1.4;
  word-break: break-word;
}
.ask-status { color: var(--fg-dim); font-size: 12px; }
.ask-sql {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px; color: var(--fg-dim);
  white-space: pre-wrap; word-break: break-all;
  margin: 8px 0; padding: 10px;
  background: #0a0d11;
  border-radius: 6px;
  border: 1px solid var(--border);
  line-height: 1.45;
}
.ask-err {
  color: var(--danger);
  font-size: 12px;
  line-height: 1.45;
  word-break: break-word;
}
.ask-result { overflow-x: auto; margin-top: 10px; }
.ask-result table {
  border-collapse: collapse; font-size: 12px;
  font-family: ui-monospace, Menlo, monospace;
  min-width: max-content;
}
.ask-result th, .ask-result td {
  padding: 8px 10px;
  border: 1px solid var(--border);
  white-space: nowrap;
}
.ask-result th { background: var(--bg3); }
.ask-time {
  color: var(--fg-dim);
  font-size: 11px;
  margin-top: 8px;
}

/* ─── トースト ────────────────────── */
#toast {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(72px + env(safe-area-inset-bottom));
  padding: 12px 14px;
  background: rgba(30, 36, 44, .98);
  color: #fff;
  border-radius: var(--radius);
  font-size: 13px; text-align: center;
  pointer-events: none;
  opacity: 0;
  transform: translateY(16px);
  transition: opacity .2s, transform .2s;
  box-shadow: 0 4px 12px rgba(0,0,0,.5);
  z-index: 9999;
}
#toast.show { opacity: 1; transform: translateY(0); }

/* ─── PC版へ ──────────────────────── */
#btn-to-pc {
  position: fixed;
  bottom: calc(14px + env(safe-area-inset-bottom));
  right: 14px;
  font-size: 12px;
  color: #fff;
  background: rgba(74, 140, 255, .92);
  padding: 8px 12px;
  border-radius: 18px;
  text-decoration: none;
  box-shadow: 0 2px 8px rgba(0,0,0,.4);
  font-weight: 600;
  z-index: 100;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  white-space: nowrap;
  transition: opacity .12s;
}
#btn-to-pc:active { opacity: .75; }

/* ─── 小型スマホ ──────────────────── */
@media (max-width: 400px) {
  #hdr { height: calc(42px + env(safe-area-inset-top)); }
  #hdr h1 { font-size: 14px; }
  .view { padding: 10px; padding-bottom: calc(66px + env(safe-area-inset-bottom)); }
  .menu-item { padding: 14px 12px; font-size: 15px; }
  .menu-item .ico { font-size: 20px; }
  #ask-input { min-height: 60px; }
  #btn-to-pc { font-size: 11px; padding: 6px 10px; }
}

/* ═══ ERツールバー ═══════════════════════ */
#er-toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
#er-toolbar button {
  padding: 8px 12px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
#er-toolbar button:active { background: var(--bg3); }

/* 2D / 3D 切替 */
#er-view-toggle {
  display: flex;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
#er-view-toggle button {
  padding: 8px 14px;
  background: transparent;
  border: none;
  border-radius: 0;
  color: var(--fg-dim);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
#er-view-toggle button.active {
  background: var(--accent);
  color: #fff;
}
#er-view-toggle button:active:not(.active) {
  background: var(--bg3);
}

/* ═══ 2D ER ビューポート ═════════════════ */
#er-viewport {
  position: relative;
  width: 100%;
  height: calc(100dvh - 190px);
  background: #0a0d11;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  touch-action: none;
}
#er-viewport-inner {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  transform-origin: 0 0;
  will-change: transform;
}
#er-svg {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#er2d-cards {
  position: absolute;
  top: 0; left: 0;
  width: 0;
  height: 0;
}

/* ─── カード ─────────────────────── */
.er2d-card {
  position: absolute;
  background: #0d1726;
  border: 1px solid #1f2c3d;
  border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0,0,0,.5);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  color: #cfe6f5;
  overflow: hidden;
  touch-action: none;
}
.er2d-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px 6px;
  background: linear-gradient(180deg, rgba(77,232,255,.15), transparent);
  border-top: 3px solid #4de8ff;
  border-bottom: 1px solid rgba(77,232,255,.25);
}
.er2d-name {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .02em;
  color: #eaf6ff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}
.er2d-meta {
  font-size: 11px;
  color: #5a7c96;
  font-family: ui-monospace, Menlo, monospace;
  flex-shrink: 0;
  margin-left: 6px;
}
.er2d-cols { padding: 4px 0; }
.er2d-col {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
  line-height: 1.4;
  min-height: 22px;
}
.er2d-col.is-pk { color: #ffd166; }
.er2d-col.is-fk { background: rgba(160,107,255,.14); }
.er2d-col .badges {
  display: flex;
  gap: 3px;
  flex-shrink: 0;
}
.er2d-col .badge {
  font-size: 9px;
  font-weight: 700;
  padding: 1px 4px;
  border-radius: 3px;
  line-height: 1;
}
.er2d-col .badge.pk {
  background: rgba(255,209,102,.2);
  color: #ffd166;
  border: 1px solid rgba(255,209,102,.5);
}
.er2d-col .badge.fk {
  background: rgba(160,107,255,.2);
  color: #c9a0ff;
  border: 1px solid rgba(160,107,255,.5);
}
.er2d-col .nm {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: inherit;
}
.er2d-col.is-fk .nm { color: #c9a0ff; }
.er2d-col .nm .ref {
  font-size: 10px;
  color: #5a7c96;
  margin-left: 2px;
}
.er2d-col .ty {
  font-size: 10px;
  color: #5a7c96;
  flex-shrink: 0;
}

/* ═══ ER 3D ビューポート ═════════════════ */
#er3d-viewport {
  position: relative;
  width: 100%;
  height: calc(100dvh - 190px);
  background: #03050d;      /* シーン背景と合わせる */
  border: 0;                /* 枠を消す */
  border-radius: 0;         /* 角丸も消す */
  overflow: hidden;
  touch-action: none;
}
#er3d-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}