:root {
  --bg: #f4f9fd;
  --bg-alt: #e8f2fc;
  --panel: #ffffff;
  --border: #c5d9ee;
  --text: #1a2b3d;
  --muted: #5a7289;
  --accent: #4a90d9;
  --accent-hover: #3a7bc4;
  --accent-soft: #dbeafe;
  --user-bg: #e3f0ff;
  --bot-bg: #ffffff;
  --danger: #d64545;
  --shadow: 0 2px 12px rgba(74, 144, 217, 0.08);
  --radius: 12px;
  font-family: "Pretendard", "Noto Sans KR", system-ui, sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: linear-gradient(165deg, var(--bg-alt) 0%, var(--bg) 45%, #f8fbff 100%);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
}

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 0.25rem; font-size: 1.4rem; color: var(--accent-hover); }
.login-card label { display: block; margin-top: 1rem; font-size: 0.85rem; color: var(--muted); }
.login-card input {
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.65rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
}
.error { color: var(--danger); font-size: 0.9rem; margin-top: 0.75rem; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.85rem; }

.btn.block { width: 100%; margin-top: 1.25rem; }

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  --sidebar-w: 260px;
  position: relative;
}
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(26, 43, 61, 0.35);
  z-index: 9;
}
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: width 0.2s ease, border-color 0.2s ease;
  z-index: 10;
}
.sidebar-inner {
  width: var(--sidebar-w);
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 0.55rem 0.45rem 0.55rem 0.55rem;
  gap: 0.35rem;
}
#new-session {
  width: 100%;
  padding: 0.45rem 0.5rem;
  font-size: 0.85rem;
}
.app.sidebar-collapsed { --sidebar-w: 0px; }
.app.sidebar-collapsed .sidebar { border-right-color: transparent; }
.sidebar-head { margin-bottom: 0.25rem; display: flex; flex-direction: column; gap: 0.1rem; }
.sidebar-brand { font-size: 0.9rem; color: var(--accent-hover); }
.sidebar-user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
  margin-top: 0.15rem;
}
.user-name {
  font-size: 0.85rem;
  color: var(--muted);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.btn-settings {
  flex-shrink: 0;
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1;
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.btn-settings:hover {
  color: var(--accent-hover);
  background: var(--accent-soft);
  border-color: var(--border);
}
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(26, 43, 61, 0.4);
  backdrop-filter: blur(4px);
}
.modal-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.35rem 1.1rem;
  box-shadow: 0 12px 40px rgba(45, 111, 173, 0.18);
}
.modal-card h2 { margin: 0; font-size: 1.1rem; color: var(--accent-hover); }
.modal-sub { margin: 0.35rem 0 1rem; font-size: 0.85rem; }
.modal-card label {
  display: block;
  margin-top: 0.75rem;
  font-size: 0.82rem;
  color: var(--muted);
}
.modal-card input {
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.55rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  font-size: 16px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.1rem;
}
.session-list { flex: 1; overflow-y: auto; min-height: 0; }
.session-item {
  border-radius: 8px;
  border: 1px solid transparent;
  margin-bottom: 0.1rem;
}
.session-item:hover { background: var(--bg-alt); }
.session-item.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.session-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 1.25rem;
  align-items: center;
  column-gap: 0.35rem;
  min-height: 1.65rem;
  padding: 0.15rem 0.2rem 0.15rem 0.4rem;
}
.session-item .title {
  font-size: 0.8rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.session-item .meta {
  font-size: 0.7rem;
  color: var(--muted);
  cursor: pointer;
}
.session-hide {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.95rem;
  padding: 0;
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 4px;
}
.session-hide:hover { color: var(--danger); background: rgba(214, 69, 69, 0.1); }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: transparent; }
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
}
.title-input {
  flex: 1 1 120px;
  min-width: 0;
  padding: 0.45rem 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
}
.toggle {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}
.icon-btn {
  flex-shrink: 0;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  font-size: 1rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  -webkit-overflow-scrolling: touch;
}
.msg {
  max-width: min(900px, 100%);
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 0.93rem;
  box-shadow: var(--shadow);
}
.msg.user { align-self: flex-end; background: var(--user-bg); border: 1px solid #bdd7f5; }
.msg.assistant {
  align-self: flex-start;
  background: var(--bot-bg);
  border: 1px solid var(--border);
}
.msg-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}
.msg .role { font-size: 0.68rem; color: var(--muted); flex: 1; }
.msg-gen-image {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  min-height: 1.85rem;
  padding: 0.38rem 0.72rem;
  font-size: 0.74rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  line-height: 1;
  border-radius: 999px;
  border: 1px solid rgba(58, 123, 196, 0.28);
  background: linear-gradient(165deg, #ffffff 0%, #e6f2ff 48%, #d4e8ff 100%);
  color: #2d6fad;
  cursor: pointer;
  box-shadow:
    0 1px 2px rgba(45, 111, 173, 0.12),
    0 0 0 1px rgba(255, 255, 255, 0.7) inset;
  transition:
    transform 0.14s ease,
    box-shadow 0.14s ease,
    border-color 0.14s ease,
    background 0.14s ease;
}
.msg-gen-image::before {
  content: "";
  width: 0.95rem;
  height: 0.95rem;
  flex-shrink: 0;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%232d6fad' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='3' width='18' height='18' rx='2.5'/%3E%3Ccircle cx='8.5' cy='8.5' r='1.75'/%3E%3Cpath d='m21 15-5-5L5 21'/%3E%3C/svg%3E")
    center / contain no-repeat;
  opacity: 0.92;
}
.msg-gen-image:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: rgba(58, 123, 196, 0.45);
  background: linear-gradient(165deg, #ffffff 0%, #dceeff 100%);
  box-shadow:
    0 3px 8px rgba(45, 111, 173, 0.16),
    0 0 0 1px rgba(255, 255, 255, 0.85) inset;
}
.msg-gen-image:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 0 1px 3px rgba(45, 111, 173, 0.12);
}
.msg-gen-image:disabled {
  opacity: 0.6;
  cursor: wait;
  transform: none;
}
.msg.user .msg-gen-image {
  background: linear-gradient(165deg, #f8fbff 0%, #cfe4ff 100%);
  border-color: rgba(45, 111, 173, 0.22);
}
.msg-generating {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  color: var(--muted);
  font-size: 0.9rem;
  padding: 0.15rem 0;
}
.msg-pending-image {
  border-style: dashed;
  border-color: rgba(74, 144, 217, 0.35);
  background: linear-gradient(180deg, #fafcff 0%, #f0f7ff 100%);
}
.spinner {
  width: 1.05rem;
  height: 1.05rem;
  border: 2px solid #c5d9ee;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
  flex-shrink: 0;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}
.msg-images {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.45rem;
  margin-top: 0.45rem;
}
.msg-images img {
  max-width: min(100%, 320px);
  max-height: 320px;
  object-fit: contain;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fff;
  box-shadow: 0 2px 10px rgba(45, 111, 173, 0.1);
}
.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.38rem 0.75rem;
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: 999px;
  border: 1px solid rgba(58, 123, 196, 0.3);
  background: #fff;
  color: var(--accent-hover);
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(45, 111, 173, 0.1);
  transition: background 0.12s, box-shadow 0.12s;
}
.btn-download::before {
  content: "";
  width: 0.85rem;
  height: 0.85rem;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%233a7bc4' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M12 3v12M7 10l5 5 5-5'/%3E%3Cpath d='M5 21h14'/%3E%3C/svg%3E")
    center / contain no-repeat;
}
.btn-download:hover {
  background: var(--accent-soft);
  box-shadow: 0 2px 6px rgba(45, 111, 173, 0.14);
}
.msg > img { max-width: 100%; border-radius: 8px; margin-top: 0.45rem; }
.msg pre { overflow-x: auto; font-size: 0.85rem; }

.composer-wrap {
  border-top: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.95);
  padding: 0.5rem 0.75rem 0.65rem;
}
.composer-wrap.drag-over { background: var(--accent-soft); }
.attach-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.45rem;
}
.attach-chip {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.attach-chip img { width: 100%; height: 100%; object-fit: cover; }
.attach-chip button {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 1.25rem;
  height: 1.25rem;
  border: none;
  border-radius: 4px;
  background: rgba(0,0,0,0.55);
  color: #fff;
  font-size: 0.75rem;
  cursor: pointer;
  line-height: 1;
}
.composer {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
}
.composer-tools { flex-shrink: 0; }
.composer-send {
  flex-shrink: 0;
  align-self: flex-end;
}
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  padding: 0.55rem 0.65rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  font-size: 16px;
  max-height: 140px;
}
.composer-hint {
  margin: 0.35rem 0 0;
  font-size: 0.72rem;
}

.btn {
  padding: 0.5rem 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  font-size: 0.88rem;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent-hover);
  color: #fff;
}
.btn.primary:hover { background: var(--accent-hover); }
.btn.secondary {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-hover);
}
.btn.ghost { margin-top: auto; background: transparent; color: var(--muted); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

@media (max-width: 768px) {
  .app:not(.sidebar-collapsed) .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    height: 100%;
    height: 100dvh;
    --sidebar-w: min(88vw, 300px);
    box-shadow: 4px 0 24px rgba(26, 43, 61, 0.15);
  }
  .app.sidebar-collapsed { --sidebar-w: 0px; }
  .toolbar { padding: 0.45rem 0.5rem; gap: 0.35rem; }
  .toolbar-thinking .toggle-label { font-size: 0.75rem; }
  .messages { padding: 0.5rem 0.55rem; }
  .composer-wrap { padding: 0.45rem 0.5rem; padding-bottom: max(0.45rem, env(safe-area-inset-bottom)); }
  .composer-hint { display: none; }
  .composer-send { padding: 0.55rem 0.75rem; }
  .msg { font-size: 0.9rem; }
  .msg-images img { max-width: 120px; max-height: 120px; }
}

