/*
 * Port of res/layout/activity_main.xml and the shape drawables.
 *
 * Colours, corner radii and stroke widths are taken verbatim from the Android
 * resources, and dp are treated as px — the layout is phone-first, so the whole
 * thing sits in a phone-width column and simply centres itself on a desktop.
 */

:root {
  --bg: #12121E;
  --card-score: #1A1A2E;
  --card-chat: #16162A;
  --stroke: #2A2A44;
  --accent: #7C4DFF;
  --accent-2: #448AFF;
  --indicator: #B39DFF;
  --text: #ECEFF1;
  --muted: #9FA8DA;
  --muted-2: #6E7A9A;
  --gold: #FFD54F;
  --cyan: #4FC3F7;

  /* Sizes the desktop layout scales up. --max-bubbles is read by ui.js, which
     trims the chat to whatever the current layout can show. */
  --die-size: 56px;
  --dice-gap: 6px;
  --cell-h: 44px;
  --cell-lbl: 9px;
  --cell-val: 13px;
  --bubble-max: 300px;
  --max-bubbles: 4;
}

* { box-sizing: border-box; }

/* The UA rule for [hidden] is display:none at specificity (0,1,0), so any class
   here that sets display silently beats it — which left the absolutely
   positioned progress overlay sitting on top of the action button, eating every
   click. Settle it once for the whole sheet. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Roboto, "Segoe UI", system-ui, -apple-system, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  justify-content: center;
  min-height: 100dvh;
}

.root {
  width: 100%;
  max-width: 480px;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 12px;
  /* Matches the edge-to-edge inset handling in onCreate. */
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  padding-bottom: 8px;
}

.avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: #fff;
  font-weight: 700;
  flex: none;
  /* bg_avatar.xml: angle 135 starts the gradient at the bottom-right corner. */
  background: linear-gradient(to top left, var(--accent), var(--accent-2));
}
.avatar-lg { width: 44px; height: 44px; font-size: 22px; }
.avatar-sm { width: 34px; height: 34px; font-size: 17px; }

.title {
  flex: 1;
  margin: 0;
  padding-left: 10px;
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
}

.turn-pill {
  background: var(--accent);
  border-radius: 24px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
}

