/* ─────────────────────────────────────────────────────────
   turtleChat — style.css
───────────────────────────────────────────────────────── */

:root {
    --bg:            #f0f2f5;
    --app-bg:        #ffffff;
    --me-bubble:     #007aff;
    --me-text:       #ffffff;
    --them-bubble:   #e9ecef;
    --them-text:     #000000;
    --border:        #d1d5db;
    --system-text:   #8e8e93;
    --safe-top:      env(safe-area-inset-top,    20px);
    --safe-bottom:   env(safe-area-inset-bottom, 20px);

    /* status dot */
    --dot-on:        #34c759;
    --dot-wait:      #ff9f0a;
    --dot-off:       #ff3b30;

    /* modal */
    --modal-bg:      #ffffff;
    --input-bg:      #f2f2f7;
    --label-color:   #3c3c43;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0; padding: 0;
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100dvh;
    background: var(--bg);
    overflow: hidden;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

/* ── App shell ─────────────────────────────────────────── */
#app-container {
    width: 100%;
    max-width: 600px;
    height: 100dvh;
    background: var(--app-bg);
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 10px 30px rgba(0,0,0,.12);
}

@media (min-width: 601px) {
    body { padding: 20px; }
    #app-container {
        height: 90vh;
        border-radius: 24px;
        overflow: hidden;
        border: 8px solid #2a2a2a;
    }
}

/* ── Header ────────────────────────────────────────────── */
header {
    background: rgba(255,255,255,.95);
    backdrop-filter: blur(10px);
    padding: calc(var(--safe-top) + 12px) 16px 12px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 10;
    flex-shrink: 0;
}

.header-title {
    font-size: 1.05rem;
    font-weight: 700;
    letter-spacing: -.01em;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

#status-pill {
    display: flex;
    align-items: center;
    gap: 5px;
    background: var(--them-bubble);
    padding: 4px 10px;
    border-radius: 20px;
    font-size: .78rem;
    font-weight: 600;
    color: var(--system-text);
    max-width: 130px;
    overflow: hidden;
}

#status-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--dot-off);
    flex-shrink: 0;
    transition: background .3s;
}
#status-dot.on   { background: var(--dot-on); }
#status-dot.wait { background: var(--dot-wait); }

#status-nick {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#setup-btn {
    background: none;
    border: none;
    font-size: 1.3rem;
    cursor: pointer;
    padding: 2px;
    line-height: 1;
    opacity: .7;
    transition: opacity .15s;
}
#setup-btn:active { opacity: 1; }

/* ── Chat area ─────────────────────────────────────────── */
#chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

/* ── Message rows ──────────────────────────────────────── */
.message-row {
    display: flex;
    width: 100%;
}
.message-row.me     { justify-content: flex-end; }
.message-row.them   { justify-content: flex-start; }
.message-row.system { justify-content: center; }

/* ── Sender label ──────────────────────────────────────── */
.message-col { display: flex; flex-direction: column; max-width: 78%; }
.message-row.me .message-col { align-items: flex-end; }

.sender-label {
    font-size: .68rem;
    font-weight: 600;
    color: var(--system-text);
    margin-bottom: 3px;
    padding: 0 4px;
}

/* ── Text bubbles ──────────────────────────────────────── */
.bubble {
    padding: 10px 14px;
    border-radius: 20px;
    font-size: .975rem;
    line-height: 1.45;
    word-wrap: break-word;
    animation: popIn .22s cubic-bezier(.175,.885,.32,1.275) forwards;
    transform-origin: bottom center;
}

@keyframes popIn {
    from { opacity: 0; transform: scale(.82); }
    to   { opacity: 1; transform: scale(1); }
}

.message-row.me   .bubble { background: var(--me-bubble); color: var(--me-text); border-bottom-right-radius: 4px; }
.message-row.them .bubble { background: var(--them-bubble); color: var(--them-text); border-bottom-left-radius: 4px; }

/* ── System messages ───────────────────────────────────── */
.system-msg {
    font-size: .72rem;
    color: var(--system-text);
    text-align: center;
    padding: 2px 0;
}

/* ── History divider ───────────────────────────────────── */
.history-divider {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--system-text);
    font-size: .7rem;
    font-weight: 600;
}
.history-divider::before,
.history-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* ── Logo / canvas bubbles ─────────────────────────────── */
.bubble.logo-bubble {
    padding: 8px;
    background: #fff;
    border: 2px solid var(--border);
    border-radius: 18px;
    max-width: 100%;
}
.message-row.me .bubble.logo-bubble { border-color: var(--me-bubble); }

.canvas-wrapper {
    border-radius: 12px;
    overflow: hidden;
    box-shadow: inset 0 2px 6px rgba(0,0,0,.07);
    display: flex;
    justify-content: center;
    align-items: center;
}
/* ── Restored Canvas Interactivity ─────────────────────── */
.canvas-wrapper {
    cursor: pointer;
    transition: transform 0.1s;
}
.canvas-wrapper:active {
    transform: scale(0.97);
}

