/* ── テーマ変数 ──────────────────────────── */
[data-bs-theme="dark"] {
  --bg:       #0d1117;
  --surface:  #161b22;
  --surface2: #21262d;
  --border:   #30363d;
  --tx:       #e6edf3;
  --tx2:      #8b949e;
  --blue:     #58a6ff;
  --green:    #3fb950;
  --yellow:   #d29922;
  --red:      #f85149;
  --log-bg:   #0d1117;
}
[data-bs-theme="light"] {
  --bg:       #f6f8fa;
  --surface:  #ffffff;
  --surface2: #f0f2f5;
  --border:   #d0d7de;
  --tx:       #1f2328;
  --tx2:      #636c76;
  --blue:     #0969da;
  --green:    #1a7f37;
  --yellow:   #9a6700;
  --red:      #cf222e;
  --log-bg:   #f6f8fa;
}

/* ── ベース ──────────────────────────────── */
body { background: var(--bg); color: var(--tx); }
.main-wrap { padding-top: 58px; }

/* ── ナビバー ─────────────────────────────── */
.navbar { border-bottom: 1px solid var(--border); background: var(--surface) !important; }
.navbar-brand { font-weight: 700; font-size: 15px; }

/* ── WebSocket ステータスバッジ ───────────── */
.ws-badge {
  font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 4px;
}
.ws-online  { background: #1a7f3722; color: var(--green); border: 1px solid #1a7f3755; }
.ws-offline { background: #cf222e22; color: var(--red);   border: 1px solid #cf222e55; }

/* ── 一時停止バー ─────────────────────────── */
#paused-alert {
  background: #9a670022; border: 1px solid #9a670055; color: var(--yellow);
  border-radius: 6px; padding: 5px 14px; font-size: 13px;
}

/* ── カード ───────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.card-header {
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  padding: 8px 14px;
  font-size: 12px;
  font-weight: 600;
  color: var(--tx2);
  text-transform: uppercase;
  letter-spacing: .5px;
  display: flex;
  align-items: center;
  gap: 7px;
}
.card-header .card-title-icon { color: var(--blue); }
.card-header .ms-auto { text-transform: none; letter-spacing: 0; }
.card-body { color: var(--tx); }

/* ── 画像フィード ─────────────────────────── */
img.feed {
  width: 100%; height: auto; display: block;
  border-radius: 4px; object-fit: contain; background: var(--surface2);
}

/* ── ステータスバッジ ─────────────────────── */
.s-badge { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 5px; font-size: 13px; font-weight: 600; }
.s-working  { background: #1a7f3722; color: var(--green);  border: 1px solid #1a7f3755; }
.s-idle     { background: #9a670022; color: var(--yellow); border: 1px solid #9a670055; }
.s-away     { background: #cf222e22; color: var(--red);    border: 1px solid #cf222e55; }
.s-sleeping { background: #0969da22; color: #7c8cf8;       border: 1px solid #7c8cf855; }
.s-unknown  { background: #0969da22; color: var(--blue);   border: 1px solid #0969da55; }

/* ── 外出バッジ ───────────────────────────── */
.away-badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 5px; font-size: 12px; font-weight: 600; }
.away-home    { background: #1a7f3718; color: var(--green);  border: 1px solid #1a7f3744; }
.away-out     { background: #e6690022; color: #f0803c;       border: 1px solid #e6690055; }
.away-unknown { background: transparent; color: var(--tx2);  border: none; }

/* ── 怒りピップ ───────────────────────────── */
.pip { width: 12px; height: 12px; border-radius: 50%; display: inline-block; margin: 0 1px; }
.pip-on  { background: var(--red);    box-shadow: 0 0 4px var(--red); }
.pip-off { background: var(--border); }

/* ── stat ─────────────────────────────────── */
.stat-val  { font-size: 1.7rem; font-weight: 700; color: var(--tx); line-height: 1; }
.stat-unit { font-size: 11px; color: var(--tx2); }
.stat-lbl  { font-size: 10px; text-transform: uppercase; letter-spacing: .5px; color: var(--tx2); margin-bottom: 2px; }

/* ── 作業ログボックス ─────────────────────── */
.worklog-box {
  overflow-y: auto;
  font-family: 'Cascadia Code', 'Consolas', monospace;
  font-size: 11px;
  line-height: 1.5;
  background: var(--log-bg);
  color: var(--tx);
  padding: 8px;
  height: 100%;
}
.worklog-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}
.worklog-row:last-child { border-bottom: none; }
.worklog-ts   { color: var(--tx2); white-space: nowrap; flex-shrink: 0; font-size: 10px; }
.worklog-dot  { width: 8px; height: 8px; border-radius: 50%; margin-top: 3px; flex-shrink: 0; }
.worklog-body { flex: 1; word-break: break-word; }
.worklog-state { color: var(--tx2); font-size: 10px; }

/* ── チャット UI ──────────────────────────── */
.chat-area {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  min-height: 0;
}
.bubble {
  max-width: 82%;
  border-radius: 12px;
  padding: 7px 11px;
  font-size: 12px;
  line-height: 1.55;
  word-break: break-word;
}
.bubble-user   { background: var(--blue); color: #fff; align-self: flex-end; border-bottom-right-radius: 2px; }
.bubble-yukari { background: var(--surface2); color: var(--tx); align-self: flex-start; border-bottom-left-radius: 2px; }
.bubble-loading { opacity: 0.6; font-style: italic; animation: pulse 1s infinite; }
@keyframes pulse { 0%, 100% { opacity: .4; } 50% { opacity: .9; } }

.quick-btn { font-size: 11px; padding: 2px 8px; border-radius: 12px; }

/* ── dark-input ───────────────────────────── */
.dark-inp { background: var(--log-bg) !important; border-color: var(--border) !important; color: var(--tx) !important; }
.dark-inp::placeholder { color: var(--tx2) !important; }
.dark-inp:focus { border-color: var(--blue) !important; box-shadow: 0 0 0 2px #58a6ff22 !important; }

/* ── トークンボックス ─────────────────────── */
.tok-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.tok-val { font-weight: 700; color: var(--blue); font-size: 14px; }

/* ── 設定テーブル ─────────────────────────── */
.cfg-table { width: 100%; }
.cfg-table td { padding: 5px 0; border-bottom: 1px solid var(--border); font-size: 12px; vertical-align: middle; }
.cfg-table tr:last-child td { border-bottom: none; }
.cfg-table td:first-child { color: var(--tx2); width: 60%; }
.cfg-inp {
  width: 76px; background: var(--log-bg);
  border: 1px solid var(--border); color: var(--tx);
  border-radius: 4px; padding: 3px 7px; font-size: 12px;
}
.cfg-inp:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 2px #58a6ff22; }
.cfg-inp-wide { width: 100%; box-sizing: border-box; }
.pw-wrap { position: relative; display: flex; align-items: center; }
.pw-wrap .cfg-inp { flex: 1; padding-right: 28px; }
.pw-eye {
  position: absolute; right: 4px;
  background: none; border: none; cursor: pointer;
  color: var(--tx2); padding: 2px 4px; font-size: 13px;
  line-height: 1; opacity: 0.7;
}
.pw-eye:hover { opacity: 1; color: var(--tx); }

/* ── サブカメラグリッド ───────────────────────── */
.subcam-grid {
  display: flex; flex-wrap: wrap; gap: 8px; padding: 4px;
  overflow-y: auto; max-height: 100%;
}
.subcam-item {
  display: flex; flex-direction: column; align-items: center;
  background: var(--log-bg); border: 1px solid var(--border); border-radius: 6px;
  overflow: hidden; flex: 0 0 calc(25% - 6px); min-width: 120px;
}
.subcam-label {
  font-size: 11px; color: var(--muted); padding: 3px 0; width: 100%;
  text-align: center; background: var(--card-bg);
}
.subcam-img {
  width: 100%; aspect-ratio: 16/9; object-fit: cover; display: block;
}

/* ── トースト通知 ─────────────────────────── */
#toast-container {
  position: fixed; top: 56px; left: 50%; transform: translateX(-50%);
  z-index: 9999; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none;
}
.toast-msg {
  padding: 7px 18px; border-radius: 20px; font-size: 13px; font-weight: 500;
  opacity: 0; pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
  transform: translateY(-8px); white-space: nowrap;
}
.toast-msg.show { opacity: 1; transform: translateY(0); }
.toast-ok   { background: #1f6e2e; color: #aff5b4; border: 1px solid #2ea043; }
.toast-err  { background: #6e1f1f; color: #ffc7c7; border: 1px solid #a03030; }
.toast-info { background: #1e3a5f; color: #b8d9ff; border: 1px solid #2060a0; }

/* ── PTZ ─────────────────────────────────── */
.ptz-wrap { width: 100%; max-width: 160px; }

/* ── フィードカード (タブ統合ビュー) ────────── */
.feeds-header {
  padding-bottom: 0 !important;
  align-items: flex-end !important;
  gap: 0 !important;
  flex-wrap: nowrap;
}
.feeds-tabs-wrap {
  flex: 1;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  min-width: 0;
}
.feeds-tabs-wrap::-webkit-scrollbar { display: none; }
.feeds-nav {
  flex-wrap: nowrap;
  border-bottom: none;
  gap: 2px;
}
.feeds-nav .nav-link {
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 6px 6px 0 0;
  color: var(--tx2);
  border: 1px solid transparent;
  white-space: nowrap;
}
.feeds-nav .nav-link.active {
  color: var(--tx);
  background: var(--surface);
  border-color: var(--border) var(--border) var(--surface);
}
.feeds-nav .nav-link:hover:not(.active) {
  color: var(--tx);
  background: var(--surface2);
}
.feeds-tab-content {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.feed-pane {
  height: 100%;
  display: none;
  flex-direction: column;
}
.feed-pane.active { display: flex !important; }
.feed-img-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface2);
  overflow: hidden;
  position: relative;
}
.feed-llm-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(0,0,0,0.55);
  color: #ccc;
  cursor: pointer;
  z-index: 10;
  line-height: 1.4;
}
.feed-llm-btn:hover { background: rgba(0,0,0,0.8); color: #fff; }
.feed-llm-btn-active { background: var(--accent, #0d6efd) !important; color: #fff !important; border-color: transparent; }
.feed-img-wrap img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}
.feed-ts {
  font-size: 10px;
  color: var(--tx2);
  text-align: right;
  padding: 2px 6px;
  flex-shrink: 0;
}
/* PTZ バー (映像下) */
.feed-ptz-bar {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 4px 6px;
  display: flex;
  justify-content: center;
  gap: 3px;
  background: var(--surface2);
  flex-wrap: wrap;
}
.feed-ptz-bar .btn { font-size: 11px; padding: 2px 7px; line-height: 1.4; }
.feed-ptz-sep {
  width: 1px; background: var(--border); margin: 2px 2px; flex-shrink: 0;
}

/* ── GridStack オーバーライド ─────────────── */
.grid-stack { background: transparent; }
.grid-stack-item-content {
  border-radius: 10px !important;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.35);
}
[data-bs-theme="light"] .grid-stack-item-content {
  box-shadow: 0 1px 4px rgba(0,0,0,0.12);
}
.grid-stack-item.ui-draggable-dragging .grid-stack-item-content {
  box-shadow: 0 8px 24px rgba(0,0,0,0.55);
  opacity: 0.9;
}
.grid-stack-placeholder > .placeholder-content {
  border: 2px dashed var(--blue) !important;
  border-radius: 10px !important;
  background: rgba(88,166,255,0.06) !important;
}
.card-header { cursor: grab; user-select: none; }
.card-header:active { cursor: grabbing; }
.ui-draggable-dragging .card-header { cursor: grabbing; }
.card { height: 100%; display: flex; flex-direction: column; overflow: hidden; }
.card-body { flex: 1; overflow-y: auto; min-height: 0; }

/* ── レスポンシブ: ナロー画面対応 ──────────── */
/* GridStack が1カラムモードのとき、カードを自動高さで縦積み表示する */
@media (max-width: 599px) {
  .main-wrap { padding-top: 54px; }

  /* GridStack の absolute 配置を無効化して flex 縦積みに切り替え */
  #main-grid.grid-stack {
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    padding: 8px !important;
    height: auto !important;
  }
  #main-grid .grid-stack-item {
    position: static !important;
    transform: none !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    margin: 0 !important;
  }
  #main-grid .grid-stack-item-content {
    position: static !important;
    inset: 0 !important;
    height: auto !important;
  }
  /* カード本体は自動高さ、スクロール可能な最大高さを設定 */
  #main-grid .card { height: auto !important; min-height: 120px; }
  #main-grid .card-body {
    flex: none !important;
    max-height: 400px;
    overflow-y: auto;
  }
  /* チャットカードは少し高め */
  #main-grid .grid-stack-item[gs-id="chat"] .card-body { max-height: 500px; }
  /* カメラ/スクリーンショット画像は auto */
  #main-grid img.feed { max-height: 220px; object-fit: contain; }
}

/* ── レスポンシブ: タブレット (600-991px) ── */
@media (min-width: 600px) and (max-width: 991px) {
  .main-wrap { overflow-x: hidden; }
}

/* ── カメラ管理リスト ────────────────────── */
.cam-entry {
  border: 1px solid var(--border);
  border-radius: 5px;
  margin-bottom: 5px;
  overflow: hidden;
}
.cam-row {
  padding: 5px 7px;
  background: var(--surface2);
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: nowrap;
}
.cam-name {
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}
.cam-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 3px;
  flex-shrink: 0;
  line-height: 1.6;
}
.cam-badge-main {
  background: #9a670033;
  color: var(--yellow);
  border: 1px solid #9a670066;
}
.cam-badge-sub {
  background: var(--surface);
  color: var(--tx2);
  border: 1px solid var(--border);
}
.cam-edit-panel {
  padding: 7px;
  background: var(--bg);
  border-top: 1px solid var(--border);
}

/* ── カード管理パネル ─────────────────────── */
#card-panel {
  position: fixed; top: 48px; right: -290px; width: 276px;
  height: calc(100vh - 48px); background: var(--surface);
  border-left: 1px solid var(--border); z-index: 1050;
  transition: right .25s cubic-bezier(.4,0,.2,1);
  display: flex; flex-direction: column;
  box-shadow: -4px 0 20px rgba(0,0,0,0.3);
}
#card-panel.open { right: 0; }
#cp-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.25); z-index: 1049;
}
.cp-header {
  padding: 11px 14px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; font-weight: 600; color: var(--tx); flex-shrink: 0;
}
.cp-body   { flex: 1; overflow-y: auto; padding: 10px 12px; }
.cp-footer { padding: 10px 12px; border-top: 1px solid var(--border); flex-shrink: 0; }
.cp-item {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 8px; border-radius: 7px; cursor: pointer;
  font-size: 13px; color: var(--tx); transition: background .1s; margin-bottom: 2px;
}
.cp-item:hover { background: var(--surface2); }
.cp-item input[type=checkbox] { width: 15px; height: 15px; flex-shrink: 0; cursor: pointer; }
