body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* iOS キーボード表示時に正しくリサイズ */
  width: 100%;
  overflow: hidden;
}

html {
  overflow: hidden;
  width: 100%;
}

/* 狭い画面でヘッダーボタンを小さく */
@media (max-width: 400px) {
  .icon-btn {
    width: 28px;
    height: 28px;
  }
  .icon-btn svg {
    width: 15px;
    height: 15px;
  }
  #model-select {
    max-width: 100px;
    font-size: 11px;
  }
}

/* ===== Header ===== */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  padding-top: calc(6px + env(safe-area-inset-top, 0px));
  padding-left: calc(10px + env(safe-area-inset-left, 0px));
  padding-right: calc(10px + env(safe-area-inset-right, 0px));
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
  gap: 6px;
  min-width: 0;
  overflow: hidden;
}
.header-left {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.header-right {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
  flex-shrink: 1;
}
.app-title {
  font-size: 15px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), #00a8ff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: -0.3px;
  user-select: none;
}
#model-select {
  padding: 5px 8px;
  padding-right: 24px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-family: var(--font-family);
  max-width: 130px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%23666' viewBox='0 0 24 24'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 6px center;
  transition: border-color var(--transition);
}
#model-select:focus {
  border-color: var(--accent);
}
#model-select.models-loading {
  animation: models-pulse 0.7s ease infinite;
}
#model-select.models-refreshed {
  animation: models-lock 0.45s ease forwards;
}
@keyframes models-pulse {
  0%, 100% { opacity: 0.4; }
  50%       { opacity: 0.7; }
}
@keyframes models-lock {
  0%   { opacity: 0.5; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(0,212,170,0.35); }
  60%  { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0,212,170,0.15); }
  100% { opacity: 1;   border-color: var(--border);  box-shadow: none; }
}
#model-select option {
  background: var(--bg-surface);
  color: var(--text-primary);
}

/* ===== Sidebar (conversations) ===== */
.sidebar-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
}
.sidebar-backdrop.visible {
  opacity: 1;
  pointer-events: auto;
}

#conversation-sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: 270px;
  height: 100%;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  z-index: 100;
  transform: translateX(-100%);
  transition: transform 220ms cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
}
#conversation-sidebar.visible {
  transform: translateX(0);
}
.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 14px 10px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
  padding-left: calc(14px + env(safe-area-inset-left, 0px));
  border-bottom: 1px solid var(--border);
}
.sidebar-header h2 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}
#conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}
.conversation-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 2px;
  position: relative;
}
.conversation-item:hover {
  background: var(--bg-hover);
}
.conversation-item.active {
  background: var(--accent-dim);
}
.conversation-item .conv-title {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}
.conversation-item .conv-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  flex-shrink: 0;
}
.conversation-item .conv-time {
  font-size: 10px;
  color: var(--text-muted);
}
.conversation-item .conv-delete {
  opacity: 0;
  transition: opacity var(--transition);
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  padding: 2px;
  line-height: 1;
}
.conversation-item:hover .conv-delete {
  opacity: 1;
}
.conversation-item .conv-delete:hover {
  color: var(--danger);
}

/* ===== Settings panel ===== */
#settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 300px;
  height: 100%;
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  z-index: 100;
  transform: translateX(100%);
  transition: transform 220ms cubic-bezier(0.4, 0, 0.2, 1);
  padding: 16px;
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  padding-right: calc(16px + env(safe-area-inset-right, 0px));
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}
#settings-panel.visible {
  transform: translateX(0);
}
#settings-panel h2 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.settings-actions {
  display: flex;
  gap: 8px;
}
.settings-actions .btn {
  flex: 1;
}
#system-prompt-input {
  resize: vertical;
  min-height: 60px;
  max-height: 200px;
  line-height: 1.4;
  font-size: 12px;
}

/* ===== Context bar ===== */
.context-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.context-label {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 500;
  flex-shrink: 0;
  margin-right: 2px;
}
.context-chip {
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--text-secondary);
  font-size: 11px;
  font-family: var(--font-family);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
  flex-shrink: 0;
}
.context-chip:hover {
  border-color: var(--text-muted);
  color: var(--text-primary);
}
.context-chip.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 500;
}
.context-status {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: auto;
  flex-shrink: 0;
}
.context-status.loading {
  color: var(--warning);
}
.context-status.cached {
  color: var(--accent);
}
.context-status.error {
  color: var(--danger);
}
.context-badge {
  display: inline-block;
  font-size: 10px;
  padding: 1px 7px;
  border-radius: var(--radius-full);
  background: rgba(0, 0, 0, 0.3);
  color: rgba(255, 255, 255, 0.7);
  margin-bottom: 4px;
  font-weight: 500;
}