canvas {
    display: block;
    width: 100%;
    max-width: 280px;
    aspect-ratio: 1;
    background:
        linear-gradient(rgba(0,0,0,.035) 1px, transparent 1px),
        linear-gradient(90deg, rgba(0,0,0,.035) 1px, transparent 1px),
        #fff;
    background-size: 20px 20px;
}

.logo-indicator {
    font-size: .7rem;
    color: var(--system-text);
    margin-top: 4px;
    padding: 0 4px;
    font-weight: 600;
}

/* ── Footer ────────────────────────────────────────────── */
footer {
    background: var(--app-bg);
    padding: 10px 16px calc(var(--safe-bottom) + 10px);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: flex-end;
    gap: 10px;
    flex-shrink: 0;
}

#msg-input {
    flex: 1;
    background: var(--them-bubble);
    border: none;
    border-radius: 20px;
    padding: 10px 16px;
    font-size: .975rem;
    resize: none;
    max-height: 120px;
    min-height: 40px;
    outline: none;
    -webkit-user-select: text;
    user-select: text;
    line-height: 1.4;
}
#msg-input:disabled { opacity: .5; }

#send-btn {
    background: var(--me-bubble);
    color: #fff;
    border: none;
    width: 40px; height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    cursor: pointer;
    flex-shrink: 0;
    transition: transform .1s, background .15s;
}
#send-btn:active   { transform: scale(.88); }
#send-btn:disabled { background: #a0cbfc; cursor: not-allowed; }


/* ════════════════════════════════════════════════════════
   SETUP MODAL
════════════════════════════════════════════════════════ */

#modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.45);
    display: flex;
    align-items: flex-end;   /* sheet from bottom on mobile */
    justify-content: center;
    z-index: 100;
    backdrop-filter: blur(4px);
}

@media (min-width: 500px) {
    #modal-overlay { align-items: center; }
}

#modal-overlay.hidden { display: none; }

#setup-modal {
    background: var(--modal-bg);
    width: 100%;
    max-width: 480px;
    border-radius: 20px 20px 0 0;
    padding: 0 0 calc(var(--safe-bottom) + 8px);
    display: flex;
    flex-direction: column;
    max-height: 90vh;
    overflow-y: auto;
}

@media (min-width: 500px) {
    #setup-modal {
        border-radius: 20px;
        max-height: 80vh;
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 18px 20px 12px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--modal-bg);
    z-index: 1;
}

.modal-header h2 { font-size: 1.1rem; font-weight: 700; }

#modal-close-btn {
    background: var(--them-bubble);
    border: none;
    width: 28px; height: 28px;
    border-radius: 50%;
    font-size: .85rem;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    color: var(--system-text);
    transition: background .15s;
}
#modal-close-btn:active { background: #c8cdd3; }

/* ── Tabs ─────────────────────────────────────────────── */
.modal-tabs {
    display: flex;
    padding: 10px 20px 0;
    gap: 6px;
}

.tab-btn {
    flex: 1;
    padding: 8px;
    border: none;
    border-radius: 10px;
    background: var(--them-bubble);
    color: var(--system-text);
    font-size: .85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s, color .15s;
}
.tab-btn.active {
    background: var(--me-bubble);
    color: #fff;
}

/* ── Tab panels ───────────────────────────────────────── */
.tab-panel { display: none; padding: 16px 20px; flex-direction: column; gap: 14px; }
.tab-panel.active { display: flex; }

label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: .8rem;
    font-weight: 600;
    color: var(--label-color);
    -webkit-user-select: none;
    user-select: none;
}

label input[type="text"],
label input[type="url"],
label input[type="password"] {
    padding: 10px 12px;
    background: var(--input-bg);
    border: 1.5px solid transparent;
    border-radius: 10px;
    font-size: .95rem;
    outline: none;
    transition: border-color .15s;
    -webkit-user-select: text;
    user-select: text;
}

label input:focus { border-color: var(--me-bubble); }

label small {
    font-size: .72rem;
    font-weight: 400;
    color: var(--system-text);
}

.input-row {
    display: flex;
    gap: 6px;
    align-items: center;
}
.input-row input { flex: 1; }

.eye-btn {
    background: var(--input-bg);
    border: none;
    border-radius: 8px;
    width: 38px; height: 38px;
    cursor: pointer;
    font-size: 1rem;
    flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
}

.checkbox-label {
    flex-direction: row !important;
    align-items: center !important;
    gap: 8px !important;
}
.checkbox-label input { width: auto; }

