/* ===== Reset & Variables ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg:            #fdf5ec;
  --bg2:           #fef0df;
  --primary:       #c9714d;
  --primary-light: #f4a57a;
  --primary-dark:  #a85a38;
  --accent:        #e8b89a;
  --grandma-bg:    #fdf0e8;
  --grandma-bdr:   #e8c4a8;
  --ojisan-bg:     #edf3ff;
  --ojisan-bdr:    #b0c8f8;
  --text:          #3d2b1f;
  --text-light:    #8a6a58;
  --white:         #ffffff;
  --radius:        18px;
  --shadow:        0 4px 24px rgba(120,60,20,0.12);
  --font:          'Hiragino Kaku Gothic ProN', 'ヒラギノ角ゴ ProN W3', 'Meiryo', 'メイリオ', sans-serif;
  --font-chat:     'Hiragino Mincho ProN', 'ヒラギノ明朝 ProN', 'Yu Mincho', serif;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  background: linear-gradient(160deg, var(--bg) 0%, var(--bg2) 100%);
  color: var(--text);
  font-size: 18px;
  line-height: 1.75;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ===== App layout ===== */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 1100px;
  margin: 0 auto;
}

/* ===== Header ===== */
.header {
  flex-shrink: 0;
  background: linear-gradient(135deg, #fff8f3 0%, #ffeedd 100%);
  border-bottom: 3px solid var(--accent);
  padding: 12px 20px;
  box-shadow: 0 2px 12px rgba(200,100,50,0.1);
}

.header-inner {
  text-align: center;
}

.header-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.header-title h1 {
  font-size: 26px;
  color: var(--primary);
  font-weight: bold;
  letter-spacing: 0.08em;
}

.sakura {
  font-size: 22px;
}

.subtitle {
  font-size: 13px;
  color: var(--text-light);
  margin-top: 2px;
  letter-spacing: 0.05em;
}

/* ===== Main ===== */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
  gap: 0;
}

/* ===== Grandma Aside ===== */
.grandma-aside {
  flex-shrink: 0;
  width: 260px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 20px 12px;
  background: rgba(255,255,255,0.5);
  border-right: 2px solid var(--grandma-bdr);
}

.photo-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

/* ===== Photo Frame ===== */
.photo-frame {
  width: 200px;
  height: 255px;
  border-radius: 50% 50% 48% 48% / 58% 58% 42% 42%;
  background: var(--grandma-bg);
  border: 5px solid var(--accent);
  box-shadow:
    0 0 0 3px #fff,
    0 0 0 6px var(--primary-light),
    var(--shadow);
  overflow: hidden;
  position: relative;
  transition: box-shadow 0.4s ease;
}

.photo-frame.speaking {
  animation: speakGlow 1.4s ease-in-out infinite;
}

@keyframes speakGlow {
  0%, 100% {
    box-shadow:
      0 0 0 3px #fff,
      0 0 0 6px var(--primary-light),
      0 0 16px 4px rgba(201,113,77,0.35),
      var(--shadow);
  }
  50% {
    box-shadow:
      0 0 0 3px #fff,
      0 0 0 8px var(--primary-light),
      0 0 40px 14px rgba(201,113,77,0.6),
      var(--shadow);
  }
}

.grandma-media {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
  display: block;
  transform-origin: center bottom;
  /* transition はなし — JS (RAF) がリアルタイムで transform を直接制御 */
  will-change: transform;
}

/* talking animation is driven by JS (Web Audio API amplitude) */

.grandma-media.hidden { display: none; }

/* ===== Placeholder ===== */
.photo-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 10px;
  padding: 16px;
  text-align: center;
}

.placeholder-icon {
  font-size: 56px;
  line-height: 1;
}

.photo-placeholder p {
  font-size: 14px;
  color: var(--text-light);
  line-height: 1.6;
}

/* ===== Name plate ===== */
.name-plate {
  background: var(--primary);
  color: #fff;
  padding: 6px 22px;
  border-radius: 20px;
  font-size: 15px;
  font-weight: bold;
  letter-spacing: 0.06em;
  box-shadow: 0 3px 10px rgba(201,113,77,0.4);
}

/* ===== Status ===== */
.status-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-light);
}

.status-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #4caf50;
  flex-shrink: 0;
  transition: background 0.3s;
}

