:root {
  --bg: #ffffff;
  --bg-2: #f5f5f7;
  --bg-3: #ebebef;
  --line: #e3e3e8;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --accent: #1d1d1f;
  --accent-text: #ffffff;
  --in: #efeff2;
  --out: #1d1d1f;
  --out-text: #ffffff;
  --danger: #d70015;
  --warn-bg: #fff4e0;
  --warn: #9a5b00;
  --focus: #0071e3;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111113;
    --bg-2: #1c1c1f;
    --bg-3: #2a2a2e;
    --line: #2f2f34;
    --text: #f5f5f7;
    --muted: #a1a1a6;
    --accent: #f5f5f7;
    --accent-text: #111113;
    --in: #26262a;
    --out: #f5f5f7;
    --out-text: #111113;
    --danger: #ff6961;
    --warn-bg: #3a2c12;
    --warn: #ffcc80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Segoe UI", "Noto Sans JP", Meiryo, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 16px; /* iPhoneで入力時に拡大されないように */
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: transparent; }
label { display: block; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--text); }
code { background: var(--bg-3); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); }
.loading { color: var(--muted); text-align: center; padding: 32px 0; margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--bg); border-radius: 999px;
  padding: 9px 18px; font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--bg-2); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { opacity: .88; }
.btn.primary-outline { border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 6px 14px; font-size: 13px; }
.btn:disabled, .btn.disabled { opacity: .4; cursor: default; pointer-events: none; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; flex: none; border: 0; background: transparent; border-radius: 50%;
  color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--bg-2); color: var(--text); }