/* ===== Install banner ===== */
#install-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--accent-dim);
  border-bottom: 1px solid var(--accent);
  flex-shrink: 0;
}
#install-banner span {
  flex: 1;
  font-size: 12px;
  color: var(--accent);
}
#install-banner #install-btn {
  font-size: 12px;
  padding: 5px 14px;
  flex-shrink: 0;
}
#install-banner #install-dismiss {
  color: var(--accent);
  flex-shrink: 0;
}

/* ===== Chat container ===== */
#chat-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}
#messages {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 14px;
  min-height: 100%;
}

/* Welcome state */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  min-height: 60vh;
  color: var(--text-muted);
  text-align: center;
  gap: 12px;
  padding: 40px 20px;
}
.welcome-icon {
  width: 56px;
  height: 56px;
  border-radius: var(--radius-lg);
  background: linear-gradient(135deg, var(--accent-dim), rgba(0, 168, 255, 0.1));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  margin-bottom: 4px;
}
.welcome h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-secondary);
}
.welcome p {
  font-size: 13px;
  max-width: 260px;
  line-height: 1.5;
}

/* ===== Input area ===== */
#input-area {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
}
.input-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding-top: 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom, 0px));
  padding-left: max(10px, env(safe-area-inset-left, 0px));
  padding-right: max(10px, env(safe-area-inset-right, 0px));
}
#message-input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  font-family: var(--font-family);
  font-size: 16px; /* 16px未満だとiOS Safariが自動ズームするため */
  line-height: 1.45;
  resize: none;
  outline: none;
  max-height: 200px;
  transition: border-color var(--transition);
}
#message-input:focus {
  border-color: var(--accent);
}
#message-input::placeholder {
  color: var(--text-muted);
}
#send-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent);
  color: #000;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
}
#send-btn:hover {
  background: var(--accent-hover);
  transform: scale(1.05);
}
#send-btn:active {
  transform: scale(0.95);
}
#send-btn.stop {
  background: var(--danger);
  color: #fff;
}
#send-btn.stop:hover {
  background: #e04050;
}
#send-btn svg {
  width: 18px;
  height: 18px;
}

/* Generation stats */
.gen-stats {
  font-size: 11px;
  color: var(--text-muted);
  padding: 2px 4px;
}

/* ===== Host switch flash ===== */
#host-switch-flash {
  position: fixed;
  inset: 0;
  background: var(--bg-primary, #0d0d1a);
  opacity: 0;
  pointer-events: none;
  z-index: 999;
}
#host-switch-flash.active {
  animation: host-flash 1.0s ease-in-out forwards;
}
@keyframes host-flash {
  0%   { opacity: 0; }
  35%  { opacity: 0.8; }
  100% { opacity: 0; }
}

/* ===== Node Selector ===== */
.node-selector {
  position: relative;
}
.node-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 7px 3px 6px;
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 20px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-primary);
  font-family: var(--font-family);
  transition: background 0.15s;
  white-space: nowrap;
  max-width: 110px;
}
.node-chip:hover {
  background: rgba(255,255,255,0.13);
}
#node-name-label {
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 70px;
}
.node-chevron {
  width: 10px;
  height: 10px;
  opacity: 0.5;
  flex-shrink: 0;
}
.node-dropdown {
  position: fixed;
  min-width: 190px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  z-index: 200;
  overflow: hidden;
}
.node-dropdown.hidden { display: none; }
.node-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.1s;
}
.node-item:hover { background: rgba(255,255,255,0.06); }
.node-item.active { color: var(--accent); }
.node-item-name { flex: 1; }
.node-item-dot { flex-shrink: 0; }
.node-check { width: 14px; height: 14px; flex-shrink: 0; }
.node-item-empty {
  padding: 12px 14px;
  font-size: 12px;
  opacity: 0.5;
  text-align: center;
}

/* ===== Hosts list (settings) ===== */
.hosts-list { margin-top: 8px; }
.host-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255,255,255,0.04);
  border: 1px solid transparent;
  margin-bottom: 6px;
  transition: background 0.1s;
}
.host-item.active {
  background: rgba(0,212,170,0.08);
  border-color: rgba(0,212,170,0.25);
}
.host-info {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}
.host-item-name {
  display: block;
  font-size: 13px;
  font-weight: 500;
}
.host-item-url {
  display: block;
  font-size: 11px;
  opacity: 0.5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.host-item-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}
.host-item-actions .icon-btn {
  width: 28px;
  height: 28px;
  padding: 5px;
}
.host-form {
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  margin-top: 4px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.host-form.hidden { display: none; }
.host-form-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.host-form-actions .btn { font-size: 12px; padding: 6px 14px; }

/* Empty conversation list */
.empty-conversations {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: var(--text-muted);
  text-align: center;
  gap: 8px;
}
.empty-conversations p {
  font-size: 13px;
}
