*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #eeeef6;
  --surface: #ffffff;
  --border: #d4d4e4;
  --accent: #7c6af7;
  --accent-hover: #6557e8;
  --text: #1a1a2e;
  --text-muted: #6b6b80;
  --danger: #d94050;
  --danger-hover: #c03040;
  --radius: 10px;
}

html, body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

.app {
  max-width: 680px;
  margin: 0 auto;
  padding: 2rem 1.25rem 4rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

/* Header */
header {
  text-align: center;
}

h1 {
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--accent);
}

.hint {
  margin-top: 0.4rem;
  font-size: 0.82rem;
  color: var(--text-muted);
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.78rem;
  background: var(--surface);
  color: var(--text);
}

/* Main */
main {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

textarea {
  width: 100%;
  min-height: 130px;
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1.05rem;
  font-family: inherit;
  resize: vertical;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.35s ease-out;
}

textarea:focus {
  border-color: var(--accent);
}

textarea::placeholder {
  color: var(--text-muted);
}

/* Controls */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 1rem;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
  min-width: 140px;
}

.control-group label {
  font-size: 0.78rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

select {
  appearance: none;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236b6b80' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 0.75rem center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
  padding: 0.5rem 2rem 0.5rem 0.75rem;
  cursor: pointer;
  outline: none;
  width: 100%;
  transition: border-color 0.2s;
}

select:focus {
  border-color: var(--accent);
}

input[type="range"] {
  width: 100%;
  height: 4px;
  accent-color: var(--accent);
  cursor: pointer;
  margin-top: 0.25rem;
}

.btn-stop {
  padding: 0.5rem 1.2rem;
  min-width: 130px;
  background: transparent;
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  color: var(--danger);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
  align-self: flex-end;
}

.btn-stop:hover {
  background: var(--danger);
  color: #fff;
}

.btn-stop.speaking {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-stop.speaking:hover {
  background: rgba(124, 106, 247, 0.15);
}

/* History */
.history-section {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h2 {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
}

.btn-clear {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}

.btn-clear:hover {
  color: var(--danger-hover);
  background: rgba(224, 92, 106, 0.1);
}

#history-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

#history-list li {
  padding: 0.6rem 0.9rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.92rem;
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: border-color 0.15s, background 0.15s;
}

#history-list li:hover {
  border-color: var(--accent);
  background: rgba(124, 106, 247, 0.08);
}

.history-empty {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  padding: 1rem 0;
}

/* Autocomplete */

.input-wrapper {
  position: relative;
}

.input-wrapper.open textarea {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-bottom-color: transparent;
}

.suggestions {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  list-style: none;
  padding: 0.3rem 0;
  margin: 0;
  z-index: 10;
  display: none;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

.input-wrapper.open .suggestions {
  display: block;
}

.suggestions li {
  padding: 0.5rem 1rem;
  cursor: pointer;
  font-size: 0.92rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.1s;
}

.suggestions li:hover {
  background: rgba(124, 106, 247, 0.12);
}

.suggestions li.active {
  background: rgba(124, 106, 247, 0.22);
  color: var(--accent-hover);
}

/* Quick phrases panel */

.phrases-section {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.phrases-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.phrases-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.btn-phrase {
  padding: 0.4rem 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text);
  font-size: 0.87rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.15s, background 0.15s, color 0.15s;
}

.btn-phrase:hover {
  border-color: var(--accent);
  background: rgba(124, 106, 247, 0.1);
  color: var(--accent-hover);
}

.btn-phrase:active {
  background: rgba(124, 106, 247, 0.22);
}

.btn-edit-phrases {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}

.btn-edit-phrases:hover {
  color: var(--accent-hover);
  background: rgba(124, 106, 247, 0.1);
}

/* Modal */

.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1rem;
}

.modal.open {
  display: flex;
}

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 460px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h3 {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.btn-modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  line-height: 1;
  transition: color 0.15s, background 0.15s;
}

.btn-modal-close:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
}

.phrases-edit-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 0.4rem 0;
}

.phrases-edit-list li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 1.25rem;
  transition: background 0.1s;
}

.phrases-edit-list li:hover {
  background: rgba(255, 255, 255, 0.03);
}

.pe-text {
  flex: 1;
  font-size: 0.9rem;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pe-actions {
  display: flex;
  gap: 0.2rem;
  flex-shrink: 0;
}

.pe-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.75rem;
  padding: 2px 7px;
  line-height: 1.5;
  transition: color 0.1s, border-color 0.1s, background 0.1s;
}

.pe-btn:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--accent);
}

.pe-btn:disabled {
  opacity: 0.25;
  cursor: default;
}

.pe-delete:hover:not(:disabled) {
  color: var(--danger);
  border-color: var(--danger);
  background: rgba(224, 92, 106, 0.08);
}

.modal-add {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-add input {
  flex: 1;
  padding: 0.45rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}

.modal-add input:focus {
  border-color: var(--accent);
}

.modal-add input::placeholder {
  color: var(--text-muted);
}

.btn-add {
  padding: 0.45rem 0.9rem;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: #fff;
  font-size: 0.88rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}

.btn-add:hover {
  background: var(--accent-hover);
}

.modal-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-restore {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  font-family: inherit;
  transition: color 0.15s, background 0.15s;
}

.btn-restore:hover {
  color: var(--danger-hover);
  background: rgba(224, 92, 106, 0.1);
}

.btn-save {
  padding: 0.45rem 1.2rem;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: #fff;
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-save:hover {
  background: var(--accent-hover);
}

/* Speaking indicator */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

body.speaking textarea {
  border-color: var(--accent);
  animation: pulse 1.4s ease-in-out infinite;
}