.status-dot.loading  { background: #ff9800; animation: blink 0.9s infinite; }
.status-dot.speaking { background: #c9714d; animation: blink 0.7s infinite; }
.status-dot.error    { background: #f44336; }

@keyframes blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.25; }
}

/* ===== Chat Section ===== */
.chat-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

/* ===== Message bubbles ===== */
.message {
  display: flex;
  flex-direction: column;
  max-width: 88%;
  gap: 4px;
}

.grandma-msg { align-self: flex-start; }
.ojisan-msg  { align-self: flex-end; }

.msg-label {
  font-size: 13px;
  color: var(--text-light);
  padding: 0 6px;
}

.ojisan-msg .msg-label { text-align: right; }

.bubble {
  padding: 14px 18px;
  font-family: var(--font-chat);
  font-size: 18px;
  line-height: 1.85;
  word-break: break-word;
}

.grandma-msg .bubble {
  background: var(--grandma-bg);
  border: 1.5px solid var(--grandma-bdr);
  border-radius: 4px 20px 20px 20px;
  color: var(--text);
}

.ojisan-msg .bubble {
  background: var(--ojisan-bg);
  border: 1.5px solid var(--ojisan-bdr);
  border-radius: 20px 4px 20px 20px;
  color: #1a3557;
}

/* ===== Thinking animation ===== */
.thinking-bubble {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 16px 22px;
  min-width: 70px;
}

.thinking-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--primary-light);
  animation: dotBounce 1.3s ease-in-out infinite;
}
.thinking-dot:nth-child(2) { animation-delay: 0.2s; }
.thinking-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes dotBounce {
  0%, 80%, 100% { transform: translateY(0);    opacity: 0.4; }
  40%            { transform: translateY(-8px); opacity: 1;   }
}

/* ===== Input Area ===== */
.input-area {
  flex-shrink: 0;
  padding: 12px 16px 16px;
  border-top: 2px solid var(--grandma-bdr);
  background: rgba(255,255,255,0.7);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.message-input {
  flex: 1;
  padding: 13px 18px;
  border: 2px solid var(--grandma-bdr);
  border-radius: 26px;
  font-size: 18px;
  font-family: var(--font);
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
  -webkit-appearance: none;
}

.message-input:focus { border-color: var(--primary); }

.message-input::placeholder {
  color: #cbb;
  font-size: 16px;
}

.input-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.voice-status {
  font-size: 14px;
  color: var(--primary);
  min-height: 20px;
}

/* ===== Buttons ===== */
.btn {
  cursor: pointer;
  border: none;
  border-radius: 26px;
  font-family: var(--font);
  font-size: 17px;
  font-weight: bold;
  outline: none;
  white-space: nowrap;
  transition: all 0.18s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn:active { opacity: 0.75; transform: scale(0.97); }

.btn-upload {
  background: #fff;
  color: var(--primary);
  border: 2px solid var(--primary);
  padding: 9px 18px;
  font-size: 15px;
}

.btn-change {
  background: #fff;
  color: var(--text-light);
  border: 1.5px solid var(--accent);
  padding: 6px 14px;
  font-size: 13px;
}

.btn-voice {
  background: #fff;
  color: var(--primary);
  border: 2px solid var(--grandma-bdr);
  width: 52px;
  height: 52px;
  min-width: 52px;
  border-radius: 50%;
  font-size: 22px;
  padding: 0;
}

.btn-voice.recording {
  background: #fff0f0;
  border-color: #f44336;
  animation: recordPulse 0.9s ease-in-out infinite;
}

@keyframes recordPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(244,67,54,0.4); }
  50%       { box-shadow: 0 0 0 10px rgba(244,67,54,0); }
}

.btn-send {
  background: var(--primary);
  color: #fff;
  padding: 13px 22px;
  box-shadow: 0 3px 10px rgba(201,113,77,0.4);
  font-size: 17px;
}

.btn-send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn-clear {
  background: transparent;
  color: var(--text-light);
  font-size: 14px;
  font-weight: normal;
  padding: 4px 10px;
  border: 1px solid var(--grandma-bdr);
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(50,30,15,0.9);
  color: #fff;
  padding: 12px 28px;
  border-radius: 26px;
  font-size: 15px;
  z-index: 200;
  max-width: 90vw;
  text-align: center;
  animation: toastIn 0.3s ease;
  pointer-events: none;
}

.toast.hidden { display: none; }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(12px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Settings FAB ===== */
.settings-fab {
  position: fixed;
  top: 16px;
  right: 16px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255,255,255,0.85);
  border: 2px solid var(--grandma-bdr);
  font-size: 20px;
  cursor: pointer;
  z-index: 150;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px rgba(0,0,0,0.12);
  transition: transform 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.settings-fab:active { transform: scale(0.92); }

/* ===== Settings Overlay ===== */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 160;
}
.settings-overlay.hidden { display: none; }

