/* ---------- Kid-friendly design system (simple, not “AI”) ---------- */
:root{
  --bg:#f6f7fb;
  --card:#ffffff;
  --text:#111827;
  --muted:#6b7280;
  --border:#e7e9f3;
  --shadow:0 8px 24px rgba(17,24,39,.06);
  --shadow-strong:0 16px 40px rgba(17,24,39,.10);

  --primary:#3b82f6;
  --primary-600:#2563eb;
  --success:#22c55e;
  --warning:#f59e0b;
  --danger:#ef4444;
  --purple:#7c3aed;

  --radius:16px;
  --radius-sm:12px;
}

*{margin:0;padding:0;box-sizing:border-box}
html,body{min-height:100%}
body{
  font-family:ui-rounded,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Arial,sans-serif;
  color:var(--text);
  background:
    radial-gradient(1200px 500px at 10% -10%, #dbeafe 0%, rgba(219,234,254,0) 60%),
    radial-gradient(900px 400px at 90% 0%, #e9d5ff 0%, rgba(233,213,255,0) 60%),
    var(--bg);
  background-repeat:no-repeat;
  background-attachment:scroll;
  min-height:100vh;
  padding:18px;
  -webkit-tap-highlight-color: transparent;
}

.hidden{display:none!important}
.container{max-width:1200px;margin:0 auto}

.card{
  background:var(--card);
  border:1px solid var(--border);
  padding:20px;
  border-radius:var(--radius);
  box-shadow:var(--shadow);
  margin-bottom:18px;
}

.header{
  display:flex;
  justify-content:space-between;
  align-items:center;
  gap:12px;
  flex-wrap:wrap;
  margin-bottom:14px;
}
h1{font-size:22px;letter-spacing:-.2px}
h2{font-size:18px;letter-spacing:-.2px}

.pill{
  display:inline-flex;
  align-items:center;
  gap:8px;
  padding:8px 12px;
  border:1px solid var(--border);
  border-radius:999px;
  background:#f8fafc;
  font-weight:700;
}

.user-menu{position:relative;display:inline-flex}
.user-pill{gap:10px}
.user-pill{
  cursor:pointer;
  appearance:none;
  -webkit-appearance:none;
  text-align:left;
}
.user-pill:focus-visible{
  outline:none;
  box-shadow:0 0 0 4px rgba(59,130,246,.18);
}
.user-pill:active{transform:translateY(1px)}
.user-menu-pop{
  position:absolute;
  right:0;
  top:calc(100% + 10px);
  min-width:180px;
  background:#fff;
  border:1px solid var(--border);
  border-radius:14px;
  box-shadow:var(--shadow-strong);
  padding:8px;
  z-index:50;
}
.menu-item{
  width:100%;
  text-align:left;
  border:none;
  background:transparent;
  padding:10px 10px;
  border-radius:12px;
  cursor:pointer;
  font-weight:900;
  color:var(--text);
}
.menu-item:hover{background:#f8fafc}
.menu-item.danger{color:#b91c1c}
.menu-item.danger:hover{background:#fff1f2}

.avatar{
  width:22px;
  height:22px;
  border-radius:999px;
  object-fit:cover;
  border:1px solid var(--border);
  background:#fff;
}

/* Buttons */
.btn{
  padding:10px 14px;
  border:none;
  border-radius:12px;
  cursor:pointer;
  font-weight:800;
  letter-spacing:.2px;
  transition:transform .05s ease, filter .15s ease, background .15s ease;
}
.btn:disabled{opacity:.5;cursor:not-allowed;transform:none}
.btn-icon{display:inline-flex;align-items:center;gap:8px}
.icon{width:18px;height:18px;flex:0 0 18px}
.btn:active{transform:translateY(1px)}
.btn-primary{background:var(--primary);color:#fff}
.btn-primary:hover{background:var(--primary-600)}
.btn-success{background:var(--success);color:#fff}
.btn-warning{background:var(--warning);color:#111827}
.btn-danger{background:var(--danger);color:#fff}
.btn-ghost{background:transparent;border:1px solid var(--border);color:var(--text)}

.btn-group{display:flex;gap:10px;flex-wrap:wrap;margin:10px 0 0}

/* Login */
.login-page{display:flex;align-items:center;justify-content:center;min-height:100vh}
.login-box{
  background:var(--card);
  border:1px solid var(--border);
  padding:26px;
  border-radius:22px;
  box-shadow:var(--shadow-strong);
  max-width:420px;
  width:100%;
}
.login-avatar-wrap{display:flex;justify-content:center;margin-bottom:12px}
.login-avatar{
  width:84px;
  height:84px;
  border-radius:999px;
  object-fit:cover;
  border:2px solid #fff;
  box-shadow:0 10px 24px rgba(17,24,39,.12);
  background:#fff;
}
.login-title{font-size:22px;font-weight:900;text-align:center;margin-bottom:12px}
.login-subtitle{font-size:13px;color:var(--muted);text-align:center;margin-bottom:18px}
.form-group{margin-bottom:14px}
.form-group label{display:block;margin-bottom:8px;font-weight:800}
.form-input{
  width:100%;
  padding:12px 12px;
  border:2px solid var(--border);
  border-radius:12px;
  font-size:14px;
  outline:none;
}
.form-input:focus{border-color:#bfdbfe;box-shadow:0 0 0 4px rgba(59,130,246,.15)}
.login-btn{width:100%;padding:12px;background:var(--primary);color:#fff;border:none;border-radius:12px;font-size:15px;font-weight:900;cursor:pointer}
.login-btn:hover{background:var(--primary-600)}
.error{background:#fff1f2;color:#9f1239;padding:12px;border-radius:12px;margin-bottom:12px;display:none;border:1px solid #fecdd3}
.error.show{display:block}

/* User + top actions */
.user-info{display:flex;align-items:center;gap:8px}
.logout-btn{background:var(--danger);color:#fff;border:none;padding:10px 12px;border-radius:12px;cursor:pointer;font-weight:900}

/* Dashboard tiles */
.dash-title{font-size:18px;font-weight:900;margin:6px 0 14px;text-align:center}
.dash-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:14px}
.tile{
  border:1px solid var(--border);
  border-radius:20px;
  padding:18px;
  background:#ffffff;
  box-shadow:var(--shadow);
  cursor:pointer;
  transition:transform .12s ease, box-shadow .12s ease;
  display:flex;
  align-items:center;
  gap:14px;
  min-height:92px;
}
.tile:hover{transform:translateY(-2px);box-shadow:var(--shadow-strong)}
.tile-icon{font-size:34px;line-height:1}
.tile-title{font-size:16px;font-weight:950}
.tile-desc{font-size:12px;color:var(--muted);margin-top:4px}
.tile-badge{margin-left:auto;font-weight:900;font-size:12px;color:#0f172a;background:#f1f5f9;border:1px solid var(--border);padding:6px 10px;border-radius:999px}
.tile.studies{background:linear-gradient(180deg,#ffffff 0%, #f0f7ff 100%)}
.tile.notes{background:linear-gradient(180deg,#ffffff 0%, #f6f0ff 100%)}
.tile.play{background:linear-gradient(180deg,#ffffff 0%, #fff7ed 100%)}
.tile.food{background:linear-gradient(180deg,#ffffff 0%, #fff1f2 100%)}

/* Studies */
.stats{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px;margin:12px 0 0}
.stat{background:#fafafa;border:1px solid var(--border);padding:14px;border-radius:14px;text-align:center}
.stat-num{font-size:22px;font-weight:950;color:var(--text)}
.stat-label{font-size:12px;margin-top:6px;color:var(--muted);font-weight:800}

.progress-wrap{margin-top:14px}
.progress-top{display:flex;justify-content:space-between;align-items:center;gap:10px;flex-wrap:wrap}
.progress-title{font-weight:950}
.progress-sub{color:var(--muted);font-weight:800;font-size:12px}
.progress-bar{height:14px;background:#eef2ff;border:1px solid var(--border);border-radius:999px;overflow:hidden;margin-top:10px}
.progress-fill{height:100%;background:linear-gradient(90deg,#60a5fa, #34d399);width:0%}

.filter-bar{display:flex;gap:10px;flex-wrap:wrap;align-items:center}
.filter-bar label{font-weight:900;color:var(--muted);font-size:12px}
.select{
  padding:10px 12px;
  border:2px solid var(--border);
  border-radius:12px;
  background:#fff;
  font-weight:800;
}

table{width:100%;border-collapse:separate;border-spacing:0;margin-top:12px;overflow:hidden;border:1px solid var(--border);border-radius:16px}
th,td{padding:12px 12px;text-align:left;border-bottom:1px solid var(--border);vertical-align:middle}
thead th{background:#f8fafc;color:#0f172a;font-weight:950}
tbody tr:nth-child(even){background:#fcfcfd}
tbody tr:hover{background:#f7fbff}
.sortable{cursor:pointer;user-select:none}
.sortable span{margin-left:6px;opacity:.9}

.studies-table-wrap{overflow-x:auto;-webkit-overflow-scrolling:touch}
.studies-cards{display:none;margin-top:12px;gap:12px}
.studies-empty{padding:14px;color:var(--muted);font-weight:800}
.study-card{
  border:1px solid var(--border);
  border-radius:18px;
  background:#ffffff;
  box-shadow:var(--shadow);
  padding:14px;
}
.study-card-top{display:flex;justify-content:space-between;gap:10px;align-items:flex-start}
.study-card-title{font-weight:950;font-size:15px;line-height:1.25}
.study-card-chapter{font-weight:950;color:var(--muted);margin-left:6px}
.study-card-meta{margin-top:6px;color:var(--muted);font-weight:800;font-size:12px}
.study-card-status{font-weight:900;font-size:12px;color:#0f172a;white-space:nowrap}
.study-card-actions{display:flex;gap:10px;align-items:center;flex-wrap:wrap;margin-top:12px}
.study-card-actions .status-select{flex:1;min-width:180px}
.study-card-note{
  margin-top:10px;
  padding:10px 12px;
  border-radius:14px;
  border:1px solid var(--border);
  background:#fbfcff;
  color:#0f172a;
  font-size:13px;
  line-height:1.4;
}

.status-select{padding:10px 10px;border:2px solid var(--border);border-radius:12px;font-size:13px;background:#fff;font-weight:800}
.action-wrap{display:flex;gap:10px;align-items:center;flex-wrap:wrap}
.btn-small{padding:10px 12px;border:none;border-radius:12px;cursor:pointer;font-size:13px;font-weight:900}
.note-btn{background:#ede9fe;color:#4c1d95;border:1px solid #ddd6fe}
.note-btn.has-note{background:#7c3aed;color:#fff;border-color:#7c3aed}

/* Play (Word Search) */
.play-layout{display:grid;grid-template-columns:360px 1fr;gap:16px;align-items:start}
.play-panel{border:1px solid var(--border);border-radius:18px;background:#fbfcff;padding:16px}
.play-title{font-weight:950;font-size:18px}
.play-subtitle{margin-top:6px;color:var(--muted);font-weight:800;font-size:12px}
.play-controls{display:flex;gap:10px;flex-wrap:wrap;align-items:center;margin-top:14px}
.play-label{font-weight:900;color:var(--muted);font-size:12px}
.play-words{margin-top:14px}
.ws-words{display:flex;gap:8px;flex-wrap:wrap;margin-top:10px}
.ws-word{padding:8px 10px;border-radius:999px;border:1px solid var(--border);background:#fff;font-weight:900;font-size:12px}
.ws-word.found{background:#dcfce7;border-color:#86efac;color:#065f46;text-decoration:line-through}
.play-learn{margin-top:14px}
.ws-meaning{margin-top:8px;padding:12px;border-radius:14px;border:1px solid var(--border);background:#fff;line-height:1.5}
.ws-meaning-title{font-weight:950}
.ws-meaning-text{margin-top:6px;color:var(--muted);font-weight:800;font-size:13px}
.ws-meaning-example{margin-top:10px;font-size:13px}

.play-board{border:1px solid var(--border);border-radius:18px;background:#fff;padding:16px}
.play-board{overflow-x:auto;-webkit-overflow-scrolling:touch}
.ws-grid{display:grid;gap:6px;touch-action:none;user-select:none;--ws-cell:42px;width:fit-content;max-width:100%;margin:0 auto}
.ws-cell{
  width:var(--ws-cell,42px);height:var(--ws-cell,42px);border-radius:12px;
  border:1px solid var(--border);
  display:flex;align-items:center;justify-content:center;
  font-weight:950;
  font-size:clamp(13px, calc(var(--ws-cell,42px) * .38), 18px);
  background:#ffffff;
}
.ws-cell:hover{background:#f8fafc}
.ws-cell.selecting{background:#dbeafe;border-color:#93c5fd}
.ws-cell.found{background:#dcfce7;border-color:#86efac}
.ws-status{margin-top:12px;color:var(--muted);font-weight:800;font-size:12px}

/* Modal */
.modal{position:fixed;inset:0;z-index:9999}
.modal.hidden{display:none!important}
.modal-backdrop{position:absolute;inset:0;background:rgba(15,23,42,.45)}
.modal-content{position:relative;background:#fff;border-radius:18px;max-width:560px;width:calc(100% - 36px);margin:70px auto 0;box-shadow:var(--shadow-strong);overflow:hidden;border:1px solid var(--border)}
.modal-header{display:flex;justify-content:space-between;align-items:flex-start;padding:16px 16px 10px;border-bottom:1px solid var(--border);gap:12px}
.modal-title{font-size:16px;font-weight:950}
.modal-subtitle{font-size:12px;color:var(--muted);margin-top:4px;font-weight:700}
.modal-close{border:none;background:transparent;font-size:18px;cursor:pointer;color:var(--muted)}
.modal-body{padding:14px 16px;display:flex;flex-direction:column;gap:10px}
.modal-label{font-weight:900;font-size:12px;color:var(--muted)}
.modal-select{padding:10px;border:2px solid var(--border);border-radius:12px;font-size:14px;font-weight:800}
.modal-textarea{width:100%;min-height:140px;padding:12px;border:2px solid var(--border);border-radius:12px;font-size:14px;resize:vertical;outline:none}
.modal-textarea:focus{border-color:#c7d2fe;box-shadow:0 0 0 4px rgba(124,58,237,.12)}
.modal-footer{display:flex;gap:10px;align-items:center;padding:12px 16px;border-top:1px solid var(--border);background:#fafafa}

/* Notes page */
.notes-toolbar{display:flex;gap:12px;align-items:center;flex-wrap:wrap;margin-bottom:12px}
.notes-search{flex:1;min-width:220px;padding:12px;border:2px solid var(--border);border-radius:14px;font-size:14px;background:#fff;outline:none}
.notes-search:focus{border-color:#bfdbfe;box-shadow:0 0 0 4px rgba(59,130,246,.15)}
.notes-layout{display:grid;grid-template-columns:340px 1fr;gap:16px;min-height:560px}
.notes-list{border:1px solid var(--border);border-radius:18px;overflow:auto;background:#fbfcff}
.notes-empty{padding:18px;text-align:center;color:var(--muted)}
.notes-empty-emoji{font-size:40px}
.notes-empty-title{font-weight:950;color:var(--text);margin-top:8px}
.notes-empty-sub{font-weight:800;font-size:12px;margin-top:6px}
.note-item{padding:12px 14px;border-bottom:1px solid var(--border);cursor:pointer}
.note-item:last-child{border-bottom:none}
.note-item.active{background:#eef2ff;border-left:4px solid var(--primary)}
.note-title{font-weight:950}
.note-preview{margin-top:6px;font-size:12px;color:var(--muted);line-height:1.35}
.note-time{margin-top:6px;font-size:11px;color:#94a3b8;font-weight:800}
.notes-editor{border:1px solid var(--border);border-radius:18px;display:flex;flex-direction:column;overflow:hidden;background:#fff}
.notes-editor-top{padding:14px;border-bottom:1px solid var(--border);background:#fbfcff}
.notes-back{display:none}
.notes-title{width:100%;padding:12px;border:2px solid var(--border);border-radius:14px;font-size:16px;font-weight:950;outline:none}
.notes-title:focus{border-color:#bfdbfe;box-shadow:0 0 0 4px rgba(59,130,246,.15)}
.notes-meta{margin-top:8px;font-size:12px;color:var(--muted);font-weight:800}
.notes-content{flex:1;padding:14px;border:none;outline:none;font-size:14px;resize:none;line-height:1.5}
.notes-actions{display:flex;gap:10px;align-items:center;padding:14px;border-top:1px solid var(--border);background:#fbfcff}

@media (max-width: 900px){
  body{padding:12px}
  /* Header: keep title left, user menu right (mobile) */
  .header{flex-direction:row;align-items:center;flex-wrap:nowrap}
  .header > :first-child{flex:1;min-width:0}
  .header > :last-child{flex:0 0 auto;justify-content:flex-end}
  h1{font-size:20px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
  .btn-group{flex-direction:column;align-items:stretch}
  .btn-group .btn{width:100%}
  .dash-grid{grid-template-columns:1fr}
  .stats{grid-template-columns:repeat(2,minmax(0,1fr))}
  .play-layout{grid-template-columns:1fr}
  .play-board{padding:12px}
  .ws-cell{border-radius:10px}
  .notes-layout{grid-template-columns:1fr}
  /* Notes list should not be artificially short on mobile */
  .notes-layout.mobile-list .notes-list{max-height:none;overflow:visible}
  .notes-actions{position:sticky;bottom:0;padding-bottom:calc(14px + env(safe-area-inset-bottom))}
  /* Notes: iOS Notes style (list -> editor) */
  .notes-layout.mobile-list .notes-editor{display:none}
  .notes-layout.mobile-editor .notes-list{display:none}
  .notes-back{display:inline-flex;margin-bottom:10px}
  .filter-bar{flex-direction:column;align-items:stretch}
  .select{width:100%}
  /* Prevent iOS Safari focus-zoom; improve tap targets */
  input,select,textarea{font-size:16px}
  .btn,.logout-btn,.btn-small{min-height:44px}
  .study-card-actions{flex-direction:column;align-items:stretch}
  .study-card-actions .status-select{min-width:0}
  .study-card-actions .btn-small{width:100%}
  .studies-table-wrap{display:none}
  .studies-cards{display:grid}
  table{display:block;overflow:auto}
  .modal-content{
    width:100%;
    max-width:none;
    margin:0;
    position:absolute;
    left:0;right:0;bottom:0;top:auto;
    border-radius:18px 18px 0 0;
  }
  .modal-body{max-height:min(62vh, 520px);overflow:auto}
  .modal-footer{padding-bottom:calc(12px + env(safe-area-inset-bottom))}
  /* stats page charts stack */
  #stats .card > div[style*="grid-template-columns"]{grid-template-columns:1fr!important}
}

@media print{
  body{background:#fff}
  .btn,.btn-group,.logout-btn,.filter-bar,.notes-toolbar{display:none!important}
}
