:root {
  --font-sans: system-ui, -apple-system, Segoe UI, Roboto, Noto Sans Thai, Noto Sans, Helvetica, Arial, sans-serif;

  /* Sizing & Radius */
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-full: 999px;

  /* Shadows */
  --shadow-sm: 0 4px 12px -8px color-mix(in oklab, black, transparent 80%);
  --shadow-md: 0 8px 24px -16px color-mix(in oklab, black, transparent 85%);

  /* Light Theme */
  --bg: #f8f9fa;
  --fg: #212529;
  --muted: #6c757d;
  --surface: #ffffff;
  --card: #ffffff;
  --border: #dee2e6;

  --ok: #198754;
  --err: #dc3545;
  --current: #0d6efd;
  --accent: #fd7e14;
  --ring: #86b7fe;
}

[data-theme="dark"] {
  --bg: #121212;
  --fg: #e9ecef;
  --muted: #adb5bd;
  --surface: #1e1e1e;
  --card: #2f2f2f;
  --border: #495057;

  --ok: #20c997;
  --err: #fd7888;
  --current: #4dabf7;
  --accent: #ff922b;
  --ring: #3682f1;
}

.panel {
  background: var(--card);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.btn {
  font-family: inherit;
  font-size: 1rem;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--fg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn:hover { background: color-mix(in oklab, var(--fg), transparent 92%); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .6; cursor: not-allowed; }

.btn-primary {
  background: color-mix(in oklab, var(--current), transparent 88%);
  border-color: var(--ring);
}
.btn-primary:hover {
  background: color-mix(in oklab, var(--current), transparent 82%);
}
.btn-primary:disabled {
  background: var(--surface);
  border-color: var(--border);
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-start {
  font-size: 1.1rem;
  font-weight: 600;
  padding: 10px 24px;
}

.btn-icon {
  padding: 6px 8px;
  font-size: 1.1rem;
  line-height: 1;
  margin-left: -4px;
}


* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; font-family: var(--font-sans);
  background-color: var(--bg);
  color: var(--fg);
  line-height: 1.5;
  transition: background-color 0.2s, color 0.2s;
}

.app-header {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 16px 20px; position: sticky; top: 0; backdrop-filter: saturate(120%) blur(6px);
  background: color-mix(in oklab, var(--surface), transparent 15%);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { margin: 0; font-size: 1.25rem; color: var(--muted); }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: .9rem; }
.history-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); color: var(--fg); font-size: 1.2rem; font-weight: 600;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.history-btn:hover { background: color-mix(in oklab, var(--fg), transparent 92%); }
.header-actions { display: flex; align-items: center; gap: 8px; }
.lang-selector { display: flex; align-items: center; gap: 4px; }

.theme-toggle {
  font-size: 1.2rem; background: none; border: none; cursor: pointer; padding: 4px; border-radius: var(--radius-sm); line-height: 1;
}
.theme-toggle:hover { background: color-mix(in oklab, var(--fg), transparent 90%); }


.app-main { max-width: 1000px; margin: 0 auto; padding: 16px 20px 28px; display: grid; gap: 14px; }

.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px;
  padding: 10px;
  border-radius: var(--radius-md);
}
.toolbar-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px 12px;
}
select, button { font-family: inherit; }

.toolbar select {
  font-size: 1rem;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--fg);
}

.hidden { display: none; }

.start-area {
  display: flex;
  justify-content: center;
}

.custom-form { display: grid; gap: 10px; padding: 16px 0; }
.custom-form label { font-size: .9rem; color: var(--muted); }
.criteria-container { display: flex; justify-content: space-between; align-items: baseline; padding: 0 4px; }
#wordCountIndicator { font-size: .85rem; color: var(--muted); }
.criteria-indicator { font-size: .85rem; color: var(--muted); min-height: 1.2em; }
.bypass-validation { display: flex; align-items: center; gap: 6px; font-size: .9rem; }
.custom-form input, .custom-form textarea {
  width: 100%; font-family: inherit; font-size: 1rem; padding: 8px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg); color: var(--fg);
}
.custom-form input[type="checkbox"] { width: auto; }
.custom-form input:focus, .custom-form textarea:focus { outline: 3px solid var(--ring); border-color: var(--current); }