/* ===== Settings Panel ===== */
.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: min(360px, 92vw);
  height: 100%;
  background: #fff8f3;
  border-left: 3px solid var(--grandma-bdr);
  z-index: 170;
  padding: 0 0 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 24px rgba(0,0,0,0.15);
  animation: slideIn 0.25s ease;
}
.settings-panel.hidden { display: none !important; }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 2px solid var(--grandma-bdr);
  background: linear-gradient(135deg, #fff8f3, #ffeedd);
  position: sticky;
  top: 0;
  z-index: 1;
}

.settings-header h2 {
  font-size: 18px;
  color: var(--primary);
}

.settings-close {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-light);
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 8px;
  -webkit-tap-highlight-color: transparent;
}
.settings-close:active { background: #eee; }

/* ===== Accordion ===== */
.accordion-section {
  border-bottom: 2px solid var(--grandma-bdr);
}

.accordion-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  background: linear-gradient(135deg, #fff8f3 0%, #ffeedd 100%);
  border: none;
  cursor: pointer;
  font-family: var(--font);
  font-size: 16px;
  font-weight: bold;
  color: var(--primary);
  text-align: left;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
  user-select: none;
}

.accordion-header:hover {
  background: linear-gradient(135deg, #fef0e0 0%, #fde0cc 100%);
}

.accordion-header:active { opacity: 0.82; }

.accordion-arrow {
  font-size: 13px;
  color: var(--primary-light);
  transition: transform 0.25s ease;
  flex-shrink: 0;
}

.accordion-section.open .accordion-arrow {
  transform: rotate(180deg);
}

.accordion-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.35s ease;
}

.accordion-body.open {
  max-height: 4000px;
}

/* ===== Setting groups ===== */
.setting-group {
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--grandma-bdr);
}

.setting-label {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 15px;
  font-weight: bold;
  color: var(--text);
  margin-bottom: 12px;
}

.setting-value {
  font-size: 13px;
  font-weight: normal;
  color: var(--primary);
}

/* ===== Gender toggle ===== */
.gender-toggle {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.gender-btn {
  flex: 1;
  padding: 9px 0;
  border: 2px solid var(--grandma-bdr);
  border-radius: 20px;
  background: #fff;
  font-family: var(--font);
  font-size: 14px;
  font-weight: bold;
  color: var(--text-light);
  cursor: pointer;
  transition: all 0.18s;
  -webkit-tap-highlight-color: transparent;
}

.gender-btn.active {
  background: var(--grandma-bg);
  border-color: var(--primary);
  color: var(--primary);
}

.gender-btn:active { transform: scale(0.96); }

/* ===== Relation buttons ===== */
.relation-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.relation-btn {
  background: #fff;
  border: 2px solid var(--grandma-bdr);
  border-radius: 12px;
  padding: 9px 4px;
  font-family: var(--font);
  font-size: 14px;
  font-weight: bold;
  color: var(--text);
  cursor: pointer;
  transition: all 0.18s;
  -webkit-tap-highlight-color: transparent;
}

.relation-btn:active { transform: scale(0.96); }

.relation-btn.active {
  background: var(--grandma-bg);
  border-color: var(--primary);
  color: var(--primary);
}

/* ===== Voice buttons ===== */
.voice-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.voice-btn {
  background: #fff;
  border: 2px solid var(--grandma-bdr);
  border-radius: 12px;
  padding: 10px 8px;
  font-family: var(--font);
  font-size: 15px;
  font-weight: bold;
  color: var(--text);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: all 0.18s;
  -webkit-tap-highlight-color: transparent;
}

.voice-btn:active { transform: scale(0.96); }

.voice-btn.active {
  background: var(--grandma-bg);
  border-color: var(--primary);
  color: var(--primary);
}

.voice-sub {
  font-size: 12px;
  font-weight: normal;
  color: var(--text-light);
}
.voice-btn.active .voice-sub { color: var(--primary-light); }

/* ===== Slider ===== */
.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.slider-hint {
  font-size: 13px;
  color: var(--text-light);
  white-space: nowrap;
  min-width: 24px;
}

.slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(to right, var(--primary-light), var(--primary));
  outline: none;
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 2px 8px rgba(201,113,77,0.4);
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--primary);
  border: none;
  cursor: pointer;
}