.icon-btn {
  width: 36px;
  height: 36px;
  margin-left: 6px;
  padding: 6px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  flex: none;
}
.icon-btn svg { width: 100%; height: 100%; fill: currentColor; display: block; }
.icon-btn:hover { background: #ffffff14; }
.icon-btn:active { background: #ffffff22; }

/* ---------- Standings ---------- */

.standings {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 2px 0 6px;
  scrollbar-width: none;
}
.standings::-webkit-scrollbar { display: none; }

.chip {
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex: none;
  padding: 5px 11px;
  border: 1px solid var(--stroke);
  border-radius: 16px;
  background: var(--card-score);
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color .2s, background-color .2s, color .2s;
}
.chip .pts { font-weight: 700; color: var(--text); }
.chip:hover { border-color: #4A4A6E; }
/* Whose sheet is on screen. */
.chip[aria-selected="true"] { background: #23234099; border-color: var(--indicator); color: var(--text); }
/* Whose turn it is — a left marker, so it does not rely on colour alone. */
.chip.turn::before {
  content: "▸";
  color: var(--indicator);
  font-weight: 700;
}
.chip.didi .pts { color: var(--gold); }
.chip.done { opacity: .55; }

/* ---------- Cards ---------- */

.card {
  border: 1px solid var(--stroke);
}

.scorecard {
  background: var(--card-score);
  border-radius: 16px;
  padding: 10px;
  margin: 4px 0;
}

.card-label {
  margin: 0 0 6px 2px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--muted-2);
}

/* ---------- Scoreboard ---------- */

.score-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.score-cell {
  height: var(--cell-h);
  padding: 3px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  /* bg_score_empty.xml */
  background: #1F1F36;
  border: 1px solid #34345A;
  transition: background-color .2s, border-color .2s;
}
.score-cell.filled { background: #1B5E20; border-color: #66BB6A; }  /* bg_score_filled */
.score-cell.zero   { background: #3E2723; border-color: #8D6E63; }  /* bg_score_zero */

.score-cell .lbl {
  font-size: var(--cell-lbl);
  color: #8E99C9;
  line-height: 1.1;
  text-align: center;
}
.score-cell .val {
  font-size: var(--cell-val);
  font-weight: 700;
  color: #607D8B;
  line-height: 1.1;
}
.score-cell.filled .val { color: #A5D6A7; }
.score-cell.zero   .val { color: #BCAAA4; }
.score-cell .val.bonus  { color: var(--gold); }

.total-row {
  display: flex;
  align-items: center;
  padding-top: 6px;
}
.total-label { flex: 1; padding-left: 4px; font-size: 12px; color: var(--muted-2); }
.total-value { padding-right: 4px; font-size: 14px; font-weight: 700; color: var(--gold); }

/* ---------- Dice ---------- */

.dice-row {
  display: flex;
  justify-content: center;
  gap: var(--dice-gap);
  padding: 12px 0 6px;
}

.die {
  position: relative;
  width: var(--die-size);
  height: var(--die-size);
  border-radius: 10px;
  /* bg_dice.xml */
  background: #FAFAFA;
  border: 1px solid #B0BEC5;
  transition: background-color .18s, border-color .18s, transform .18s;
}
/* bg_dice_kept.xml — 3dp stroke, so compensate the border box to keep the size. */
.die.kept {
  background: #E1F5FE;
  border: 3px solid #29B6F6;
}

/*
 * Pips, shared by the dice on the table and the faces on the input pad.
 *
 * Defined once on purpose. These rules were originally scoped to `.die`, with
 * the pad given its own copy — which carried the size and colour but not the
 * positions, so every pad face rendered its pips on top of each other and read
 * as a 1. Percentages mean the same rules serve both sizes.
 */
.pip {
  position: absolute;
  width: 14.286%;   /* 8dp of 56dp */
  height: 14.286%;
  border-radius: 50%;
  background: #212121;
}
/* Pip centres sit at 14/28/42 of 56dp, i.e. offsets 10/24/38. */
.pip.c0 { left: 17.857%; }
.pip.c1 { left: 42.857%; }
.pip.c2 { left: 67.857%; }
.pip.r0 { top: 17.857%; }
.pip.r1 { top: 42.857%; }
.pip.r2 { top: 67.857%; }
/* The kept border eats 2px on each side; nudge the pips back onto the same grid. */
.die.kept .pip { margin: -2px 0 0 -2px; }

@keyframes roll {
  0%   { transform: rotate(0deg)   scale(1); }
  35%  { transform: rotate(-11deg) scale(1.09); }
  70%  { transform: rotate(8deg)   scale(1.04); }
  100% { transform: rotate(0deg)   scale(1); }
}
.die.rolling { animation: roll .42s ease-out; }

.keep-line {
  margin: 0;
  padding-bottom: 6px;
  min-height: 18px;
  text-align: center;
  font-size: 12px;
  color: var(--cyan);
}

/* ---------- Dice pad ---------- */

.dice-pad {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding-bottom: 8px;
}

.pad-faces { display: flex; gap: 6px; }

.pad-face {
  position: relative;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 8px;
  background: #FAFAFA;
  border: 1px solid #B0BEC5;
  cursor: pointer;
  flex: none;
  transition: transform .12s, box-shadow .12s;
}
.pad-face:hover { transform: translateY(-2px); box-shadow: 0 3px 10px #0006; }
.pad-face:active { transform: translateY(0); }
.pad-face:disabled { opacity: .35; cursor: default; transform: none; box-shadow: none; }

.pad-undo {
  padding: 8px 12px;
  border: 1px solid var(--stroke);
  border-radius: 16px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.pad-undo:hover:not(:disabled) { background: #ffffff14; }
.pad-undo:disabled { opacity: .35; cursor: default; }

/* A slot still waiting for a value. */
.die.empty { background: #23233C; border-style: dashed; border-color: #4A4A6E; }

/* ---------- Score cells offered to a human ---------- */

.score-cell.playable {
  cursor: pointer;
  border-color: var(--indicator);
  background: #262647;
}
.score-cell.playable:hover { background: #303058; }
.score-cell.playable .val { color: var(--indicator); }
/* A slot worth nothing is still legal — say so rather than hide it. */
.score-cell.playable.nil .val { color: #8D7B76; }

.spacer { flex: 1; min-height: 4px; }

/* ---------- Chat ---------- */

.chatcard {
  background: var(--card-chat);
  border-radius: 20px;
  padding: 10px 12px;
}

.chat-header {
  display: flex;
  align-items: center;
  padding-bottom: 8px;
}
.chat-who { display: flex; flex-direction: column; padding-left: 10px; }
.chat-name { font-size: 15px; font-weight: 700; color: var(--text); }
.chat-status { font-size: 11px; color: #7BD389; }

.chat-sep { height: 1px; background: #22223A; }

.chat-scroll {
  height: 180px;
  overflow-y: auto;
  padding: 6px 0 2px;
  scrollbar-width: none;
}
.chat-scroll::-webkit-scrollbar { display: none; }

.chat-container { display: flex; flex-direction: column; }

.chat-item { padding: 4px 2px; }

.bubble {
  display: inline-block;
  max-width: var(--bubble-max);
  padding: 10px 14px;
  font-size: 16px;
  line-height: 1.35;
  color: var(--text);
  /* bg_bubble_didi.xml */
  background: linear-gradient(to top left, #2E2E4A, #3A3A5C);
  border: 1px solid #4A4A6E;
  border-radius: 6px 20px 20px 20px;
}

.chat-divider {
  display: flex;
  align-items: center;
  padding: 10px 0 6px;
}
.chat-divider::before,
.chat-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: #3A3A5C;
}
.chat-divider span {
  padding: 0 10px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--muted);
}

/* Mirrors the 220ms fade + 8px slide the activity runs on each new entry. */
@keyframes enter {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.chat-item, .chat-divider { animation: enter .22s ease-out; }

/* ---------- Action slot ---------- */

.action-slot {
  position: relative;
  height: 64px;
  padding: 8px 0 4px;
  flex: none;
}

.action-btn {
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 28px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  transition: filter .15s, opacity .15s;
}
.action-btn:hover:not(:disabled) { filter: brightness(1.12); }
.action-btn:active:not(:disabled) { filter: brightness(.92); }
.action-btn:disabled { opacity: .5; cursor: default; }

.loading-linear {
  position: absolute;
  inset: 8px 0 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.progress-text { font-size: 12px; color: var(--muted); padding-bottom: 6px; }
.progress-track {
  width: calc(100% - 48px);
  height: 6px;
  border-radius: 3px;
  background: var(--stroke);
  overflow: hidden;
}
.progress-fill {
  width: 0;
  height: 100%;
  border-radius: 3px;
  background: var(--indicator);
  transition: width .15s linear;
}

.loading-circular {
  position: absolute;
  inset: 0;
  margin: auto;
  width: 36px;
  height: 36px;
  border: 4px solid var(--stroke);
  border-top-color: var(--indicator);
  border-radius: 50%;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Switches ---------- */

.switch-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-top: 6px;
  flex: none;
}
.switch-label { font-size: 13px; color: var(--muted); padding-right: 6px; cursor: pointer; }
.switch-gap { width: 16px; }

.switch {
  appearance: none;
  width: 46px;
  height: 26px;
  margin: 0;
  border: 2px solid #4A4A6E;
  border-radius: 13px;
  background: #2A2A44;
  position: relative;
  cursor: pointer;
  flex: none;
  transition: background-color .2s, border-color .2s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 4px;
  width: 14px;
  height: 14px;
  margin-top: -7px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform .2s, width .2s, height .2s, margin .2s, background-color .2s;
}
.switch:checked { background: var(--accent); border-color: var(--accent); }
.switch:checked::after {
  width: 18px;
  height: 18px;
  margin-top: -9px;
  background: #fff;
  transform: translateX(20px);
}

/* ---------- Dialogs ---------- */

.dlg {
  border: 1px solid var(--stroke);
  border-radius: 20px;
  background: #1E1E34;
  color: var(--text);
  padding: 20px;
  width: min(400px, calc(100vw - 48px));
  font-family: inherit;
}
.dlg::backdrop { background: #000a; }

.dlg-title { margin: 0 0 10px; font-size: 19px; font-weight: 700; }
.dlg-msg { margin: 0 0 14px; font-size: 14px; color: var(--muted); line-height: 1.4; }
.dlg-note { margin: 14px 0 0; font-size: 12px; color: var(--muted-2); line-height: 1.4; }

.dlg-input, .dlg-select {
  width: 100%;
  padding: 10px 12px;
  font-size: 20px;
  font-family: inherit;
  color: var(--text);
  background: #16162A;
  border: 1px solid #4A4A6E;
  border-radius: 10px;
}
.dlg-select { font-size: 14px; }
.dlg-input:focus, .dlg-select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.dlg-error { margin: 8px 0 0; font-size: 13px; color: #EF9A9A; }

.field-label { font-size: 13px; color: var(--muted); }
.field-head { display: flex; justify-content: space-between; align-items: baseline; margin-top: 16px; }
.field-value { font-size: 13px; font-weight: 700; color: var(--text); }

.dlg-slider {
  width: 100%;
  margin: 8px 0 0;
  accent-color: var(--accent);
}

/* ---------- Setup ---------- */

.setup-list { display: flex; flex-direction: column; gap: 8px; }

.setup-row { display: flex; gap: 8px; align-items: center; }
.setup-row .dlg-input { font-size: 15px; }

.setup-remove {
  width: 34px;
  height: 34px;
  flex: none;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.setup-remove:hover { background: #ffffff14; color: #EF9A9A; }

.setup-add {
  margin-top: 10px;
  padding: 8px 12px;
  border: 1px dashed #4A4A6E;
  border-radius: 10px;
  background: transparent;
  color: var(--indicator);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  width: 100%;
}
.setup-add:hover:not(:disabled) { background: #ffffff0d; }
.setup-add:disabled { opacity: .4; cursor: default; }

.dlg-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 20px 0 0;
  padding: 0;
}
.dlg-spacer { flex: 1; }

.dlg-btn {
  padding: 10px 16px;
  border: 0;
  border-radius: 20px;
  background: transparent;
  color: var(--indicator);
  font-size: 14px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
}
.dlg-btn:hover { background: #ffffff14; }
.dlg-btn.primary { background: var(--accent); color: #fff; }
.dlg-btn.primary:hover { filter: brightness(1.12); background: var(--accent); }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  transform: translateX(-50%);
  z-index: 10;
  padding: 12px 20px;
  border-radius: 24px;
  background: #2E2E4A;
  border: 1px solid #4A4A6E;
  color: var(--text);
  font-size: 14px;
  box-shadow: 0 6px 24px #0008;
  animation: enter .2s ease-out;
}

/* Short window on the single-column layout: give the chat less room rather than
   scrolling the page. The two conditions are the inverse of the desktop query
   below, so this can never fight it: either the window is too narrow for two
   columns, or it is too short for them. */
@media (max-width: 939px) and (max-height: 720px),
       (max-height: 559px) {
  .chat-scroll { height: 120px; }
  .dice-row { padding-top: 8px; }
}

/* ---------- Desktop ----------
 *
 * Below this the layout is the phone one, unchanged. Above it the single
 * column splits in two: the board on the left, the chat as a full-height panel
 * on the right. The height condition keeps a short window (a half-screen split,
 * a laptop with devtools open) on the phone layout rather than squashing two
 * columns into it.
 *
 * Every element is already a direct child of .root, so this is a grid
 * assignment with no change to the markup.
 */
@media (min-width: 940px) and (min-height: 560px) {
  :root {
    --die-size: 76px;
    --dice-gap: 10px;
    --cell-h: 58px;
    --cell-lbl: 11px;
    --cell-val: 17px;
    --bubble-max: 380px;
    --max-bubbles: 12;
  }

  body { align-items: center; }

  .root {
    display: grid;
    width: min(100%, 1120px);
    max-width: none;
    height: min(100dvh, 880px);
    min-height: 0;
    padding: 20px;
    column-gap: 20px;
    grid-template-columns: minmax(0, 1.1fr) minmax(330px, 0.9fr);
    grid-template-rows: auto auto auto auto auto auto 1fr auto auto;
    grid-template-areas:
      "topbar    topbar"
      "standings chat"
      "score     chat"
      "dice      chat"
      "keep      chat"
      "pad       chat"
      "spacer    chat"
      "action    chat"
      "switch    chat";
  }

  .topbar      { grid-area: topbar; padding-bottom: 12px; }
  .standings   { grid-area: standings; flex-wrap: wrap; overflow: visible; }
  .scorecard   { grid-area: score; margin: 0 0 4px; padding: 14px; }
  .dice-row    { grid-area: dice; }
  .keep-line   { grid-area: keep; }
  .dice-pad    { grid-area: pad; }
  .spacer      { grid-area: spacer; }
  .chatcard    { grid-area: chat; }
  .action-slot { grid-area: action; }
  .switch-row  { grid-area: switch; }

  .chip { font-size: 13px; padding: 6px 13px; }
  .pad-face { width: 48px; height: 48px; }

  /* The card is a grid item spanning six rows, so its height is fixed by the
     grid and the message area can simply take what is left. */
  .chatcard {
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 14px 16px;
  }
  .chat-scroll {
    height: auto;
    flex: 1;
    min-height: 0;
  }
  /* Anchor the conversation to the bottom of a tall panel, as a chat should. */
  .chat-container {
    min-height: 100%;
    justify-content: flex-end;
  }

  .card-label { font-size: 12px; }
  .total-label { font-size: 13px; }
  .total-value { font-size: 16px; }
  .keep-line { font-size: 13px; min-height: 20px; }
  .bubble { font-size: 16px; padding: 11px 15px; }
  .action-slot { height: 72px; }
  .action-btn { font-size: 17px; }
  .switch-label { font-size: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