.board {
  position: relative;
  padding: 18px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
}
.board::after {
  content: '';
  position: absolute;
  bottom: 18px;
  left: 18px;
  right: 18px;
  height: 2.1rem;
  background: linear-gradient(to bottom, transparent, var(--card) 90%);
  pointer-events: none;
}
.text-display { font-size: 1.45rem; line-height: 2.1rem; word-wrap: break-word; word-break: break-word; max-height: 6.5rem; overflow-y: auto; scrollbar-width: none; }
.text-display::-webkit-scrollbar { display: none; }
.text-display span { padding: 0 1px; border-radius: var(--radius-xs); transition: background-color 0.15s, color 0.15s; }
.text-display .done.ok { color: var(--ok); }
.text-display .done.err { background-color: color-mix(in oklab, var(--err), transparent 85%); text-decoration: underline; text-decoration-color: var(--err); text-decoration-thickness: 2px; text-underline-offset: 3px; }
.text-display .current { position: relative; background: color-mix(in oklab, var(--current), transparent 85%); outline: 1px solid var(--current); }
.text-display .current::after { content: ''; position: absolute; right: -1px; top: 15%; bottom: 15%; width: 2px; background: var(--current); animation: caret 1s steps(1) infinite; border-radius: 1px; }
@keyframes caret { 50% { opacity: 0; } }

.progress { height: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-full); overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--current), var(--accent)); transition: width .2s ease; }

.input-area { display: flex; }
.typing-input {
  width: 100%; font-size: 1.2rem; padding: 14px; border-radius: var(--radius-md); border: 1px solid var(--border); background: var(--surface); color: var(--fg);
  box-shadow: 0 4px 16px -12px color-mix(in oklab, black, transparent 85%);
}
.typing-input:focus { outline: 3px solid var(--ring); border-color: var(--current); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.stat {
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  border-radius: var(--radius-md);
}
.stat .label { color: var(--muted); font-size: 0.85rem; }
.stat span:last-child { font-weight: 600; font-size: 1.15rem; }

.toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background-color: var(--card);
  color: var(--fg);
  padding: 12px 20px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  z-index: 200;
  transition: opacity 0.3s ease;
}

.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  opacity: 0; transition: opacity 0.2s ease-in-out;
}
.modal-overlay:not(.hidden) { opacity: 1; }
.modal-overlay.hidden { display: none; }

.modal-content {
  padding: 24px;
  border-radius: var(--radius-lg);
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
  width: 90%; max-width: 480px;
  transform: scale(0.95); transition: transform 0.2s ease-in-out;
}
.modal-content.wide { max-width: 720px; }
.modal-overlay:not(.hidden) .modal-content { transform: scale(1); }

.modal-content h2 { margin-top: 0; text-align: center; }
.results-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 20px 0; }
.modal-actions { display: flex; justify-content: center; gap: 12px; margin-top: 16px; }

.history-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.summary-group {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 12px;
  border-radius: var(--radius-md);
}
.summary-group h3 {
  margin: 0 0 10px;
  font-size: 1.1rem;
}
.summary-stat {
  font-size: 0.9rem;
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}
.summary-stat .label { color: var(--muted); }
.summary-stat .value { font-weight: 600; }
.best-record {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}
.best-record .title { font-size: 0.9rem; font-weight: 600; }
.best-record .details { font-size: 0.8rem; color: var(--muted); }


.history-table-container {
  max-height: 40vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.history-table {
  width: 100%;
  border-collapse: collapse;
}
.history-table th, .history-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.history-table th {
  position: sticky;
  top: 0;
  background: var(--surface);
  font-size: 0.9rem;
}
.history-table tbody tr:last-child td { border-bottom: none; }
.history-table td { font-size: 0.95rem; }
.history-table td:nth-child(3), .history-table td:nth-child(4) { font-weight: 600; text-align: right; }
.history-table th:nth-child(3), .history-table th:nth-child(4) { text-align: right; }

#historyTitle span[contenteditable="true"] {
  cursor: text;
  padding: 0 4px;
  border-radius: var(--radius-xs);
  outline: none;
}
#historyTitle span[contenteditable="true"]:hover {
  background-color: color-mix(in oklab, var(--fg), transparent 95%);
}
#historyTitle span[contenteditable="true"]:focus {
  background-color: var(--surface);
  box-shadow: 0 0 0 2px var(--ring);
}

@media (max-width: 600px) {
  .text-display { font-size: 1.2rem; line-height: 1.9rem; }
  .results-grid { grid-template-columns: 1fr; }
  .history-summary { grid-template-columns: 1fr; }
}