/* ===== Test button ===== */
.btn-test {
  margin: 18px 20px 6px;
  background: var(--primary);
  color: #fff;
  padding: 14px;
  border-radius: 26px;
  font-size: 16px;
  font-weight: bold;
  width: calc(100% - 40px);
  box-shadow: 0 3px 10px rgba(201,113,77,0.4);
  cursor: pointer;
  border: none;
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  transition: all 0.18s;
}
.btn-test:active  { opacity: 0.75; transform: scale(0.98); }
.btn-test:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.test-hint {
  font-size: 13px;
  color: var(--text-light);
  text-align: center;
  padding: 0 20px;
  line-height: 1.6;
}

/* mouth canvas is unused — JS animation drives photo transforms instead */
.mouth-canvas { display: none; }

/* ===== Photo lightbox ===== */
.photo-lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  animation: fadeIn 0.25s ease;
}

.photo-lightbox.hidden { display: none !important; }

.lightbox-inner {
  position: relative;
  display: inline-block;
  max-width: 92vw;
  max-height: 92vh;
}

.photo-lightbox img {
  display: block;
  max-width: 92vw;
  max-height: 92vh;
  object-fit: contain;
  border-radius: 14px;
  box-shadow: 0 8px 48px rgba(0,0,0,0.7);
  transform-origin: center center;
  will-change: transform, filter;
}

#lightboxCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 14px;
  pointer-events: none;
  will-change: contents;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* 写真フレームにカーソルを表示 */
.photo-frame.has-photo {
  cursor: zoom-in;
}

/* ===== Persona form ===== */
.persona-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
}

.persona-label {
  font-size: 13px;
  color: var(--text-light);
  font-weight: bold;
}

.persona-hint {
  font-size: 12px;
  color: var(--text-light);
  opacity: 0.8;
  margin-top: -2px;
  line-height: 1.4;
}

.persona-input {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--grandma-bdr);
  border-radius: 12px;
  font-size: 15px;
  font-family: var(--font);
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}
.persona-input:focus { border-color: var(--primary); }

.persona-textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--grandma-bdr);
  border-radius: 12px;
  font-size: 15px;
  font-family: var(--font);
  background: #fff;
  color: var(--text);
  outline: none;
  resize: vertical;
  line-height: 1.6;
  transition: border-color 0.2s;
}
.persona-textarea:focus { border-color: var(--primary); }

.btn-save-persona {
  width: 100%;
  margin-top: 6px;
  padding: 14px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 26px;
  font-size: 16px;
  font-weight: bold;
  font-family: var(--font);
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(201,113,77,0.4);
  transition: all 0.18s;
  -webkit-tap-highlight-color: transparent;
}
.btn-save-persona:active   { opacity: 0.75; transform: scale(0.98); }
.btn-save-persona:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* ===== Photo Gallery ===== */
.photo-gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 14px;
  min-height: 40px;
}

.gallery-empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-light);
  font-size: 14px;
  padding: 14px 0;
}

.gallery-empty.hidden { display: none; }

.gallery-thumb {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  overflow: hidden;
  border: 3px solid var(--grandma-bdr);
  cursor: pointer;
  transition: transform 0.18s;
  -webkit-tap-highlight-color: transparent;
}

.gallery-thumb:active { transform: scale(0.94); }

.gallery-thumb.active {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary);
}

.gallery-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.thumb-active-badge {
  position: absolute;
  top: 4px;
  left: 4px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
  pointer-events: none;
}

.thumb-delete {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
}

.thumb-delete:active { background: rgba(180,0,0,0.8); }

.btn-upload-gallery {
  display: block;
  width: 100%;
  text-align: center;
  padding: 12px;
}

/* ===== Utility ===== */
.hidden { display: none !important; }

/* ===== Tablet / Responsive ===== */
@media (max-width: 700px) {
  .main {
    flex-direction: column;
    overflow: auto;
  }

  .grandma-aside {
    width: 100%;
    border-right: none;
    border-bottom: 2px solid var(--grandma-bdr);
    padding: 16px;
  }

  .photo-wrapper {
    flex-direction: row;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
    justify-content: center;
  }

  .photo-frame {
    width: 140px;
    height: 180px;
  }

  .messages {
    max-height: 35vh;
  }

  .header-title h1 {
    font-size: 22px;
  }
}

@media (min-width: 701px) and (max-width: 1024px) {
  /* タブレット横向き最適化 */
  .grandma-aside {
    width: 230px;
    padding: 16px 10px;
  }

  .photo-frame {
    width: 185px;
    height: 238px;
  }
}