.subtle-link {
    color: inherit;
    text-decoration: underline;
    text-decoration-style: dashed;
    text-decoration-color: var(--system-text);
    text-underline-offset: 2px;
}
.subtle-link:visited { color: inherit; }
/* ── Modal footer ─────────────────────────────────────── */
.modal-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px 6px;
    border-top: 1px solid var(--border);
    gap: 10px;
    flex-shrink: 0;
}

.primary-btn {
    background: var(--me-bubble);
    color: #fff;
    border: none;
    padding: 11px 22px;
    border-radius: 14px;
    font-size: .9rem;
    font-weight: 700;
    cursor: pointer;
    transition: opacity .15s;
}
.primary-btn:active { opacity: .8; }

.secondary-btn {
    background: var(--them-bubble);
    color: var(--label-color);
    border: none;
    padding: 10px 18px;
    border-radius: 14px;
    font-size: .85rem;
    font-weight: 600;
    cursor: pointer;
    width: 100%;
    transition: background .15s;
}
.secondary-btn:active { background: #d0d4d8; }

.danger-btn {
    background: none;
    color: #ff3b30;
    border: none;
    font-size: .8rem;
    font-weight: 600;
    cursor: pointer;
    padding: 4px;
    white-space: nowrap;
}

#register-result {
    font-size: .8rem;
    min-height: 1.2em;
    font-weight: 600;
}
#register-result.ok  { color: #34c759; }
#register-result.err { color: #ff3b30; }

.hidden {
    display: none !important;
}

/* ── SVG bubbles ───────────────────────────────────────── */
.bubble.svg-bubble {
    padding: 8px;
    background: #fff;
    border: 2px solid var(--border);
    border-radius: 18px;
    max-width: 100%;
}
.message-row.me .bubble.svg-bubble { border-color: var(--me-bubble); }

.svg-wrapper {
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.1s;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 120px;
    max-width: 280px;
}
.svg-wrapper:active { transform: scale(0.97); }
.svg-wrapper svg { max-width: 280px; max-height: 280px; display: block; }

/* ── Clipboard button ──────────────────────────────────── */
.copy-code-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: .9rem;
    padding: 0 2px;
    opacity: .5;
    transition: opacity .15s;
    vertical-align: middle;
    line-height: 1;
}
.copy-code-btn:active { opacity: 1; }

/* ── PM bubbles ─────────────────────────────────────────── */
.pm-row { justify-content: flex-start; }
.pm-row.mine { justify-content: flex-end; }

.pm-bubble {
    background: linear-gradient(135deg, #0d1b2a, #1b2838);
    border: 1.5px solid #4a9eff;
    border-radius: 16px;
    padding: 10px 14px;
    max-width: 80%;
    animation: popIn .22s cubic-bezier(.175,.885,.32,1.275) forwards;
}

.pm-header {
    font-size: .68rem;
    font-weight: 700;
    color: #4a9eff;
    margin-bottom: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.pm-label { flex: 1; }

.pm-receipt {
    font-size: .85rem;
    flex-shrink: 0;
    transition: opacity .3s;
}

.pm-text {
    font-size: .95rem;
    color: #d0e8ff;
    line-height: 1.45;
    word-wrap: break-word;
}

/* ── IM bubbles ─────────────────────────────────────────── */
.im-row { justify-content: center; }

.im-bubble {
    background: rgba(255, 159, 10, 0.1);
    border: 1px solid rgba(255, 159, 10, 0.3);
    border-radius: 16px;
    padding: 8px 16px;
    max-width: 85%;
    text-align: center;
    transition: opacity 3s ease;
    animation: popIn .22s cubic-bezier(.175,.885,.32,1.275) forwards;
}

.im-bubble.faded { opacity: 0.25; }

.im-nick {
    font-size: .68rem;
    font-weight: 700;
    color: #ff9f0a;
    margin-bottom: 3px;
}

.im-text {
    font-size: .9rem;
    color: #b87000;
    font-style: italic;
    line-height: 1.4;
}

/* ── AI Tab ─────────────────────────────────────────────── */
.ai-hidden { display: none; }

.ai-section {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.ai-divider {
    text-align: center;
    font-size: .75rem;
    color: var(--system-text);
    padding: 4px 0;
}

#tab-ai select {
    padding: 10px 12px;
    background: var(--input-bg);
    border: 1.5px solid transparent;
    border-radius: 10px;
    font-size: .95rem;
    outline: none;
    width: 100%;
    -webkit-user-select: text;
    user-select: text;
}
#tab-ai select:focus { border-color: var(--me-bubble); }
#tab-ai select:disabled { opacity: .5; }

#tab-ai textarea {
    padding: 10px 12px;
    background: var(--input-bg);
    border: 1.5px solid transparent;
    border-radius: 10px;
    font-size: .9rem;
    outline: none;
    width: 100%;
    -webkit-user-select: text;
    user-select: text;
    line-height: 1.4;
}
#tab-ai textarea:focus { border-color: var(--me-bubble); }