/* ---------- レイアウト ---------- */
.layout { display: flex; height: 100vh; height: 100dvh; }
.list-pane { width: 340px; flex: none; border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.thread-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
.info-pane { width: 300px; flex: none; border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }

/* ---------- 一覧 ---------- */
.list-head { padding: 12px 12px 8px; padding-top: max(12px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); display: grid; gap: 10px; }
.account { display: flex; align-items: center; gap: 10px; }
.account > div { flex: 1; min-width: 0; }
.account img { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; }
.acct-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acct-sub { font-size: 12px; color: var(--muted); }
.segmented { display: flex; background: var(--bg-2); border-radius: 10px; padding: 3px; }
.segmented button { flex: 1; border: 0; background: transparent; border-radius: 8px; padding: 6px 0; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer; }
.segmented button.on { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.count:not(:empty) { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: var(--accent-text); font-size: 11px; line-height: 18px; }
.search-row { display: flex; gap: 6px; }
.search-row input { padding: 7px 10px; font-size: 15px; }
.search-row select { width: auto; max-width: 110px; padding: 7px 8px; font-size: 14px; }

.notice { margin: 8px 12px 0; padding: 9px 12px; border-radius: 10px; background: var(--warn-bg); color: var(--warn); font-size: 13px; }
.notice a { font-weight: 700; }

.contact-list { list-style: none; margin: 0; padding: 4px 0; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.list-more { padding: 16px; text-align: center; color: var(--muted); font-size: 13px; }
.list-empty { padding: 40px 16px; text-align: center; color: var(--muted); font-size: 14px; }
.contact { display: flex; gap: 10px; padding: 10px 12px; margin: 0 6px; border-radius: 10px; cursor: pointer; align-items: flex-start; }
.contact:hover { background: var(--bg-2); }
.contact.active { background: var(--bg-3); }
.c-main { flex: 1; min-width: 0; }
.c-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.c-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-top time { font-size: 12px; color: var(--muted); flex: none; }
.c-text { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact.unread .c-name, .contact.unread .c-text { font-weight: 700; color: var(--text); }
.c-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.badge { flex: none; align-self: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: #0071e3; color: #fff; font-size: 12px; font-weight: 700; text-align: center; line-height: 20px; }
.due { font-size: 11px; color: var(--warn); background: var(--warn-bg); border-radius: 6px; padding: 0 6px; }

.avatar { width: 44px; height: 44px; border-radius: 50%; object-fit: cover; flex: none; background: var(--bg-3); }
.avatar.ph { display: inline-flex; align-items: center; justify-content: center; font-weight: 700; color: var(--muted); }

.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 999px; border: 1px solid transparent; }
.c-gray { background: #ececf0; color: #515154; }
.c-red { background: #ffe5e5; color: #b3001b; }
.c-orange { background: #ffeedd; color: #a64b00; }
.c-yellow { background: #fff6cc; color: #7a5d00; }
.c-green { background: #e2f6e6; color: #1b6e2f; }
.c-blue { background: #e3efff; color: #0a4fb3; }
.c-purple { background: #f0e7ff; color: #6a2fb8; }
@media (prefers-color-scheme: dark) {
  .c-gray { background: #3a3a3f; color: #d1d1d6; }
  .c-red { background: #4a1c20; color: #ff9b9b; }
  .c-orange { background: #4a2e14; color: #ffc085; }
  .c-yellow { background: #453b12; color: #ffe27a; }
  .c-green { background: #183a22; color: #8fe0a3; }
  .c-blue { background: #152f52; color: #94c2ff; }
  .c-purple { background: #33204f; color: #cfb1ff; }
}

/* ---------- 会話 ---------- */
.empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.thread { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread-head { display: flex; align-items: center; gap: 10px; padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); }
.thread-head .avatar { width: 36px; height: 36px; font-size: 14px; }
.t-title { flex: 1; min-width: 0; }
.t-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.t-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 4px; -webkit-overflow-scrolling: touch; }
.day { align-self: center; font-size: 12px; color: var(--muted); margin: 14px 0 6px; }
.msg { display: flex; flex-direction: column; max-width: min(72%, 520px); }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 8px 13px; border-radius: 18px; overflow-wrap: anywhere; }
.msg.in .bubble { background: var(--in); border-bottom-left-radius: 6px; }
.msg.out .bubble { background: var(--out); color: var(--out-text); border-bottom-right-radius: 6px; }
.bubble .text { white-space: pre-wrap; }
.bubble a { text-decoration: underline; }
.meta { font-size: 11px; color: var(--muted); margin: 2px 6px 6px; }
.att-img img { display: block; max-width: 240px; max-height: 320px; border-radius: 12px; margin: 2px 0; }
.att-video { max-width: 260px; border-radius: 12px; }
.att-label { font-size: 12px; opacity: .75; }
.att { font-size: 13px; opacity: .85; }

.window-bar { font-size: 12px; text-align: center; padding: 5px 12px; color: var(--muted); border-top: 1px solid var(--line); }
.window-bar.human_agent { background: var(--warn-bg); color: var(--warn); }
.window-bar.closed { background: var(--bg-2); color: var(--danger); }

.composer { position: relative; padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.composer-row { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea { flex: 1; width: auto; min-width: 0; resize: none; overflow-y: hidden; max-height: 200px; border-radius: 20px; padding: 8px 14px; line-height: 1.4; margin: 0 4px; }
.composer .btn { height: 38px; }
.done-check { display: inline-flex; align-items: center; gap: 6px; margin: 6px 0 0 4px; font-size: 12px; cursor: pointer; }
.done-check input { width: auto; margin: 0; }
.attach-preview { display: flex; align-items: flex-start; gap: 4px; margin-bottom: 6px; }
.attach-preview img { max-height: 90px; border-radius: 10px; }
.popover {
  position: absolute; left: 12px; bottom: calc(100% + 6px); width: min(420px, calc(100vw - 24px)); max-height: 50vh; overflow-y: auto;
  background: var(--bg); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 10px 30px rgba(0,0,0,.15); padding: 6px; z-index: 10;
}
.popover button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 8px 10px; border-radius: 10px; cursor: pointer; }
.popover button:hover { background: var(--bg-2); }
.popover button strong { display: block; font-size: 14px; }
.popover button span { display: block; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.popover p { margin: 8px 10px; }

/* ---------- お客様情報 ---------- */
.info-head, .settings-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 10px 16px; border-bottom: 1px solid var(--line); min-height: 57px; }
.info-body { padding: 16px; overflow-y: auto; flex: 1; }
.info-body h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin: 20px 0 8px; }
.info-profile { display: flex; gap: 12px; align-items: center; }
.info-profile .avatar { width: 56px; height: 56px; font-size: 20px; }
.i-name { font-weight: 700; }
.tag-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-picker .tag { cursor: pointer; font-size: 13px; padding: 4px 11px; opacity: .45; }
.tag-picker .tag.on { opacity: 1; border-color: currentColor; }
.saved { font-weight: 400; margin-left: 6px; }
.link { display: inline-block; margin-top: 16px; font-size: 13px; color: var(--muted); }

/* ---------- 設定 ---------- */
.settings { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; justify-content: center; align-items: flex-start; padding: 40px 16px; z-index: 50; overflow-y: auto; }
.settings-panel { background: var(--bg); width: 100%; max-width: 720px; border-radius: 16px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.settings-tabs { display: flex; gap: 4px; padding: 8px 12px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.settings-tabs button { border: 0; background: transparent; padding: 6px 12px; border-radius: 999px; font-weight: 600; font-size: 14px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.settings-tabs button.on { background: var(--accent); color: var(--accent-text); }
.settings-body { padding: 16px; }
.help { font-size: 13px; color: var(--muted); margin: 0 0 12px; }
.cards { display: grid; gap: 12px; }
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 10px; margin: 0 0 12px; }
.cards .card { margin: 0; }
.card.new { border-style: dashed; }
.card p { margin: 0; }
.card.inline { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.card.inline input { flex: 1; min-width: 120px; width: auto; }
.card.inline select { width: auto; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; flex-wrap: nowrap; }
.row.tight { gap: 6px; margin-top: 4px; color: var(--text); flex-wrap: nowrap; }
.row.tight input { width: auto; }
.row > label { flex: 1; min-width: 200px; }
.switch { display: inline-flex; align-items: center; gap: 6px; flex: none !important; min-width: 0 !important; white-space: nowrap; color: var(--text); font-size: 14px; }
.switch input { width: auto; margin: 0; }
.rule-test { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.rule-test input { flex: 1; min-width: 200px; width: auto; }
.rule-test #rule-test-result { flex-basis: 100%; }
.log details { border-top: 1px solid var(--line); padding: 6px 0; font-size: 13px; }
.log summary { cursor: pointer; }
.log pre { white-space: pre-wrap; word-break: break-all; font-size: 11px; background: var(--bg-2); padding: 8px; border-radius: 8px; max-height: 300px; overflow: auto; }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--accent); color: var(--accent-text); padding: 10px 18px; border-radius: 12px; font-size: 14px; z-index: 100; max-width: calc(100vw - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.2); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- ログイン ---------- */
body.auth { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--bg-2); padding: 16px; }
.auth-card { background: var(--bg); padding: 32px 28px; border-radius: 18px; width: 100%; max-width: 360px; display: grid; gap: 14px; box-shadow: 0 8px 30px rgba(0,0,0,.08); }
.auth-card h1 { margin: 0; font-size: 24px; }
.auth-card p { margin: 0; }

/* ---------- 画面幅ごとの調整 ---------- */
@media (max-width: 1099px) {
  .info-pane { position: fixed; top: 0; right: 0; bottom: 0; width: min(340px, 100vw); background: var(--bg); z-index: 30; box-shadow: -10px 0 30px rgba(0,0,0,.15); }
}
@media (max-width: 767px) {
  .list-pane { width: 100%; border-right: 0; }
  .thread-pane { display: none; }
  body.show-thread .list-pane { display: none; }
  body.show-thread .thread-pane { display: flex; }
  .back { display: inline-flex; margin-left: -8px; }
  .thread-head { gap: 6px; padding-left: 8px; padding-right: 8px; }
  .thread-head .btn.small { padding: 6px 10px; font-size: 12px; }
  .msg { max-width: 82%; }
  .messages { padding: 12px; }
  .settings { padding: 0; }
  .settings-panel { border-radius: 0; min-height: 100%; }
  .settings-head { padding-top: max(10px, env(safe-area-inset-top)); }
  .info-pane { width: 100vw; }
}
